Program skipping some input

This is a discussion on Program skipping some input within the C++ Programming forums, part of the General Programming Boards category; Hey all, I recently started learning C++ off the tutorials on this site and they are great. I've started making ...

  1. #1
    nucakes
    Join Date
    Aug 2004
    Posts
    2

    Program skipping some input

    Hey all,

    I recently started learning C++ off the tutorials on this site and they are great. I've started making my own simple program which takes in information and trying to make it display it in a structured format. I first used int name, and int alias. When i ran the program it would work fine but when it came to cin>>name; and cin>>alias; it would close the program because the user was inputting letters and instead of numbers. So i changed some stuff around and this is what i have so far.

    #include <iostream.h>
    #include <string.h>
    int main()
    {
    char name[50], alias[50], filename[50];
    int age;
    int yearsexp;
    int yesorno;
    cout<<"Please enter your real name: ";
    cin.getline(name, 50, '\n');
    //Gets Name

    cout<<"Please enter your age: ";
    cin>>age;
    //Gets Age

    cout<<"How long have you been playing in years: ";
    cin>>yearsexp;
    //Gets years

    cout<<"Whats your in-game name: ";
    cin.getline(alias, 50, '\n');
    //Gets In-Game Name

    cout<<"This is all the info you have entered"<<endl;
    cout<<"NAME:"<<name<<endl;
    cout<<"AGE:"<<age<<endl;
    cout<<"Years Playing:"<<yearsexp<<endl;
    cout<<"ALIAS:"<<alias<<endl;
    //Displays information inputted from user

    cout<<"Is this information correct? Press 1(Yes) or 2(No) then enter: ";
    cin>>yesorno;
    if(yesorno==1)

    {
    cout<<"Please enter a 1-word name for the info to be saved in: ";
    cin.getline(filename, 50, '\n');
    cout<<"Cannot save file "<<filename<<endl;
    //Added the "Cannot save file" msg to inform user it cant do anything yet

    }
    else if(yesorno==2)
    {
    cout<<"Please restart program";
    //Loop function not implemented yet

    }
    return 0;
    }

    Excuse the untidyness. I compile and build the program just fine. But when i run it (via msdos) it takes in name part, then the age, years playing... and then "skips" the ALIAS part and goes right into the "do you want to save" part, if i pick yes it "skips" the filename part and closes the program.

    Could someone tell me why is it doing this? And if possible explain it in a way a beginner would understand.

    Thanks for taking the time to read this!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Your mixing of getline and cin is the cause of the problem here. After you enter the age using cin and press the enter key, cin converts the numbers entered and stores them into age however it leaves the newline/enter character in the input buffer. This newline character is read in by the next getline statement and you program is thinking that you pressed enter at this point to skip entering in anything for the alias. You can try putting cin.ignore(80,'\n'); after the cin statement and I think that should eat up any trailing newline characters. This happens again where you cin the yesorno varaible and then use getline on the filename.
    "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

  3. #3
    nucakes
    Join Date
    Aug 2004
    Posts
    2
    I did what you said and it works perfectly!

    Thank you!

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Cool

    Don't forget to leave hk_mp5kpdw some good feedback



    Hey hk_mp5kpdw .... nice to see a fellow virginian up in here
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Catching all input and sending it to another program
    By RedWind in forum C Programming
    Replies: 13
    Last Post: 09-15-2008, 01:51 PM
  2. Program that requests input of an integer number
    By theejuice in forum C Programming
    Replies: 6
    Last Post: 04-30-2008, 03:18 AM
  3. Replies: 4
    Last Post: 04-03-2008, 10:07 PM
  4. getting input from another program
    By adr in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2006, 03:29 AM
  5. skipping input
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2002, 02:22 PM

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