Cin无法产生所需的结果

时间:2019-04-23 16:02:35

标签: c++

我正在尝试建立一个简单的航班预订系统程序,但是我运行该程序时,我的cin代码的第二位不要求输入。不同于最初的cin需要最初输入您的名字。该程序只是运行并返回0。我是c ++的初学者,我知道这是一个简单的修复,所以请理解。谢谢您的指导,将不胜感激。

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    int name;
    int Seatnumber;
    int optionnumber = 1-5 ;
    std::string out_string;
    std::stringstream ss;
    ss << optionnumber;
    out_string = ss.str();

    cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
    cout << "Enter full name : " << endl;
    cin >> name ; "\n";

    cout << "\n" "The Available travel times for flights are:" << endl;
    cout << "         Depart                Arrive" << endl;
    cout << "1.       7.00                  9.30"  << endl;
    cout << "2.       9.00                  11.30" << endl;
    cout << "3.       11.00                 13.30" << endl;
    cout << "4.       13.00                 15.30" << endl;
    cout << "5.       15.00                 17.30" << endl;
    cout << "Choose the time by entering the option number from the displayed list : " << endl;

    cin >> optionnumber ;

    if (optionnumber == 1-5){
        cout << "\n" "The available seats for are as follows " << endl;
    }
    else
        cout << "Incorrect option! Please Choose from 1-5 " << endl;

        cout << "First Class(1920)" << endl;
        cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
        cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
        cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
        cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
        cout << "| Economy Class(1600)" << endl;
        cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
        cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
        cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
        cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
        cout << "|I1||I2|" << endl;
        cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
        cin >> Seatnumber;
}
  1. 提示用户输入其姓名。
  2. 然后显示一个菜单,其中显示了航班的可用时间。 用户可以选择首选出发时间(选项1-5) 所选的选项应验证为1-5
  3. 如果用户输入了正确的时间,则应向下一个用户显示该特定飞行时间的座位安排,以便用户选择座位。

1 个答案:

答案 0 :(得分:1)

警告

int optionnumber = 1-5 ;

确实

int optionnumber = -4 ;

if (optionnumber == 1-5){

确实

if (optionnumber == -4){

但是您想要if ((optionnumber >= 1) && (optionnumber <= 5))

  

如果用户输入了正确的时间,则应向下一个用户显示该特定飞行时间的座位安排,以便用户选择座位。

否,无论您进行上述测试的结果如何,继续写"First Class(1920)"等,即使选择无效也是如此


  cin >> name ; "\n";

您对"\n"有什么期望?

我建议您检查读取是否成功,目前,如果用户未输入整数,您将不知道

但是您确定名称必须是整数吗?可能是s string


out_string未使用,可以将其删除


可见的 Seatnumber 不是 int 而是 string (A1 ...)


您可能想要循环直到输入有效时间,还解决了其他问题的解决方案:

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string name;
  string Seatnumber;
  int optionnumber;

  cout << "Welcome to the COS1511 Flight Booking System" "\n" << endl;
  cout << "Enter full name : " << endl;

  if (!(cin >> name))
    // EOF (input from a file)
    return -1;

  cout << "\n" "The Available travel times for flights are:" << endl;
  cout << "         Depart                Arrive" << endl;
  cout << "1.       7.00                  9.30"  << endl;
  cout << "2.       9.00                  11.30" << endl;
  cout << "3.       11.00                 13.30" << endl;
  cout << "4.       13.00                 15.30" << endl;
  cout << "5.       15.00                 17.30" << endl;
  cout << "Choose the time by entering the option number from the displayed list : " << endl;

  for (;;) {
    if (!(cin >> optionnumber)) {
      // not an int
      cin.clear(); // clear error

      string s;

      // flush invalid input
      if (!(cin >> s)) 
        // EOF (input from a file)
        return -1;
    }
    else if ((optionnumber >= 1) && (optionnumber <= 5))
      // valid choice
      break;

    cout << "Incorrect option! Please Choose from 1-5 " << endl;
  }

  cout << "\n" "The available seats for are as follows " << endl;
  cout << "First Class(1920)" << endl;
  cout << "|A1||A2||A3|----|A4||A5||A6|" << endl;
  cout << "|B1||B2||B3|----|B4||B5||B6|" << endl;
  cout << "|C1||C2||C3|----|C4||C5||C6|" << endl;
  cout << "|D1||D2||D3|----|D4||D5||D6|" << endl;
  cout << "| Economy Class(1600)" << endl;
  cout << "|E1||E2||E3|----|E4||E5||E6|" << endl;
  cout << "|F1||F2||F3|----|F4||F5||F6|" << endl;
  cout << "|G1||G2||G3|----|G4||G5||G6|" << endl;
  cout << "|H1||H2||H3|----|H4||H5||H6|" << endl;
  cout << "|I1||I2|" << endl;

  cout << "Please Key in a seat number to choose a seat(eg: A2)" << endl;
  cin >> Seatnumber;

  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ g++ -pedantic -Wextra -Wall cc.cc
pi@raspberrypi:/tmp $ ./a.out
Welcome to the COS1511 Flight Booking System

Enter full name : 
bruno

The Available travel times for flights are:
         Depart                Arrive
1.       7.00                  9.30
2.       9.00                  11.30
3.       11.00                 13.30
4.       13.00                 15.30
5.       15.00                 17.30
Choose the time by entering the option number from the displayed list : 
aze
Incorrect option! Please Choose from 1-5 
7
Incorrect option! Please Choose from 1-5 
2

The available seats for are as follows 
First Class(1920)
|A1||A2||A3|----|A4||A5||A6|
|B1||B2||B3|----|B4||B5||B6|
|C1||C2||C3|----|C4||C5||C6|
|D1||D2||D3|----|D4||D5||D6|
| Economy Class(1600)
|E1||E2||E3|----|E4||E5||E6|
|F1||F2||F3|----|F4||F5||F6|
|G1||G2||G3|----|G4||G5||G6|
|H1||H2||H3|----|H4||H5||H6|
|I1||I2|
Please Key in a seat number to choose a seat(eg: A2)
qsd
pi@raspberrypi:/tmp $ 

请注意,使用cin >> name输入名称不允许其包含多个用空格分隔的名称,以允许使用组合名称​​ getline