C++ 如何修复我的C++;箱子开关菜单?

C++ 如何修复我的C++;箱子开关菜单?,c++,unix,switch-statement,cout,cin,C++,Unix,Switch Statement,Cout,Cin,我的学校项目有问题。我正在学校服务器上使用UNIX中的VIM编写它。当我在项目中插入记录时,姓氏将被跳过。名字和姓氏同时被提示,而不等待姓氏接收输入。见下文: 我的菜单是这样做的: 菜单 (一) n插入新记录 (五十) ast名称搜索 (S) 将数据库保存到文件 (R) 从文件读取数据库 (Q) 尤特 输入选项:i 插入选定的新记录 请输入员工的姓氏: 请输入员工的名字: 它在姓氏之前跳过了 我需要在某处添加cout.flush()吗?当我在姓氏部分下面添加cout.flush()时,我得到以下

我的学校项目有问题。我正在学校服务器上使用UNIX中的VIM编写它。当我在项目中插入记录时,姓氏将被跳过。名字和姓氏同时被提示,而不等待姓氏接收输入。见下文:

我的菜单是这样做的:

菜单
(一) n插入新记录
(五十) ast名称搜索
(S) 将数据库保存到文件
(R) 从文件读取数据库
(Q) 尤特

输入选项:i

插入选定的新记录

请输入员工的姓氏: 请输入员工的名字:

它在姓氏之前跳过了

我需要在某处添加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â
我的中的菜单代码如下:

int main() {

  cout << "\n"
       << "************************************************************\n"
       << "Remember: Search for TODO's in VI before submitting project!\n"
       << "************************************************************\n\n";

  char menu_choice;
  string fileName;

  // Create a database
  LinkedSortedList<Employee> database;

  while (true) {
    // Display menu
    cout << "MENU\n"
     << "(I)nsert new record\n"
     << "(L)ast name search\n"
     << "(S)ave database to a file\n"
     << "(R)ead database from a file\n"
     << "(Q)uit\n\n";

    cout << "Enter choice: ";
    cin >> menu_choice;
    cout << endl;

    switch (toupper(menu_choice)) {

    case 'I':
      cout << "Insert new record selected\n\n";
      {database.insert(createRecord());}
      break;

    case 'L':
      cout << "Last name search selected\n\n";
      // TODO call function to search by last name
      // TODO call search for Last Name
      // TODO Print all matches found
      {string searchVal = "";
    database.find(searchVal);}
      break;

    case 'S':
      cout << "Save database to a file selected\n\n";
      // TODO call function to save database to file
      // File I/O Save to file
      cout << "Please enter a file name: " << endl;
      cin >> fileName;
      {char* file = (char*) fileName.c_str();
    writeFile(file, database);}
      break;

    case 'R':
      cout << "Read database from a file selected\n\n";
      // TODOOA call function to read database from file
      // File I/O Read file
      cout << "Please enter a file name: " << endl;
      cin >> fileName;
      {char* file = (char*) fileName.c_str();
    readFile(file, database);}
      break;

    case 'Q':
      exit(EXIT_SUCCESS);

      // default case:
    default:
      cout << "Invalid choice!\n\n"
       << "##### Please try again #####\n\n";
      break;

      cin >> menu_choice;
    }
  }
  return 0;
}

看起来输入流中仍然有一个换行符或其部分


尝试使用
std::istream::ignore()
来吃掉输入流中剩余的字符。

您是否尝试使用
cin.get()
而不是
cout.flush()

具体来说,尝试添加
cin.ignore(std::numeric_limits::max(),'\n')
@SethCarnegie:是的,只是提醒人们换行符可以是两个字符:回车“\r”和换行符;取决于平台或操作系统。:-)@ThomasMatthews我甚至认为windows不再使用
\r\n
// Create the record to be inserted into the employee database
Employee createRecord() {
  // Temporary variables for getting input from user
  string stringInput = "";
  int intInput = 0;

  Employee newEmployee;

  cout << "Please enter employee's Last Name: " << endl;
  getline(cin, stringInput);
  newEmployee.setLastName(stringInput);

  cout << "Please enter employee's First Name: " << endl;
  getline(cin, stringInput);
  newEmployee.setFirstName(stringInput);

  cout << "Please enter employee's ID: ";
  getline(cin, stringInput);
  stringstream myStream(stringInput);
  myStream >> intInput;
  newEmployee.setId(intInput);

  cout << "Please enter employee's Salary: ";
  getline(cin, stringInput);
  myStream >> intInput;
  newEmployee.setSalary(intInput);

  cout << "Please enter employee's Department: ";
  getline(cin, stringInput);
  newEmployee.setDepartment(stringInput);

  cout << "Please enter employee's Phone Number: ";
  getline(cin, stringInput);
  newEmployee.setPhoneNumber(stringInput);

  cout << "Please enter employee's Address: ";
  getline(cin, stringInput);
  newEmployee.setAddress(stringInput);

  cout << "Please enter employee's Hire Date: ";
  getline(cin, stringInput);
  newEmployee.setHireDate(stringInput);
}