| | |
String input problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 17
Reputation:
Solved Threads: 0
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.
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.
My menu coding is as below.
C++ Syntax (Toggle Plain Text)
int main() { myCarSystem b; char ch; string temp1, temp2; double temp3, temp4; while(1) { cout<<endl<<endl; cout<<" My Car System Operations "<<endl; cout<<" ----------------------------- "<<endl; cout<<" 1. Insertion/Creation "<<endl; cout<<" 2. In-Order Traversal "<<endl; cout<<" 3. Removal "<<endl; cout<<" 4. Exit "<<endl; cout<<" Enter your choice : "; cin>>ch; switch(ch) { case 1 : cout<<" Enter car model to be inserted : " <<endl; fflush(stdin); getline(cin, temp1); cout<<"\n Enter car colour type : "<<endl; fflush(stdin); getline(cin, temp2); cout<<"\n Enter car price : "<<endl; fflush(stdin); cin>>temp3; cout<<"\n Enter car deposit : "<<endl; fflush(stdin); cin>>temp4; b.insert(temp1, temp2, temp3, temp4); break; case 2 : cout<<endl; cout<<" In-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_inorder(); break; case 3 : cout<<" Enter data to be deleted : "; cin>>temp1; b.remove(temp1); break; case 4 : system("pause"); return 0; break; } } return 0; }
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.
0
#2 Oct 19th, 2009
Try adding a
See this: http://www.cplusplus.com/reference/i...stream/ignore/
Incidentally, its better you don't use
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."
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."
•
•
Join Date: Oct 2008
Posts: 17
Reputation:
Solved Threads: 0
0
#4 Oct 19th, 2009
C++ Syntax (Toggle Plain Text)
int main() { myCarSystem b; char ch; string temp1, temp2; double temp3, temp4; while(1) { cout<<endl<<endl; cout<<" My Car System Operations "<<endl; cout<<" ----------------------------- "<<endl; cout<<" 1. Insertion/Creation "<<endl; cout<<" 2. In-Order Traversal "<<endl; cout<<" 3. Removal "<<endl; cout<<" 4. Exit "<<endl; cout<<" Enter your choice : "; cin>>ch; switch(ch) { case 1 : cout<<" Enter car model to be inserted : " <<endl; fflush(stdin); cin.ignore(256, ' '); getline(cin, temp1); cout<<"\n Enter car colour type : "<<endl; fflush(stdin); cin.ignore(256, ' '); getline(cin, temp2); cout<<"\n Enter car price : "<<endl; fflush(stdin); cin.ignore(); cin>>temp3; cout<<"\n Enter car deposit : "<<endl; fflush(stdin); cin.ignore(); cin>>temp4; b.insert(temp1, temp2, temp3, temp4); break; case 2 : cout<<endl; cout<<" In-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_inorder(); break; case 3 : cout<<" Enter data to be deleted : "; cin>>temp1; b.remove(temp1); break; case 4 : system("pause"); return 0; break; } } return 0; }
•
•
Join Date: Oct 2008
Posts: 17
Reputation:
Solved Threads: 0
0
#5 Oct 19th, 2009
•
•
•
•
Try adding acin.ignore();statement before each of your inputs.
See this: http://www.cplusplus.com/reference/i...stream/ignore/
Incidentally, its better you don't usefflush(stdin);. See here for more info.
C++ Syntax (Toggle Plain Text)
int main() { myCarSystem b; char ch; string temp1, temp2; double temp3, temp4; while(1) { cout<<endl<<endl; cout<<" My Car System Operations "<<endl; cout<<" ----------------------------- "<<endl; cout<<" 1. Insertion/Creation "<<endl; cout<<" 2. In-Order Traversal "<<endl; cout<<" 3. Removal "<<endl; cout<<" 4. Exit "<<endl; cout<<" Enter your choice : "; cin>>ch; switch(ch) { case 1 : cout<<" Enter car model to be inserted : " <<endl; cin.ignore(); getline(cin, temp1); cout<<"\n Enter car colour type : "<<endl; cin.ignore(); getline(cin, temp2); cout<<"\n Enter car price : "<<endl; cin.ignore(); cin>>temp3; cout<<"\n Enter car deposit : "<<endl; cin.ignore(); cin>>temp4; b.insert(temp1, temp2, temp3, temp4); break; case 2 : cout<<endl; cout<<" In-Order Traversal "<<endl; cout<<" -------------------"<<endl; b.print_inorder(); break; case 3 : cout<<" Enter data to be deleted : "; cin>>temp1; b.remove(temp1); break; case 4 : system("pause"); return 0; break; } } return 0; }
The problem still same.
Anyway thank you for the information.
0
#6 Oct 19th, 2009
If
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:
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)
cin.clear(); string rubbish; 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."
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."
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
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.
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"....
•
•
Join Date: Oct 2008
Posts: 17
Reputation:
Solved Threads: 0
0
#9 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 usecin >> temp1;
![]() |
Similar Threads
- string input (Java)
- char** for string array problem (C)
- String input problem (C++)
- Input problem using arrays (C++)
- Use string input for enum (C++)
- Masking a string input (Python)
- Number input problem. (C++)
Other Threads in the C++ Forum
- Previous Thread: Ignoring First Line of File I/O
- Next Thread: Please help with life, universe and everything problem
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





