如果条件为假时运行语句

问题描述 投票:0回答:1

基本上,如果用户输入“否”,则 if 语句应该中断 while 循环,但是无论我输入什么,while 循环都会中断。我不确定问题实际上是 if 语句还是 while 循环。

我认为他们读取响应的方式可能有问题,所以我添加了一行来打印用户响应,但这部分似乎工作正常

int main() {
    HashTable hashTable;

    while (true) {
        cout << "Enter customer name: \n";
        string name;
        getline(cin, name);

        cout << "Enter a number: \n";
        int orderNumber;
        cin >> orderNumber;

        hashTable.addCustomer(orderNumber, name);

        cout << "Would you like to keep going(yes/no): ";
        string response;
        cin >> response;
        cout << response;

        if (response == " no");{
            break;
            hashTable.printFourCustomers();
        }
  }

  return 0;
}
c++ if-statement
1个回答
0
投票

首先,“不”前面有一个空格是有原因的吗?这可能是你的问题。

其次,您可能想要一个不区分大小写的匹配,因此它将接受 no 或 NO 或 No。

© www.soinside.com 2019 - 2024. All rights reserved.