944,038 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 470
  • C++ RSS
Oct 19th, 2009
0

String input problem

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjjack88 is offline Offline
17 posts
since Oct 2008
Oct 19th, 2009
0
Re: String input problem
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.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
Oct 19th, 2009
0
Re: String input problem
Thank you for reply.

I add cin.ignore();, but the problem still occur.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjjack88 is offline Offline
17 posts
since Oct 2008
Oct 19th, 2009
0
Re: String input problem
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjjack88 is offline Offline
17 posts
since Oct 2008
Oct 19th, 2009
0
Re: String input problem
Click to Expand / Collapse  Quote originally posted by amrith92 ...
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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjjack88 is offline Offline
17 posts
since Oct 2008
Oct 19th, 2009
0
Re: String input problem
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:
C++ Syntax (Toggle Plain Text)
  1. cin.clear();
  2. string rubbish;
  3. cin >> rubbish;
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
Oct 19th, 2009
1
Re: String input problem
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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Oct 19th, 2009
0
Re: String input problem
I found where is the problem. I did a careless mistake.
amrith92 , thank you for your help and informations.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjjack88 is offline Offline
17 posts
since Oct 2008
Oct 19th, 2009
0
Re: String input problem
Click to Expand / Collapse  Quote originally posted by csurfer ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjjack88 is offline Offline
17 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Ignoring First Line of File I/O
Next Thread in C++ Forum Timeline: Please help with life, universe and everything problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC