gpt4 book ai didi

C++ 输入验证以避免破坏代码的能力

转载 作者:行者123 更新时间:2023-11-28 05:56:36 26 4
gpt4 key购买 nike

我写了一个模拟钓鱼游戏的程序。该程序在一个运行循环中,可以根据用户的意愿进行迭代。问题是输入的任何内容(1 或 0 除外)都会破坏我的代码。我几个小时以来一直在尝试不同的事情,我需要一些帮助!包含我的主要 cpp 文件代码。如果您也需要我的头文件进行审核,请告诉我。

有人要求我编辑代码。问题是输入时除整数以外的任何内容都会破坏我的代码。我不想要这个。我希望程序捕获输入并继续循环,直到用户输入正确的输入(1 或 0)或退出游戏。

Game game; // Game object game declared
cout<<"Welcome to Go Fish 2.0!"<<endl; // welcome message
cout<<"Would you like to play?"<<endl; // Prompts user to play
cout<<"1 to play, 0 to exit:"<<endl; // Provides user with input choices
cin>>choice; // user enters game play choice

if (choice == 1) // while user chooses to play game
play = true; // play boolean is set to true
else if (choice == 0) // while user chooses to end game
play = false; // play boolean is false
while ( ! cin>>choice) { // while user entry is not valid
// display correct inpout choices again
cin.clear();
cin.ignore (100, '\n');
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input

if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}


total=0; // variable total is initialized to 0
while (play == true) { // while play boolean is set to true; user wants to play game

total1 += game.playgame(total); // total1 variable keeps a running total of the game
//game.playgame(total) uses the game object to call the playgame function that passes the variable total
// when game.playgame(total) is called this funciton essentially mobilizes game play of die rolling and point accumulation
//each time this function is called (as many times as user wants to play game), the running total is incremented

cout<< "Do you want to play again? (0 for no, 1 for yes)"<<endl;// asks user if they want to play again
cin >> choice;// user enters choice

if (choice == 1) // if user enters 1
{
play = true; // play is assigned to true
}


else if (choice == 0) // if user enters 0
{
play = false; // play is assigned to false
}
/*
while ( ! cin>>choice) { // while user entry is not valid
// display correct inpout choices again
cin.clear();
cin.ignore (100, '\n');
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input

if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}


if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
cout<<"My Total game points are "<<total1<<endl; // displays user's total points from whole game
if (total1>100) // if game total greater than 100
cout<<"Awesome! You're a great fisher!"<<endl; // congratulate user
if (total1<100)// if game total is less than 100
cout<<"Good job, but play again so you can fish some more."<<endl;// chastise user
system("pause");
}

}
return 0;// end main function
}
}

最佳答案

您应该首先将用户的输入作为字符串/字符数组,然后尝试将其转换为 int(有多种方法可以做到这一点 - Google 是您的 friend ;))但是 atoi(ASCII 到 int ) 可能是最快/最简单的。如果失败,请要求用户输入有效的输入,重复直到得到 1 或 0,诸如此类。

关于C++ 输入验证以避免破坏代码的能力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34055375/

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