skipping input

This is a discussion on skipping input within the C++ Programming forums, part of the General Programming Boards category; Hi I have a problem with an input line in my code. When it should ask for the input for ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    11

    Question skipping input

    Hi I have a problem with an input line in my code. When it should ask for the input for "name" it skips over it and goes to the imput for "school". What am I missing.

    Input screen
    -------------------
    Title: Manager
    Number: 21
    Name: School: DeVry //this is where the problem is
    Degree: BSC

    Output screen
    -------------------
    Title: Manager
    Number: 21
    Name: //see, it skipped the imput
    School: DeVry
    Degree: BSC
    Press any key to continue

    This is my code:
    --------------------------
    Code:
    #include<iostream.h>
    #include<string.h>
    #include<conio.h>
    
    class Student
    {
    private:
    	char school[20];
    	char degree[20];
    public:
    	void getData();
    	void putData();
    };
    
    class Employee
    {
    private:
    	char name[20];
    	int num;
    public:
    	void getData();
    	void putData();
    };
    
    class Manager
    {
    private:
    	char title[20];
    	Employee em;
    	Student st;
    public:
    	void getData();
    	void putData();
    };
    
    void Student::getData()
    {
    	cout<<"School: ";
    	cin.getline(school, 20, '\n');
    	cout<<"Degree: ";
    	cin.getline(degree, 20, '\n');
    }
    
    void Student::putData()
    {
    	cout<<"School: "<<school<<endl;
    	cout<<"Degree: "<<degree<<endl;
    }
    
    void Employee::getData()
    {
    	cout<<"Number: ";
    	cin>>num;
    	cout<<"Name: ";                       //THIS IS THE PROBLEM
    	cin.getline(name, 20, '\n');
    }
    
    void Employee::putData()
    {
    	cout<<"Number: "<<num<<endl;
    	cout<<"Name: "<<name<<endl;
    }
    
    void Manager::getData()
    {
    	cout<<"Title: ";
    	cin.getline(title, 20, '\n');
    	em.getData();
    	st.getData();
    }
    
    void Manager::putData()
    {
    	cout<<"Title: "<<title<<endl;
    	em.putData();
    	st.putData();
    }
    
    void main (void)
    {
    	Manager m;
    	m.getData();
    	m.putData();
    	return;
    }
    Code tags added by Kermi3

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You are mixing calls to cin >> and cin.getline. this can cause trouble as you have found. The behaviour is caused because cin>> leaves a \n in the streambuf which is read by the cin.getline call and taken as input. use cin.ignore() to rid the newline from the streambuf.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    I would suggest getting out of the world of C based string and use the C++ string datatype.

    This way, when you get the input for the string you can use
    getline(cin, stringName);
    and then place a
    cin.get();
    to get rid of the pesky character return that will pop up next time you call getline and really stuff things up.

    This should help with the errors in your program.
    Later,
    WebmasterMattD
    WebmasterMattD.NET

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    In the furture please use Code Tags. People will be much more likely to help you if you do. And they'll be happy about it

    Information on code tags may be found at:

    http://www.cprogramming.com/cboard/...&threadid=13473
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I use the following to remove every character waiting in the buffer and to reset cin.
    Nothing else I've tried has been completely "user-proof".

    Code:
     
        void flush_input()
        {
            //Clear all error flags
            std::cin.clear();
            //Remove all characters waiting to be read
            while (std::cin.rdbuf()->in_avail() > 0) std::cin.get();
        }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by Sang-drax
    I use the following to remove every character waiting in the buffer and to reset cin.
    Nothing else I've tried has been completely "user-proof".

    Nothing is ever "user-proof", I'm sure many people here agree
    Couldn't think of anything interesting, cool or funny - sorry.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by endo
    Nothing is ever "user-proof", I'm sure many people here agree
    That is true indeed

    However my function prevents strange behaviour when the user enters two numbers or a string when I ask for an int etc.
    But user-proof... no, it's not user-proof.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 12:09 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM

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