String input problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 17
Reputation: cjjack88 is an unknown quantity at this point 
Solved Threads: 0
cjjack88 cjjack88 is offline Offline
Newbie Poster

String input problem

 
0
  #1
Oct 19th, 2009
Hi, i am new in c++. I am doing an assignment where I need a menu for user to select the operation.

My menu coding is as below.
  1. int main()
  2. {
  3. myCarSystem b;
  4. char ch;
  5. string temp1, temp2;
  6. double temp3, temp4;
  7. while(1)
  8. {
  9. cout<<endl<<endl;
  10. cout<<" My Car System Operations "<<endl;
  11. cout<<" ----------------------------- "<<endl;
  12. cout<<" 1. Insertion/Creation "<<endl;
  13. cout<<" 2. In-Order Traversal "<<endl;
  14. cout<<" 3. Removal "<<endl;
  15. cout<<" 4. Exit "<<endl;
  16. cout<<" Enter your choice : ";
  17. cin>>ch;
  18. switch(ch)
  19. {
  20. case 1 : cout<<" Enter car model to be inserted : " <<endl;
  21. fflush(stdin);
  22. getline(cin, temp1);
  23. cout<<"\n Enter car colour type : "<<endl;
  24. fflush(stdin);
  25. getline(cin, temp2);
  26. cout<<"\n Enter car price : "<<endl;
  27. fflush(stdin);
  28. cin>>temp3;
  29. cout<<"\n Enter car deposit : "<<endl;
  30. fflush(stdin);
  31. cin>>temp4;
  32. b.insert(temp1, temp2, temp3, temp4);
  33. break;
  34. case 2 : cout<<endl;
  35. cout<<" In-Order Traversal "<<endl;
  36. cout<<" -------------------"<<endl;
  37. b.print_inorder();
  38. break;
  39. case 3 : cout<<" Enter data to be deleted : ";
  40. cin>>temp1;
  41. b.remove(temp1);
  42. break;
  43. case 4 : system("pause");
  44. return 0;
  45. break;
  46. }
  47. }
  48.  
  49. return 0;
  50. }

My problem is, when user is selects the operation, like example user selects 1 to enter car information, the screen will jump back to the menu and skip the input information part.
please help me to check this problem.
Thank you.
Cjjack88.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 155
Reputation: amrith92 is on a distinguished road 
Solved Threads: 18
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster
 
0
  #2
Oct 19th, 2009
Try adding a cin.ignore(); statement before each of your inputs.

See this: http://www.cplusplus.com/reference/i...stream/ignore/

Incidentally, its better you don't use fflush(stdin); . See here for more info.
Last edited by amrith92; Oct 19th, 2009 at 3:56 pm.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 17
Reputation: cjjack88 is an unknown quantity at this point 
Solved Threads: 0
cjjack88 cjjack88 is offline Offline
Newbie Poster
 
0
  #3
Oct 19th, 2009
Thank you for reply.

I add cin.ignore();, but the problem still occur.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 17
Reputation: cjjack88 is an unknown quantity at this point 
Solved Threads: 0
cjjack88 cjjack88 is offline Offline
Newbie Poster
 
0
  #4
