Input Buffer

This is a discussion on Input Buffer within the C++ Programming forums, part of the General Programming Boards category; Hi. I'm a newbie to C++ still and have yet to learn classes (next chapter, might do it later). But ...

  1. #1
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96

    Input Buffer

    Hi. I'm a newbie to C++ still and have yet to learn classes (next chapter, might do it later). But I originally had this program in C (where I also had input buffer issues) but I wanted to try some C++ to make stuff simpler to write (and so far I'm liking it). However, when I was converting my job database program from C to C++ (rewriting it) I ran into the same input buffer problem again where the input buffer is storing the newline character. But I am using a switch statement so I can't get a string with getline as switch's must be integers.

    Here's my code (Which I commented to make it easy to read)
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    void writeinfo();
    void readinfo();
    void menu();
    int main()
    {
    	menu();
    	cout << "Program exiting..." << endl;
    	return 0;
    }
    
    void menu()
    {
    	int choice;
    	//********Start Menu Display
    	cout << "What would you like to do?" << endl;
    	cout << "1. Enter employee information" << endl;
    	cout << "2. Read employee information" << endl;
    	cout << "3. Exit" << endl;
    	//********End Menu Display
    
    	cout << "Enter a number: ";
    	cin >> choice;
    
    	//**********Start Choice Switch*************
    	switch (choice)
    	{
    		case 1:
    		writeinfo();
    		break;
    
    		case 2:
    		readinfo();
    		break;
    
    		case 3:
    		return;
    
    		default:
    		cout << "Please enter 1, 2, or 3 ONLY." << endl;
    		menu();
    	}
    	//**********End Choice Switch************
    }
    
    void writeinfo()
    {
    	string name, job, id;
    	ofstream jobFile("jobdatabase.txt", ios::app);
    	if (!jobFile)
    	{
    		cout << "ERROR: Could not open jobdatabase.txt" << endl;
    		menu();
    	}
    	else
    	{
    
    		// ********Start Getting Information**********
    
    		cout << "Please enter the worker's id number: ";
    		getline(cin,id);
    
    		cout << "Please enter the worker's name: ";
    		getline(cin,name);
    
    		cout << "Please enter the worker's duty: ";
    		getline(cin,job);
    
    		//*************End Get Info************
    		//*************Start Write Info********
    
    		jobFile << "Worker's id number: " << id << " Worker's name: " << name;
    		jobFile << " Worker's Job: " << job << endl;
    		jobFile.close();
    
    		//*************End Write Info**********
    		menu();
    	}
    }
    
    void readinfo()
    {
    	string databaseinfo;
    	ifstream jobFile("jobdatabase.txt");
    
    	if (! jobFile)
    	{
    		cout << "ERROR: Could not open jobdatabase.txt" << endl;
    		menu();
    	}
    	else
    	{
    		//**********Start Reading Information **********
    
    		while (!jobFile.eof())
    		{
    			getline(jobFile,databaseinfo);
    			cout << databaseinfo << endl;;
    		}
    		jobFile.close();
    		//**********End Reading Information ************
    		menu();
    	}
    }
    Basically, everything works except this input buffer issue that is causing it to skip over the worker id number entry input. Advice would be appreciated. Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Mixing cin's >> operator with getline is just as bad as mixing scanf with fgets. The (really!) quick and dirty way to fix it is to do this after every call to cin:
    Code:
    while ( cin.get() != '\n' )
      ;
    There are other ways, but that is by far the simplest. So you would simply place that code after
    Code:
    cin >> choice;
    My best code is written with the delete key.

  3. #3
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Ahhhhhhhh. Thank you VERY VERY VERY VERY VERY VERY MUCH.


    But as you said about mixing cin with getline.... What should I do in order to get strings and integers without mixing the two?
    Last edited by Padawan; 04-05-2004 at 05:53 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What should I do in order to get strings and integers without mixing the two?
    Always use string input and then perform conversions using stringstreams. The FAQ has a good article on how to do this.
    My best code is written with the delete key.

  5. #5
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Quote Originally Posted by Prelude
    >What should I do in order to get strings and integers without mixing the two?
    Always use string input and then perform conversions using stringstreams. The FAQ has a good article on how to do this.
    Alright just wanted to clarify. I'll do that, thanks a bunch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Can't seem to come to conclusion : cin.ignore()
    By SkyHi in forum C++ Programming
    Replies: 8
    Last Post: 05-13-2005, 08:57 PM
  3. How can you tell if the input buffer is empty?
    By Punkture in forum C Programming
    Replies: 1
    Last Post: 06-04-2003, 05:10 PM
  4. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 02:17 PM
  5. Flushing the input buffer.
    By civix in forum C++ Programming
    Replies: 6
    Last Post: 08-19-2002, 06:41 PM

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