gpt4 book ai didi

C++程序陷入无限循环

转载 作者:行者123 更新时间:2023-12-02 01:45:23 24 4
gpt4 key购买 nike

请注意,我是 C++ 的初学者。我正在尝试为 ATM 编写一个简单的程序,并且必须考虑所有错误。用户可能只使用整数进行输入,因此我需要检查输入值是否确实是整数,并且我的程序(这个已缩短)在大多数情况下都有效。

当我在选择操作时尝试输入字符串值而不是整数时,就会出现问题。它适用于无效值整数,但对于字符串,它会创建一个无限循环,直到它最终停止(除非我添加 system("cls"),然后它甚至不会停止),当它应该输出时与无效整数的结果相同:

Invalid choice of operation.
Please select an operation:

1 - Balance inquiry
7 - Return card

Enter your choice and press return:

这是我的代码:

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


bool isNumber(string s) //function to determine if input value is int
{
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;
return true;
}


int ReturnCard() //function to determine whether to continue running or end program
{
string rtrn;

cout << "\nDo you wish to continue? \n1 - Yes \n2 - No, return card" << endl;
cin >> rtrn;
if (rtrn == "1" and isNumber(rtrn)) { return false; }
else if (rtrn == "2" and isNumber(rtrn)) { return true; }
else {cout << "Invalid choice." << endl; ReturnCard(); };
return 0;
}


int menu() //function for operation choice and execution
{
int choice;
do
{
cout << "\nPlease select an operation:\n" << endl
<< " 1 - Balance inquiry\n"
<< " 7 - Return card\n"
<< "\nEnter your choice and press return: ";
int balance = 512;
cin >> choice;
if (choice == 1 and isNumber(to_string(choice))) { cout << "Your balance is $" << balance; "\n\n"; }
else if (choice == 7 and isNumber(to_string(choice))) { cout << "Please wait...\nHave a good day." << endl; return 0; }
else { cout << "Invalid choice of operation."; menu(); }
} while (ReturnCard()==false);
cout << "Please wait...\nHave a good day." << endl;
return 0;
}

int main()
{
string choice;
cout << "Insert debit card to get started." << endl;
menu();
return 0;
}

我已经尝试了我所知道的所有可能的解决方案,但似乎没有任何效果。

***还有一个不同的错误,那就是当我到达“你想继续吗?”时出现的错误。部分并输入任何无效值,并在再次询问后跟随 2 (这应该结束程序),它输出 1 的结果(继续运行 - 菜单等)。我已经给我的老师发了电子邮件询问此事,这不是我的主要问题,但我将不胜感激。

谢谢!

最佳答案

您的代码中有一些内容混淆了。始终尝试在打开最大警告的情况下编译代码,例如,对于 GCC 至少添加 -Wall旗帜。然后你的编译器会警告你所犯的一些错误。

首先,你似乎很困惑string choiceint choice 。不同范围内的两个不同变量。 string一个是未使用并且完全多余。您可以删除它,什么都不会改变。

在菜单中,你说 cin >> choice; ,其中choice类型为int 。流运算符>>工作原理如下:它将尝试读取尽可能多的字符,以使字符与请求的类型相匹配。所以这将读取整数。

然后您将有效的 int 转换为转换为字符串并调用 isNumber() - 将始终返回 true。

因此,如果您想读取任何文本行并处理它,您可以使用 getline() :

string inp;
std::getline(std::cin, inp);
if (!isNumber(inp)) {
std::cout << "ERROR\n";
return 1;
}

int choice = std::stoi(inp); // May throw an exception if invalid range

参见stoi

您的isNumber()实现可能如下所示:

#include <algorithm>

bool is_number(const string &inp) {
return std::all_of(inp.cbegin(), inp.cend(),
[](unsigned char c){ return std::isdigit(c); });
}

如果您像我一样喜欢这种功能风格;)

编辑:

顺便说一句,编译器警告的另一个错误:cout << "Your balance is $" << balance; "\n\n"; - 换行符由 ; 分隔,所以这是一个新的语句,并且没有任何作用。您可能想要 <<改为运算符。

递归调用错误:

{ cout << "Invalid choice of operation."; menu(); }ReturnCard() 相同,函数调用自身(递归)。这根本不是你想要的!这将重新启动该函数,但是一旦该调用结束,您将继续该调用发生的地方。menu() 你想要什么是重新开始循环。您可以使用 continue 来做到这一点关键词。您想要 ReturnCard() 同样的。但你需要一个循环。现在,我读了该代码,您甚至不需要将输入转换为整数。你所要做的就是比较它。所以你可以简单地这样做:

string inp;
std::getline(std::cin, inp);
if (inp == "1" || inp == "2") {
// good
} else {
// Invalid
}

除非这是您任务的一部分。

关于C++程序陷入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70984611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com