gpt4 book ai didi

c++ - 菜单选择器不接受有效输入

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

当我输入“1”(不带引号)时,它会将我带到错误菜单(无效输入)。

换句话说:当我运行程序时,在初始形状提示菜单或“无效输入”菜单中,如果我将其作为输入输入,我将无法继续选择“1”。

有人知道这里发生了什么吗?

#include <iostream>
#include <string>
#include <limits>

using std::cout;
using std::cin;
using std::string;
using std::endl;

int main()
{
string username;
cout<<"Hello.\nMy name is Pythagoras.\nI will be helping you build shapes today.\n\nWhat is your name?\n"<<endl;
cin>>username;
int shapeselect;
cout<<"Hello, "<<username<<".\nWhat shape would you like to build today?\n1)Rectangle/Square\n2)Triangle\n3)Random!\n(Please select a number.)\n"<<endl;
cin>>shapeselect;

while (shapeselect!=1||shapeselect!=2||shapeselect!=3)
{
cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout<<"Sorry, "<<username<<", your entry was invalid. Please select a valid number.\n";
cout<<"What shape would you like to build today, "<<username<<"?\n1)Rectangle/Square!\n2)Triangle!\n3)Random!"<<endl;
cin>>shapeselect;
}

if (shapeselect==1)
{
int width;
cout<<"\nPlease enter the desired WIDTH of the rectangle, between 4 and 10.\n(Please select a number.)\n";
cin>>width;
cout<<"\nYou have selected a width of "<<width<<"."<<endl;
int length;
cout<<"\nPlease enter the desired LENGTH of the rectangle, between 4 and 10.\n(Please select a number.)\n";
cin>>length;
cout<<"\nYou have selected a length of "<<length<<"."<<endl;
if (width==length)
{
cout<<"Please note that you have selected a width of "<<width<<" and a length of "<<width<<".\nNote that this shape will be a square.\n"<<endl;
}
}
return 0;
}

最佳答案

据我了解,当 shapeselect 不是 1、2、3 时,您需要执行 while 循环。

要解决此问题,请将 while 条件下的所有或门替换为与门。

原因:因为,当其中一个输入为真时,或门输出真。因此,如果 shapeselect 等于 1,则它不会等于 2 和 3,这使得 shapeshelect!=2shapeselect!=3 的计算结果为 true。无论您输入哪个数字,while 循环都会一直运行。

关于c++ - 菜单选择器不接受有效输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56929112/

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