jumping beens, well... jumping programs, aaahuuu-aah

Discussion in 'C++' started by jalkadir, Oct 5, 2005.

  1. jalkadir

    jalkadir Guest

    By jump I mean ending unexpectedly.
    The program below, like the jumping beens, has an unusual behaviour.
    When the noted lines are not present the program terminates
    disregarding the rest of the lines.

    I have seen this type of behaviour before, but I can't find to recall
    the source, the needy greedy source, of the problem. I, however, was
    able to remember that the problem was resolved, temporarily, by using
    'std::cin.get()', but I am afraid this will lead to a bug in the
    future.

    Can anyone help me by explaining what causes this type of behaviour,
    why my solution works and, most importantly, how to solve the problem.

    Thanks in advance

    ---
    void getData(){
    jme::Address address2;
    std::string str;
    char* cstr;
    std::cout.flush();
    //std::cin.get().flush();

    str.clear();
    std::cout << "House/Appartment # ";
    std::cin >> str;
    std::cin.get(); // Without this line the program jumps <====
    address2.setUnitNumber(str);

    std::cout << "Enter street name: ";
    std::cin.get(cstr,255);
    address2.setStreetName(cstr);

    str.clear();
    std::cout << "City/Town: ";
    std::cin >> str;
    address2.setCity(str);

    str.clear();
    std::cout << "Province/State: ";
    std::cin >> str;
    address2.setProvince(str);

    str.clear();
    std::cout << "Country: ";
    std::cin >> str;
    address2.setCountry(str);

    str.clear();
    std::cout << "Postal Code: ";
    std::cin >> str;
    address2.setPostalCode(str);
    std::cin.get();

    Display(address2, "getData()");
    }
     
    jalkadir, Oct 5, 2005
    #1
    1. Advertising

  2. jalkadir wrote:
    > By jump I mean ending unexpectedly.


    By throwing an exception maybe? Use 'try/catch' to see if that's so.

    > The program below, like the jumping beens, has an unusual behaviour.


    What's a "been"?

    > When the noted lines are not present the program terminates
    > disregarding the rest of the lines.


    Have you tried debugging it? Does it jump (throw) or does it just run
    through all of them and you don't get to see how it executes them all?

    > I have seen this type of behaviour before, but I can't find to recall
    > the source, the needy greedy source, of the problem. I, however, was
    > able to remember that the problem was resolved, temporarily, by using
    > 'std::cin.get()', but I am afraid this will lead to a bug in the
    > future.
    >
    > Can anyone help me by explaining what causes this type of behaviour,
    > why my solution works and, most importantly, how to solve the problem.
    >
    > Thanks in advance
    >
    > ---
    > void getData(){
    > jme::Address address2;
    > std::string str;
    > char* cstr;
    > std::cout.flush();
    > //std::cin.get().flush();
    >
    > str.clear();
    > std::cout << "House/Appartment # ";
    > std::cin >> str;
    > std::cin.get(); // Without this line the program jumps <====


    Often, when you read a 'string' from a stream, the \n is left in the
    stream, and needs to be read before the next "get" is attempted. I
    recommend using

    std::cin.ignore(INT_MAX, '\n');

    instead of 'get'.

    Beyond that I don't know what to tell you. Since you didn't post your
    _complete_ program and the input you're giving it, there is no way to
    reproduce your behaviour (except accidentally). Read the FAQ 5.8.

    > address2.setUnitNumber(str);
    >
    > std::cout << "Enter street name: ";
    > std::cin.get(cstr,255);
    > address2.setStreetName(cstr);
    >
    > str.clear();
    > std::cout << "City/Town: ";
    > std::cin >> str;
    > address2.setCity(str);
    >
    > str.clear();
    > std::cout << "Province/State: ";
    > std::cin >> str;
    > address2.setProvince(str);
    >
    > str.clear();
    > std::cout << "Country: ";
    > std::cin >> str;
    > address2.setCountry(str);
    >
    > str.clear();
    > std::cout << "Postal Code: ";
    > std::cin >> str;
    > address2.setPostalCode(str);
    > std::cin.get();
    >
    > Display(address2, "getData()");
    > }
    >


    V
     
    Victor Bazarov, Oct 5, 2005
    #2
    1. Advertising

  3. jalkadir wrote:
    >
    > By jump I mean ending unexpectedly.
    > The program below, like the jumping beens, has an unusual behaviour.
    > When the noted lines are not present the program terminates
    > disregarding the rest of the lines.
    >
    > I have seen this type of behaviour before, but I can't find to recall
    > the source, the needy greedy source, of the problem. I, however, was
    > able to remember that the problem was resolved, temporarily, by using
    > 'std::cin.get()', but I am afraid this will lead to a bug in the
    > future.
    >
    > Can anyone help me by explaining what causes this type of behaviour,
    > why my solution works and, most importantly, how to solve the problem.
    >
    > Thanks in advance
    >
    > ---
    > void getData(){
    > jme::Address address2;
    > std::string str;
    > char* cstr;
    > std::cout.flush();
    > //std::cin.get().flush();
    >
    > str.clear();
    > std::cout << "House/Appartment # ";
    > std::cin >> str;
    > std::cin.get(); // Without this line the program jumps <====


    Whatever 'jump' means.
    I guess you mean, the next input seems to be ignored.
    Well that is easy to explain: When you enter some number, what *exactly*
    do you enter? You press eg. the keys '1', then '3' (because you want to
    enter the number 13) and then? Then you press 'return'. And that 'return'
    is still waiting to be processed after cin >> str has fetched the '1' and '3'.
    That is why you intoduced the read of a single character: To read that 'return'
    still waiting for input. If you don't do it now, then the next get will grab
    his hands on that waiting 'return' and conclude that the user made an empty input.

    > address2.setUnitNumber(str);
    >
    > std::cout << "Enter street name: ";
    > std::cin.get(cstr,255);


    Ouuuh. That is a big no-no.
    cstr is a pointer. But a pointer to where? Where is the memory
    where get() should put the characters and whos address you take
    from cstr?
    Answer: There is none! cstr is an uninitialized pointer and only god
    knows where it points to in memory. Why didn't you use the variable 'str'
    as you did above to get a string from the user?


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 5, 2005
    #3
  4. jalkadir

    jalkadir Guest

    Thanks Vic for your prompt response.

    First of all, I'd like to get the beens thing out of the way. In
    reality it is spelled beans.
    The expression "jumping beans" is an expression commonly used by
    people from Mexico, but because of their accent one, jokingly and
    without the intention to disrespect them, refer to the use of the
    expression as "Jumping Beens" or even "Jumping Beeens" to try
    to convey the idea that in the word 'beans' the sound of the letter
    E is extended, as it is the way the some Mexicans pronounce the word
    beans.

    And as my second and final point in this message, thanks again for your
    help, I will be trying that ASAP.

    To my fellow Mexican friends, my dearest regards and apologies if in
    some way I have offended you.

    Adios, good bye, Ciao, Assalamu Alaykum.
     
    jalkadir, Oct 5, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    1,680
    Dave Thompson
    Jan 10, 2005
  2. Coca
    Replies:
    7
    Views:
    762
    Aidan Grey
    Aug 24, 2004
  3. Replies:
    18
    Views:
    642
    Dave Thompson
    Jan 10, 2005
  4. lone_eagle
    Replies:
    3
    Views:
    656
    psykeedelik
    May 26, 2009
  5. Casey Hawthorne
    Replies:
    14
    Views:
    462
Loading...

Share This Page