C++ 为什么我的while循环在c++;不能正常运行吗?
我只是一个编程新手,我试图编写一个while循环,只要输入(C++ 为什么我的while循环在c++;不能正常运行吗?,c++,while-loop,C++,While Loop,我只是一个编程新手,我试图编写一个while循环,只要输入(num)不是以零结尾的整数,它就可以运行。当我输入一个以零结尾的数字时,程序会正确地运行循环,但当我输入一些无意义的东西,例如rofl时,程序只打印,输入无效。并且不会重复循环。我试着寻找解决办法,但一个小时后我还是被卡住了。有人能帮我吗?太多了 void rev_sum() { int num; int a = 1; while (a < 2) { cout << "Pleas
num
)不是以零结尾的整数,它就可以运行。当我输入一个以零结尾的数字时,程序会正确地运行循环,但当我输入一些无意义的东西,例如rofl
时,程序只打印,输入无效。
并且不会重复循环。我试着寻找解决办法,但一个小时后我还是被卡住了。有人能帮我吗?太多了
void rev_sum() {
int num;
int a = 1;
while (a < 2) {
cout << "Please input a natural number without zero at the end:\n";
cin >> num;
if (!cin) {
cout << "The input is not valid.\n";
cin.clear();
cin.ignore(INT_MAX);
}
if (num % 10 == 0) {
cout << "The number cannot have zero at the end\n";
} else {
cout << "gj\n";
break;
}
}
}
void rev_sum(){
int-num;
INTA=1;
while(a<2){
cout>num;
如果(!cin){
coutcin.ignore(数值限制::max())
cin.ignore(数值限制::max())
可能是
else if (num % 10 == 0) {
否则,另一个else案例可能会被执行
可能是
else if (num % 10 == 0) {
否则,另一个else案例可能会被执行如果输入有效,只检查数字是否以0结尾是否更有意义
void rev_sum() {
int num;
int a = 1;
while (a < 2) {
cout << "Please input a natural number without zero at the end:\n";
cin >> num;
if (!cin) {
cout << "The input is not valid.\n";
cin.clear();
cin.ignore(INT_MAX);
} else {
if (num % 10 == 0) {
cout << "The number cannot have zero at the end\n";
} else {
cout << "gj\n";
break;
}
}
}
}
void rev_sum(){
int-num;
INTA=1;
while(a<2){
cout>num;
如果(!cin){
cout如果输入有效,只检查数字是否以0结尾不是更有意义吗
void rev_sum() {
int num;
int a = 1;
while (a < 2) {
cout << "Please input a natural number without zero at the end:\n";
cin >> num;
if (!cin) {
cout << "The input is not valid.\n";
cin.clear();
cin.ignore(INT_MAX);
} else {
if (num % 10 == 0) {
cout << "The number cannot have zero at the end\n";
} else {
cout << "gj\n";
break;
}
}
}
}
void rev_sum(){
int-num;
INTA=1;
while(a<2){
cout>num;
如果(!cin){
不能试着更换
cin.ignore(INT_MAX);
与
到
您的最终代码应该如下所示:
void rev_sum() {
int num;
int a = 1;
while (a < 2) {
cout << "Please input a natural number without zero at the end:\n";
cin >> num;
if (!cin) {
cout << "The input is not valid.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else if (num % 10 == 0) {
cout << "The number cannot have zero at the end\n";
} else {
cout << "gj\n";
break;
}
}
}
void rev_sum(){
int-num;
INTA=1;
while(a<2){
cout>num;
如果(!cin){
不能试着更换
cin.ignore(INT_MAX);
与
到
您的最终代码应该如下所示:
void rev_sum() {
int num;
int a = 1;
while (a < 2) {
cout << "Please input a natural number without zero at the end:\n";
cin >> num;
if (!cin) {
cout << "The input is not valid.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else if (num % 10 == 0) {
cout << "The number cannot have zero at the end\n";
} else {
cout << "gj\n";
break;
}
}
}
void rev_sum(){
int-num;
INTA=1;
while(a<2){
cout>num;
如果(!cin){
cout在循环开始时设置a=1
,然后永远不要更改a
,这意味着离开while循环的唯一方法是点击break语句。如果循环没有循环,那么它一定卡在某个地方。我不熟悉if(!cin)
和cin.忽略(…)
因此,这些都是需要检查(或更改)的首要可疑对象。语句cin>>num;
无论键入什么,都会完成,因此您可以在输入“rofl”时检查“num”等于什么。然后在失败后,您仍然使用num
,因此您正在处理此意外输入。您可以在cin之后添加continue;
。忽略(…)
,跳回while循环的顶部并再次提问。您还可以在while循环之后打印一些内容,以便知道何时退出
尽管如此,我永远不会相信用户输入可接受的信息,也不会相信cin为我处理信息。就我个人而言,我会使用cin.getline(buffer,buffer_size)将cin作为字符串读取;然后,如果用户填充了缓冲区或给了我非整数的内容,我会向用户投诉(您可以使用以下函数进行检查)。然后你可以准确地回吐他们给你的东西,你可以明确你的投诉。你在循环开始时设置a=1
,然后永远不要更改a
,这意味着离开while循环的唯一方法是如果你点击break语句。如果你的循环没有循环,那么它一定卡在某个地方。我很抱歉使用语句if(!cin)
和cin。忽略(…)
,因此这些都是需要检查(或更改)的首要可疑对象。语句cin>>num;
无论键入什么都会完成,以便您可以在输入“rofl”时检查“num”等于什么。然后在失败后,您仍然使用num
,因此您正在处理此意外输入。您可以在cin之后添加continue;
。忽略(…)
,跳回while循环的顶部并再次提问。您还可以在while循环之后打印一些内容,以便知道何时退出
尽管如此,我永远不会相信用户输入可接受的信息,也不会相信cin为我处理信息。就我个人而言,我会使用cin.getline(buffer,buffer_size)将cin作为字符串读取;然后,如果用户填充了缓冲区或给了我非整数的内容,我会向用户投诉(你可以用一个函数来检查)。然后你可以准确地说出他们给你的内容,你可以对你的抱怨具体化。这是完美的工作!谢谢你的工作非常完美!谢谢你,请考虑接受下面的答案(如果有什么事情从下面解决了你的问题)。因此,面对同一问题的人可以很快找到它。请考虑接受下面的答案(如果任何事情从下面解决了你的问题),以便面对相同问题的人可以很快找到它。
void rev_sum() {
int num;
int a = 1;
while (a < 2) {
cout << "Please input a natural number without zero at the end:\n";
cin >> num;
if (!cin) {
cout << "The input is not valid.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else if (num % 10 == 0) {
cout << "The number cannot have zero at the end\n";
} else {
cout << "gj\n";
break;
}
}
}