Oct 19th, 2009
  1. int main()
  2. {
  3. myCarSystem b;
  4. char ch;
  5. string temp1, temp2;
  6. double temp3, temp4;
  7. while(1)
  8. {
  9. cout<<endl<<endl;
  10. cout<<" My Car System Operations "<<endl;
  11. cout<<" ----------------------------- "<<endl;
  12. cout<<" 1. Insertion/Creation "<<endl;
  13. cout<<" 2. In-Order Traversal "<<endl;
  14. cout<<" 3. Removal "<<endl;
  15. cout<<" 4. Exit "<<endl;
  16. cout<<" Enter your choice : ";
  17. cin>>ch;
  18. switch(ch)
  19. {
  20. case 1 : cout<<" Enter car model to be inserted : " <<endl;
  21. fflush(stdin);
  22. cin.ignore(256, ' ');
  23. getline(cin, temp1);
  24. cout<<"\n Enter car colour type : "<<endl;
  25. fflush(stdin);
  26. cin.ignore(256, ' ');
  27. getline(cin, temp2);
  28. cout<<"\n Enter car price : "<<endl;
  29. fflush(stdin);
  30. cin.ignore();
  31. cin>>temp3;
  32. cout<<"\n Enter car deposit : "<<endl;
  33. fflush(stdin);
  34. cin.ignore();
  35. cin>>temp4;
  36. b.insert(temp1, temp2, temp3, temp4);
  37. break;
  38. case 2 : cout<<endl;
  39. cout<<" In-Order Traversal "<<endl;
  40. cout<<" -------------------"<<endl;
  41. b.print_inorder();
  42. break;
  43. case 3 : cout<<" Enter data to be deleted : ";
  44. cin>>temp1;
  45. b.remove(temp1);
  46. break;
  47. case 4 : system("pause");
  48. return 0;
  49. break;
  50. }
  51. }
  52.  
  53. return 0;
  54. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 17
Reputation: cjjack88 is an unknown quantity at this point 
Solved Threads: 0
cjjack88 cjjack88 is offline Offline
Newbie Poster
 
0
  #5
Oct 19th, 2009
Originally Posted by amrith92 View Post
Try adding a cin.ignore(); statement before each of your inputs.

See this: http://www.cplusplus.com/reference/i...stream/ignore/

Incidentally, its better you don't use fflush(stdin); . See here for more info.
  1. int main()
  2. {
  3. myCarSystem b;
  4. char ch;
  5. string temp1, temp2;
  6. double temp3, temp4;
  7. while(1)
  8. {
  9. cout<<endl<<endl;
  10. cout<<" My Car System Operations "<<endl;
  11. cout<<" ----------------------------- "<<endl;
  12. cout<<" 1. Insertion/Creation "<<endl;
  13. cout<<" 2. In-Order Traversal "<<endl;
  14. cout<<" 3. Removal "<<endl;
  15. cout<<" 4. Exit "<<endl;
  16. cout<<" Enter your choice : ";
  17. cin>>ch;
  18. switch(ch)
  19. {
  20. case 1 : cout<<" Enter car model to be inserted : " <<endl;
  21. cin.ignore();
  22. getline(cin, temp1);
  23.  
  24. cout<<"\n Enter car colour type : "<<endl;
  25. cin.ignore();
  26. getline(cin, temp2);
  27.  
  28. cout<<"\n Enter car price : "<<endl;
  29. cin.ignore();
  30. cin>>temp3;
  31.  
  32. cout<<"\n Enter car deposit : "<<endl;
  33. cin.ignore();
  34. cin>>temp4;
  35.  
  36. b.insert(temp1, temp2, temp3, temp4);
  37. break;
  38. case 2 : cout<<endl;
  39. cout<<" In-Order Traversal "<<endl;
  40. cout<<" -------------------"<<endl;
  41. b.print_inorder();
  42. break;
  43. case 3 : cout<<" Enter data to be deleted : ";
  44. cin>>temp1;
  45. b.remove(temp1);
  46. break;
  47. case 4 : system("pause");
  48. return 0;
  49. break;
  50. }
  51. }
  52.  
  53. return 0;
  54. }

The problem still same.
Anyway thank you for the information.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 155
Reputation: amrith92 is on a distinguished road 
Solved Threads: 18
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster
 
0
  #6
Oct 19th, 2009
If cin.ignore(); fails, then try clearing the input stream, before accepting input. This can be done with cin.clear(); .

If all your inputs are failing, then maybe try the following:

1. Clear the input stream.
2. Read the rest of the characters in the stream into a temporary string.
3. Accept the user's input

So, for example, the statements to do this might be:
  1. cin.clear();
  2. string rubbish;
  3. cin >> rubbish;
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training
 
1
  #7
Oct 19th, 2009
Before worrying about big things please pay some attention towards small things...
There is a lot of difference between 1 and '1' .
The input you are taking is a character so the cases of switch should be as case '1' : case '2' : and all , else make the input to be taken to a integer rather than a character.

And ya usage of fflush(stdin) is bad. One more point of concern is all the inputs you are taking doesn't actually need a space and hence you don't even need to use getline().You can directly use cin >> temp1;
4 posts without even concentrating on the main problem.This way you will add all the statements in this world but still wont be able to take the inputs.
Last edited by csurfer; Oct 19th, 2009 at 4:25 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 17
Reputation: cjjack88 is an unknown quantity at this point 
Solved Threads: 0
cjjack88 cjjack88 is offline Offline
Newbie Poster
 
0
  #8
Oct 19th, 2009
I found where is the problem. I did a careless mistake.
amrith92 , thank you for your help and informations.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 17
Reputation: cjjack88 is an unknown quantity at this point 
Solved Threads: 0
cjjack88 cjjack88 is offline Offline
Newbie Poster
 
0
  #9
Oct 19th, 2009
Originally Posted by csurfer View Post
Before worrying about big things please pay some attention towards small things...
There is a lot of difference between 1 and '1' .
The input you are taking is a character so the cases of switch should be as case '1' case '2' and all , else make the input to be taken to a integer rather than a character.

And ya usage of fflush(stdin) is bad. One more point of concern is all the inputs you are taking doesn't actually need a space and hence you don't even need to use getline().You can directly use cin >> temp1;
Thank you for reply. I did a careless mistake.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 239 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC