Thread: cin.ignore or flush needed?

  1. #1
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Talking cin.ignore or flush needed?

    Hi,

    Try to run this code:

    Code:
    #include<iostream.h>
    #include<windows.h>
    #include<conio.h>
    //constants
    const int TMAX = 30;
    const int PMAX = 15;
    const int CMAX = 100;
    //class
    class books
    {
    private:
    	char title[TMAX];
    	char publisher[PMAX];
    	double price;
    	int quantity;
    public:
    	void input()
    	{
    		cout<<"\n\n\t[ Title ] :: "<<flush;
    		cin.get(title, TMAX);
    		cin.ignore(10,'\n');
    		cout<<"\n\t[ Publisher ] :: "<<flush;
    		cin.get(publisher, PMAX);
    		cin.ignore(10,'\n');
    		cout<<"\n\t[ Price ] :: ";
    		cin>>price;
    		cout<<"\n\t[ Quantity ] :: "<<flush;
    		cin>>quantity;
    	}
    	void output()
    	{
    		cout<<"\n\t[ Title ] :: "<<title<<endl;
    		cout<<"\t[ Publisher ] :: "<<publisher<<endl;
    		cout<<"\t[ Price ] :: "<<price<<endl;
    		cout<<"\t[ Quantity ] :: "<<quantity<<endl<<flush;
    	}
    	void sell()
    	{
    		--quantity;
    	}
    };
    //main
    void main()
    {
    	books book[CMAX];
    	int p = 0,j;
    	int booknumber;
    	char choice = 'x';
    	while (choice != 'q' && choice != 'Q')
    	{
    		system("CLS");
    		cout<<"\n\n\t:: [ BOOK STORE MENU ] ::"<<endl;
    		cout<<"\n\t[ [A]dd Books ]";
    		cout<<"\n\t[ [D]isplay Books ]";
    		cout<<"\n\t[ [S]ell a Book ]";
    		cout<<"\n\t[ [Q]uit ]";
    		cout<<"\n\n\t::::[ ]::::\b\b\b\b\b\b"<<flush;
    		choice = getche();
    		switch(choice)
    		{
    		case 'a':
    		case 'A': 
    			system("CLS");
    			cout<<"\n\n\t:: Enter Data For Book "<<p+1<<" ::"<<endl;
    			book[p++].input();
    			break;
    		case 'd':
    		case 'D':
    			system("CLS");
    			for (j=0;j<p;j++)
    			{
    				cout<<"\n\n\t:: Data for Book "<<j+1<<" ::"<<endl;
    				book[j].output();
    			}
    			getch();
    			break;
    		case 's':
    		case 'S':
    			system("CLS");
    			cout<<"\n\n\t:: Enter Book Number :: [   ]\b\b\b\b";
    			cin>>booknumber;
    			book[booknumber-1].sell();
    			cout<<"\n\n\t:: New Data for Book "<<booknumber<<" ::"<<endl;
    			book[booknumber-1].output();
    			getch();
    		case 'q':
    		case 'Q':
    			break;
    		}
    	}
    }
    It should work well the first time you input book details.
    However, when you come to input the details for the second book, it skips the book title!

    Also, I haven't added conditional statements that will handle cases where the user inputs incorrect data, still have to code it

    Marc
    No matter how much you know, you NEVER know enough.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    yes, a cin.ignore after you input quantity should do the trick. I also might as well tell you now, since someone undoubtedly will, 'int main' should be used instead of 'void main'

  3. #3
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Thanks, it worked!

    a question if you don't mind
    ..How did you know about that? Is it experience or is there a general pattern one should follow when using cin.ignore?

    Oh, yeh I will use int main() from now on
    I don't think it makes any difference at this stage, but anyway I guess I better get used to using it for later

    Marc
    No matter how much you know, you NEVER know enough.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    yes its from experience, ive had that same problem when i started programming with iostream.

    One thing you should understand about the extraction '>>' operator is that it doesnt remove the delimiter (ie the character that determines when to stop inputting data, '\n' in this case), and cin.get stops reading at the delimiter. So, an '>>' followed by a cin.get will cause cin.get to think you just pressed enter since thats what was left in the buffer from the >> operation, hence cin.ignore removes the delimiter and allows cin.get to recieve your input correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. endl - \n
    By Hugo716 in forum C++ Programming
    Replies: 8
    Last Post: 05-25-2006, 02:33 PM
  2. flush socket
    By Snip in forum Networking/Device Communication
    Replies: 17
    Last Post: 02-08-2005, 09:29 PM
  3. need help with cin.get, or cin.ignore
    By yoyo in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2003, 01:14 AM
  4. How do I print a pattern flush right?
    By Basia in forum C Programming
    Replies: 5
    Last Post: 06-11-2002, 07:15 AM
  5. Can someone xplain FLUSH in strings?
    By aspand in forum C Programming
    Replies: 9
    Last Post: 05-13-2002, 12:10 AM