Program not pausing cin and cin.get() to Accept input.

Hello, developers. I wrote a C++ console application and encountered a problem. Only the first cin>> statement pauses to allow user to supply input. After that, the program just runs through without giving the user to enter data. The variable fields, as a result, remain blank. What can I do to prevent this? The code is pasted below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   cout<< "\n Enter borrowerName: ";
   cin.get(borrowerName2, 29);

   cout<<"\n Enter Author,s name: ";
   cin.get(author2, 29);

   cout << "\n Enter ISBN: " ;
   cin.get(isbn2, 14);

   cout<< "\nEnter Publishing Year: ";
   cin.get(pubYear2, 4);

   cout<<"\nEnter Date Borrowed (dd/mm/yyyy): ";
   cin.get(dateBorrowed2, 10);

   cout<<"\nEnter Date Returned (dd/mm/yyyy): ";
   cin.get(dateReturned2, 10);

   cout<< "\n Enter ID: ";
   cin>> ID2;

You need to use cin.ignore (1, '\n'); to remove the newline from the stream buffer.

Or change cin.get() to cin.getline()
Thanks! both methods are working fine.

One more question, which type of loop would work well if the user was to be given the opportunity to enter as many records as he/she wished? I tried a while loop and it didn't work very well (allows only one record then goes in a perpetual loop,

Thanks..

After the line cin>> ID2; you will need to remove the newline character '\n' from the input buffer, in order that the following cin.getline() will work. Try cin.ignore() for that purpose.

If there are still problems, you'll need to show the latest version of your code.
which type of loop would work well if the user was to be given the opportunity to enter as many records as he/she wished?


For loop
Still having trouble with the loop. The menu gets displayed, user enters one record, menu displays again then exits without giving the user a chance to make a selection. The code is pasted below. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 choice = '0';

   while (!stop) {
	   
	   cout<< "1. Add Data\n";
	   cout<<"3. Exit\n";
	   cout<<"\nEnter Option Number: ";
	   cin>> choice;
	   cin.ignore(1,'\n');

	   if (choice == '1'){

		  cout<< "\n Enter ID: ";
                  cin>> ID2;
                  cin.ignore(100, '\n');

                  cout<< "\n Enter borrowerName: ";
                  cin.getline(borrowerName2, 29);

                  cout<<"\n Enter Author,s name: ";
                  cin.getline(author2, 29);

                  cout << "\n Enter ISBN: " ;
                  cin.getline(isbn2, 14);

                  cout<< "\nEnter Publishing Year: ";
                  cin>> pubYear2;
                  //cin.getline(pubYear2, 4);   

                 cout<<"\nEnter Date Borrowed (dd/mm/yyyy): ";
                 cin.getline(dateBorrowed2, 10);
                 cin.ignore(1, '\n');

                 cout<<"\nEnter Date Returned (dd/mm/yyyy): ";
                 cin.getline(dateReturned2, 10);
                 cin.ignore(1, '\n'); 

  
    
                 List Trans;
 
                 Trans.AddNode(ID2, borrowerName2, author2, isbn2, pubYear2,    
                 dateBorrowed2, dateReturned2);    
                 Trans.PrintList();
                 Trans.DeleteNode(3);
                 Trans.PrintList(); 
		
                 getch();
	   }
	   else if (choice ='3')
		   break;
		   	   
   }
   
      
return 0;
}
Last edited on
Line 50 else if (choice ='3') should have the == operator, rather than =
Some of the length values in the cin.getline() don't seem correct.
Because I can't see all of the code, it's a bit hard to tell. But for example:
30
31
32
    cout<<"\nEnter Date Borrowed (dd/mm/yyyy): ";
    cin.getline(dateBorrowed2, 10);
    cin.ignore(1, '\n');

The date is requested in the format "dd/mm/yyyy", That's 10 characters. Allowing one byte for the null terminator, that means we'd expect this definition:
 
char dateBorrowed2[11];
and the getline() should also specify 11, otherwise the last character of the date will be lost. Perhaps the safest way to code that is to put
 
    cin.getline(dateBorrowed2, sizeof(dateBorrowed2) );
and let the compiler work out the correct length for us.

Now there are a couple of other things to consider. What happens if the user accidentally types one (or more) character too many? Well, the getline will read the required 10 characters, but since it didn't encounter the newline character, it sets the fail flag for cin. In that case we need to clear the flag, and also discard characters from the input buffer until the newline is found. (If the user entered a date of the correct length or shorter, we don't need to do this, as getline will have already discarded the newline character for us.

So, with some basic error-checking, the code will look like this:
30
31
32
33
34
35
    cout << "\nEnter Date Borrowed (dd/mm/yyyy): ";
    if (!cin.getline(dateBorrowed2, sizeof(dateBorrowed2)))
    {
        cin.clear();
        cin.ignore(1000, '\n');
    }


The same principle applies to all of the getline() statements here.

See the reference page on getline:
http://www.cplusplus.com/reference/istream/istream/getline/

Last edited on
Thanks for your help, Softrix and Chervil. The program is now working well. Sorry for the delayed feedback.

Have a great day.
Topic archived. No new replies allowed.