- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请注意,我是 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 choice
和int 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/
我是 JavaFx 新手,正在尝试编写我的刽子手游戏。我现在必须检查保存我的词典的文件是否在那里。如果没有,请用户输入其位置。我试图在弹出窗口中执行此操作,要求用户输入位置并点击按钮,该按钮反过来将导
我卡住了。我试图用 SQLite 表的字段填充 ListView 。我对 Android 编程很陌生。关于我为什么会收到此错误的任何建议: 10-24 10:24:31.154: ERROR/Andr
我正在编写一个 java 程序,它接受用户输入的数字 x,对从 1 到 x(包括 x)的所有可被 3 整除的数字求和,并显示总和。它编译没有错误但是当我执行程序时,它陷入循环并继续执行直到我关闭命令提
JS 和 Web 编程新手。 我有一个图像在 Canvas 上沿水平轴移动。当图像到达某个点时,我希望它停止、缩放然后旋转。我使用的方法会导致无限循环,我不明白为什么。 这是被调用的方法 while
我制作了一个使用表单、php 和 javascript 进行登录和注销的页面。 但唯一的问题是,当您提交表单并且数据发生更改时,直到页面重新加载后才会更新。 注意lo
我在查询时陷入困境。我想根据“时间和点赞数”查询帖子,但不知道如何在 firebase 上查询。示例: Query mquery =FireBaseDatabase() getReference().
这个问题已经有答案了: Why does my ArrayList contain N copies of the last item added to the list? (5 个回答) 已关闭 6
所以,我有一个项目,任务是这样表述的: 您需要创建一个名为 Candidate 的类,它存储参加选举的人的姓氏以及他们获得的票数。您还应该有访问每个字段的方法。此时,这些字段不需要修改器,因为它们不应
我正在解决 Java 编程类(class)中的作业问题,但我陷入了困境。我会预先告诉你,我绝不是专家,并且对 Java 或一般编程了解不多。 我遇到的问题实际上不是我的作业问题,而是书中的一个例子,我
我正在用java编写一个客户端程序。服务器是用C++编写的。当我从以下位置发送消息时客户端到服务器就可以了。但是在接收过程中我的客户端线程陷入了这个调用 in = new BufferedReader
我正在参加 Edx 上的在线类(class) cs50,我有一个作业,其中我必须创建一个程序,用户在其中输入关键字(然后用于加密)和需要在 Vigenere 中加密的字符串。密码。 Vigenere
我遇到了陷入 getIntLimited 函数的问题。在调整数量中,我需要它检查用户是否输入了正确的数字,而不是多于或少于所需的数字,而不是字母。我没有对“库存”选项这样做,只在“检查”选项中这样做。
我更喜欢创建一个 Dictionary 对象并向其中添加 3 个单词。我的程序没有编译错误,但在第二个 for 循环中出现运行时错误,问题出在 addNewWord 函数中吗?我需要传递一个指向 Di
该程序的要点是获取 2 个文件,一个是字典,另一个是文本,创建一个输出文件,并将文本文件中的所有单词放入其中,但将文本文件中不存在的单词大写字典。 当我运行程序时,它不断要求输入,所以看起来我陷入了
我正在为我的 C++ 类(class)做作业。在这个作业中,我需要编写一个程序,从 cin 中读取数字,然后对它们求和,当使用 while 循环输入 0 时停止。 我已经编写了代码并获得了我需要的结果
这是我到目前为止所得到的- $awards_sql_1 = mysql_query('SELECT * FROM categories WHERE section_id = 1') or die(my
我想弄清楚某个步骤何时发生。所以我编写了一个名为 countSteps 的方法来执行此操作。它的问题是我陷入其中,因为在 while 循环中我不断获取新数据,而且我认为它永远不会返回到 onSenso
我有点卡在一个问题上了。我有一个 Spring + Hibernate 应用程序,最近几天表现得很奇怪。 通常,即使在 Debug模式下,它也会引导大约 15 秒。 几天后,在没有显示任何重大错误或问
我是编程新手,我想知道是否有人可以帮助我解决这个问题?它似乎处于连续循环中,我一直在更改它并尝试不同的方法近一个小时,将不胜感激。 #include "stdafx.h" #include
我正在用 Python 创建一个聊天服务器,并且对这门语言的了解还很浅。我现在有 1 个问题,我想在进一步解决之前解决,但我似乎找不到解决问题的方法。 这是一个持续的 while 循环.. 下面的代码
我是一名优秀的程序员,十分优秀!