【问题标题】:How do I fix my C++ case switch menu?如何修复我的 C++ 大小写切换菜单?
【发布时间】:2012-04-30 15:06:58
【问题描述】:

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

我的菜单是这样的:

菜单
(I)插入新记录
(L)姓氏搜索
(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;
}

这是我的createRecord() 函数:

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

【问题讨论】:

    标签: c++ unix switch-statement cout cin


    【解决方案1】:

    输入流中似乎仍有换行符或其部分。

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

    【讨论】:

    • 具体试试加cin.ignore(std::numeric_limits&lt;streamsize&gt;::max(), '\n');
    • @SethCarnegie:是的,只是提醒人们换行可以是两个字符:回车'\r'和换行;取决于平台或操作系统。 :-)
    • @ThomasMatthews 我什至认为 Windows 不再使用 \r\n
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多