gpt4 book ai didi

c++ - 如何验证整数输入?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:48:51 25 4
gpt4 key购买 nike

所以我只是因为无聊而为我哥哥玩的游戏制作了这个小计算器程序。上学期我在大学里选修了 C++ 类(class)(现在选修了 Java 类(class),我觉得这更让人困惑),我发现制作这些小程序很有趣。好吧,像往常一样,我越来越得意忘形,一定是一个完美主义者,这真的让我很困扰。

现在在这个游戏中,就像在现实生活中一样,数字用逗号分隔,显然更容易阅读。因此,这主要是我兄弟将数字输入计算器的方式。现在我可以告诉他在输入数字时不要输入任何逗号,并在提示中输入不应该有任何逗号的提示,但即使那样你也不能确定。即便如此,最好不要每次输入非数字的内容时代码都乱七八糟。

到目前为止,我所拥有的都非常好。如果你只输入字母,它会再次提示用户,如果你只在数字之后输入字母(不是在中间,我发现它搞砸了)那么它会忽略这些字母并正常工作。如果你输入逗号,它总是返回相同的东西(0 和 5),尽管这可能是我输入的内容。我不知道逗号是作为一个截止点还是什么。这是获取整数的代码:

#include <iostream> 
using namespace std;

int main() {

int numberofbones, amountofxp, comparableDrag, comparableBaby, comparableDragNoAltar, comparableBigNoAltar, comparableBabyNoAltar;
double comparableBig;
char Gildedaltar, BoneSelection, replay;
bool bFail;

do{
cout << "How many bones do you have?: ";
cin >> numberofbones;
bFail = cin.fail();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while (bFail == true);

do{
cout << "How much XP do you need?: ";
cin >> amountofxp;
bFail = cin.fail();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while (bFail == true);

cout << "Are you using a Gilded Altar? (Y/N) ";
prompt:
cin >> Gildedaltar;

comparableDrag = amountofxp / 252;
comparableBig = amountofxp / 52.5;
comparableBaby = amountofxp / 105;
comparableDragNoAltar = amountofxp / 72;
comparableBigNoAltar = amountofxp / 15;
comparableBabyNoAltar = amountofxp / 30;

if (Gildedaltar == 'y' || Gildedaltar == 'Y') {
system("cls");
cout << "What bones will you be using: " << endl;
cout << "a) Dragon Bones " << endl;
cout << "b) Big Bones " << endl;
cout << "c) BabyDragon Bones " << endl;
cin >> BoneSelection;

if (BoneSelection == 'a' || BoneSelection == 'A') {
cout << endl << "With Dragon Bones you need " << comparableDrag << " Bones" << endl;

if (comparableDrag < numberofbones) {
cout << "You have enough bones with " << numberofbones - comparableDrag << " left over, get to sacrafising!" << endl;
}
else {
cout << "You will need " << comparableDrag - numberofbones << " more bones" << endl;
}
}
if (BoneSelection == 'b' || BoneSelection == 'B') {
cout << endl << "With Big Bones you need a total of " << comparableBig << " Bones" << endl;

if (comparableBig < numberofbones) {
cout << "You have enough bones with " << numberofbones - comparableBig << " Left over, get to sacrafising!" << endl;
}
else {
cout << "You will need " << comparableBig - numberofbones << " more bones" << endl;
}
}
if (BoneSelection == 'c' || BoneSelection == 'C') {
cout << endl << "With BabyDragon Bones you will need " << comparableBaby << " Bones" << endl;

if (comparableBaby < numberofbones) {
cout << "You have enough bones with " << numberofbones - comparableBaby << " left over, get to sacrafising!" << endl;
}
else {
cout << "You will need " << comparableBaby - numberofbones << " more bones" << endl;
}
}
}
else if (Gildedaltar == 'n' || Gildedaltar == 'N') {
system("cls");
cout << "What bones will you be using: " << endl;
cout << "a) Dragon Bones " << endl;
cout << "b) Big Bones " << endl;
cout << "c) BabyDragon Bones " << endl;
cin >> BoneSelection;

if (BoneSelection == 'a' || BoneSelection == 'A') {
cout << endl << "With Dragon Bones, you will need " << comparableDragNoAltar << " Bones " << endl;

if (comparableDragNoAltar < numberofbones) {
cout << "You have enough bones with " << numberofbones - comparableDragNoAltar << " left over, get to sacrafising!" << endl;
}
else {
cout << "You will neeed " << comparableDragNoAltar - numberofbones << " More bones" << endl;
}
}
if (BoneSelection == 'b' || BoneSelection == 'B') {
cout << endl << "With Big Bones, you will need " << comparableBigNoAltar << " Bones" << endl;

if (comparableBigNoAltar < numberofbones) {
cout << "You have enough bones with " << numberofbones - comparableBigNoAltar << " Left over, get to sacrafising!" << endl;
}
else {
cout << "You will need " << comparableBigNoAltar - numberofbones << " More bones" << endl;
}
}
if (BoneSelection == 'c' || BoneSelection == 'c') {
cout << endl << "With BabyDragon Bones, you will need " << comparableBabyNoAltar << " Bones" << endl;

if (comparableBabyNoAltar < numberofbones) {
cout << "You have enough bones with " << numberofbones - comparableBabyNoAltar << " Left over, get to sacrafising!" << endl;
}
else {
cout << "You will need " << comparableBigNoAltar - numberofbones << " More Bones" << endl;

}
}
}
else {
goto prompt;
}

您可以忽略大部分代码,我知道它可能看起来很草率,并且可能有更好的方法来处理这里的大部分内容。我只是出于无聊而决定这样做,并认为这对我来说可能是一个很好的教训。如果你知道有什么方法可以帮助我,我会很高兴听到解决方案,如果有一种方法可以让程序完全忽略逗号,那会更好,但可惜我不相信有办法那个。

附言请不要对我太技术化,我只上过一门关于这方面的类(class):)在此先感谢您的帮助。

最佳答案

在尝试从中获取 int 之前,您只需要从输入字符串中删除逗号。更好的办法是从输入中删除任何非数字字符,您可以使用 isdigit() 来完成。 .然后调用atoi()在上面。

string& remove_nondigit(string& s) {
s.erase(remove_if(s.begin(), s.end(), [](const char& c) {
return !isdigit(c);
}), s.end());
return s;
}

yourF() {
string sBones;
cin >> sBones;
cout >> remove_nondigit(sBones);
// you'll want to use atoi() on sBones
}

关于c++ - 如何验证整数输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33049621/

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