2012-04-30 46 views
-1

我的學校項目出現問題。我在學校服務器上的UNIX中使用VIM編寫它。當我在我的項目中插入記錄時,姓氏會被跳過。名字和姓氏被同時提示,而不用等待姓氏接收它的輸入。請看下圖:如何修復我的C++ case切換菜單?

我的菜單是這樣做的:

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:

它跳過姓的未來!

我需要在某處添加cout.flush()嗎?當我添加cout.flush()下的姓氏節對的,我得到這個錯誤:

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â

我在我的就是菜單這段代碼:

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

我createRecord()函數是這樣的:

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);

回答

4

看起來輸入流中仍然有換行符或其部分。

嘗試使用std::istream::ignore()來吃掉輸入流中的其餘字符。

+0

換行符的部分? :) +1 –

+1

特別是,嘗試添加'cin.ignore(std :: numeric_limits :: max(),'\ n');' –

+1

@SethCarnegie:是的,只是爲了提醒人們一個換行符可以是兩個字符:回車'\ r'和換行;取決於平臺或操作系統。 :-) –

1

您是否嘗試過使用cin.get()而不是cout.flush()