How do I fix my C++ case switch menu?

DemiSheep Source

I am having a problem with my school project. I am writing it using VIM in UNIX on the school server. When I go to insert a record in my project, Last name gets skipped. First and last name are prompted simultaneously without waiting for Last Name to receive it's input. See below:

my menu is doing this:

MENU
(I)nsert new record
(L)ast name search
(S)ave database to a file
(R)ead database from a file
(Q)uit

Enter choice: i

Insert new record selected

Please enter employee's Last Name: Please enter employee's First Name:

It is skipping ahead of Last Name!

Do I need to add cout.flush() somewhere? When I add cout.flush() right under the last name section I get this error:

unixapps1:Lab1> make g++ -o LSL lab1.cpp Employee.cpp lab1.cpp: In function âEmployee createRecord()â: lab1.cpp:41: error: âstruct std::istreamâ has no member named âflushâ

My menu code in my is this:

160 int main() {
161
162 cout << "\n"
163 << "*********************************\n"
164 << "Remember: Search for TODO's in VI before submitting project!\n"
165 << "
*********************************\n\n";
166
167 char menu_choice;
168 string fileName;
169
170 // Create a database
171 LinkedSortedList database;
172
173 while (true) {
174 // Display menu
175 cout << "MENU\n"
176 << "(I)nsert new record\n"
177 << "(L)ast name search\n"
178 << "(S)ave database to a file\n"
179 << "(R)ead database from a file\n"
180 << "(Q)uit\n\n";
181
182 cout << "Enter choice: ";
183 cin >> menu_choice;
184 cout << endl;
185
186 switch (toupper(menu_choice)) {
187
188 case 'I':
189 cout << "Insert new record selected\n\n";
190 {database.insert(createRecord());}
191 break;
192
193 case 'L':
194 cout << "Last name search selected\n\n";
195 // TODO call function to search by last name
196 // TODO call search for Last Name
197 // TODO Print all matches found
198 {string searchVal = "";
199 database.find(searchVal);}
200 break;
201
202 case 'S':
203 cout << "Save database to a file selected\n\n";
204 // TODO call function to save database to file
205 // File I/O Save to file
206 cout << "Please enter a file name: " << endl;
207 cin >> fileName;
208 {char* file = (char*) fileName.c_str();
209 writeFile(file, database);}
210 break;
211
212 case 'R':
213 cout << "Read database from a file selected\n\n";
214 // TODO call function to read database from file
215 // File I/O Read file
216 cout << "Please enter a file name: " << endl;
217 cin >> fileName;
218 {char* file = (char*) fileName.c_str();
219 readFile(file, database);}
220 break;
221
222 case 'Q':
223 exit(EXIT_SUCCESS);
224
225 // default case:
226 default:
227 cout << "Invalid choice!\n\n"
228 << "##### Please try again #####\n\n";
229 break;
230
231 cin >> menu_choice;
232 }
233 }
234 return 0;
235 }
236

My createRecord() function is this:

29 // Create the record to be inserted into the employee database
30 Employee createRecord() {
31 // Temporary variables for getting input from user
32 string stringInput = "";
33 int intInput = 0;
34
35 Employee newEmployee;
36
37 cout << "Please enter employee's Last Name: " << endl;
38 getline(cin, stringInput);
39 newEmployee.setLastName(stringInput);
40
41 cout << "Please enter employee's First Name: " << endl;
42 getline(cin, stringInput);
43 newEmployee.setFirstName(stringInput);
44
45 cout << "Please enter employee's ID: ";
46 getline(cin, stringInput);
47 stringstream myStream(stringInput);
48 myStream >> intInput;
49 newEmployee.setId(intInput);
50
51 cout << "Please enter employee's Salary: ";
52 getline(cin, stringInput);
53 myStream >> intInput;
54 newEmployee.setSalary(intInput);
55
56 cout << "Please enter employee's Department: ";
57 getline(cin, stringInput);
58 newEmployee.setDepartment(stringInput);
59
60 cout << "Please enter employee's Phone Number: ";
61 getline(cin, stringInput);
62 newEmployee.setPhoneNumber(stringInput);
63
64 cout << "Please enter employee's Address: ";
65 getline(cin, stringInput);
66 newEmployee.setAddress(stringInput);
67
68 cout << "Please enter employee's Hire Date: ";
69 getline(cin, stringInput);
70 newEmployee.setHireDate(stringInput);

c++unixswitch-statementcoutcin

Answers

answered 6 years ago Thomas Matthews #1

Looks like there is still a newline or parts thereof in the input stream.

Try using std::istream::ignore() to eat the remaining characters in the input stream.

answered 6 years ago hinafu #2

Have you tried using cin.get() instead of cout.flush()?

comments powered by Disqus