【问题标题】:C++ for loop continues even after break statement inserted [closed]即使在插入 break 语句后,C++ for 循环仍在继续 [关闭]
【发布时间】:2022-11-27 10:41:45
【问题描述】:

目前正在制作一个记录系统,它将继续添加记录,直到我输入“99”。但是,即使我输入“99”,它仍会继续运行系统。


为了更好地说明这是我的代码的样子:

#include <cstdio>
#include <string>
#include <iostream>
using namespace std;

struct book {
    char authorName[100], titleName[100], pubName[100];
    float price;
    int stockNum;
}list;
int choice;

int main() {
    for (;;) {
        cout << "Enter Author Name   :";
        cin.ignore();
        cin.getline(list.authorName, 100);
        if (list.authorName == "99") {
            break;
        }
        cout << "Enter Title Name    :";
        cin.getline(list.titleName, 100);
        cout << "Enter Publisher Name:";
        cin.getline(list.pubName, 100);
        cout << "Enter Price in RM   :";
        cin.ignore();
        cin >> list.price;
        cout << "Enter Stock Position :";
        cin >> list.stockNum;
        cout << "Record Saved!" << endl;
    }
    return 0;
}

我尝试将其更改为其他循环(while 循环)。 我尝试将 cin.getline 更改为 cin。 即使尝试将输入更改为 char (NO,STOP) 也不会影响循环

【问题讨论】:

  • 在调试器中运行您的代码并查看变量。
  • struct book { ... } list; 在语义上没有意义。一本书不是清单。对于编译器来说,它在语法上是有意义的,你现在有一本名为 list 的书。
  • 另请注意,您会收到编译器警告:comparison with string literal results in unspecified behavior。不要在 C++ 中使用 char[],只能使用 std::string
  • 包括cstring,并将您的条件替换为if (strcmp(book.authorName, "99") == 0)
  • @RohanBari 或者只使用已经包含的std::string并继续使用operator==

标签: c++ for-loop break dev-c++


【解决方案1】:

问题是

  1. 您使用字符数组而不是指针
  2. 你的第一个cin.ignore();是一个输入函数。它等待输入,然后消费“99”中的第一个“9”。
  3. 您的比较无效。您将苹果与馅饼进行比较。 if 语句永远不会为真。

    此外,您应该始终检查您的输入是否有效。

    然后,您需要了解格式化输入和未格式化输入之间的区别。

    从格式化输入转换为未格式化输入后(此处在读取“库存编号”后,您可以使用 std::ws 吃掉尾随空格。尽量避免使用 ignore 函数。

    您的代码可能如下所示:

    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std::string_literals;
    
    struct book {
        std::string authorName{};
        std::string titleName{};
        std::string pubName{};
        float price;
        int stockNum;
    };
    
    int main() {
        book list{};
        int choice{};
    
        for (;std::cin;) {
            std::cout << "Enter Author Name   :";
            std::getline(std::cin >> std::ws, list.authorName);
            if (list.authorName == "99"s) {
                break;
            }
            std::cout << "Enter Title Name    :";
            std::getline(std::cin, list.titleName);
    
            std::cout << "Enter Publisher Name:";
            std::getline(std::cin, list.pubName);
    
            std::cout << "Enter Price in RM   :";
            if (!(std::cin >> list.price)) {
                std::cout << "
    Invalid input
    ";
                break;
            }
    
            std::cout << "Enter Stock Position :";
            if (!(std::cin >> list.stockNum)) {
                std::cout << "
    Invalid input
    ";
                break;
            }
            std::cout << "Record Saved!" << '
    ';
        }
    }
    
    

【讨论】:

  • 关于ignore 的注意事项:不要把它放在可能需要它的阅读之前,您刚刚看到当您发现不需要它的情况时会发生什么,将ignore 放在留下不需要的字符的阅读之后在流中。
猜你喜欢
  • 1970-01-01
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2021-01-31
  • 2016-12-22
  • 1970-01-01
相关资源
最近更新 更多