code acting a bit strange

This is a discussion on code acting a bit strange within the C++ Programming forums, part of the General Programming Boards category; Hi, just doing a simple progamme for my younger brother(a funny news spread).The programme asks for his name,favourite simpsons character ...

  1. #1
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26

    code acting a bit strange

    Hi,
    just doing a simple progamme for my younger brother(a funny news spread).The programme asks for his name,favourite simpsons character and the like but halve way thru the code it starts out putting 2 questions without waiting for input from the user.Cant see what im doin wrong,


    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    
    {
        string yourName;
        
        cout<<"please enter your name "<<endl;
        getline(cin,yourName);
        
        
        cout<<"thanks "<<yourName<<",would u mind answering afew questions?"<<endl;
        
        string yourGF;
        cout<<"What is your girlfriends name?"<<endl;
        getline(cin,yourGF);
        
        
        int favNumber=0;
        cout<<"what is your favourite number?"<<endl;
        cin>> favNumber;
        
        
        string Party;
        cout<<"what famous peson would u like at your party?"<<endl;
        getline(cin,Party);
        
        
        string hatedPet;
        cout<<"what is the pet animal you hate the most?"<<endl;
        getline(cin,hatedPet);
        
        
        string simpsons;
        cout<<"who is your favourite Simpsons character?"<<endl;
        getline(cin,simpsons);
        
        string favJob;
        cout<<"Nearly finished just a few more questions"<<endl;
        cout<<"What would you most like to do for a living?"<<endl;
        getline(cin,favJob);
        
        cin.get();
        return 0;
    }

    I put in cin.get() at the end so hat the console window doesnt shut
    Again any help much appreciated.

    Labmouse

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> but halve way thru the code it starts out putting 2 questions without waiting for input from the user.
    Exactly which part? You might try putting in a cin.ignore() at the offending lines.

  3. #3
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    works fine down as far as 'your favourite number',then prints the next 2 questions,then waits for user input and then prints out the next 2 questions.Will try and put in cin.ignore at the offending parts.
    thanks for quick reply

  4. #4
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    Yep Twomer put in the cin.ignore at the offending parts and it works fine.Im just curios why it works without cin.ignore for the 1st few questions and then starts acting quirky..

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    when you read in the int, there is still the rest of the line there.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    int favNumber=0;
    cout<<"what is your favourite number?"<<endl;
    cin>> favNumber;
    The cin >> call will leave the trailing newline character in the input stream. Then...

    Code:
    string Party;
    cout<<"what famous peson would u like at your party?"<<endl;
    getline(cin,Party);
        
    string hatedPet;
    cout<<"what is the pet animal you hate the most?"<<endl;
    getline(cin,hatedPet);
    The next call for input in the following getline statement sees the newline in the input stream and thinks that the user typed something in and pressed the "Enter" key so you see the question printed to the screen but you can't type in anything for that question. After that first getline eats the newline left over from the cin >> call, the second getline asking you for your "most hated pet animal" will work as intended.

    Mixing calls to cin >> and getline can confuse many that aren't aware of the details of what's going on and leave them scratching their heads. You should call cin.ignore() after any cin >> call that comes before a getline statement to eat the trailing newline... so in this case it looks like you only need it once in your sample code.

    By themselves, a group of cin >> calls or a group of getline calls all by themselves will work fine because of how they work in dealing with leading/leftover newlines/whitespace in the input stream. With cin >>, leading whitespace (including newlines) is stripped and then any data will get processed. With the getlines however, leading newlines are taken as the user having typed something in so there is a difference in how they work that you need to be aware of.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    If you're too lazy to write English properly why would you expect to ever write code properly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. output bit strange
    By onthewaytoC in forum C Programming
    Replies: 5
    Last Post: 06-10-2005, 05:36 AM
  2. Replies: 1
    Last Post: 01-10-2003, 09:08 PM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21