错误输入将退出程序C++ 我开始学习C++,我写了这个简单的程序,当用户输入错误的数字时,它给出一个选项来重试,然而当用户输入任何字符时,它就给出了重试并直接退出程序的选项,为什么会发生这种情况? `
`扫描%d 我打赌你的问题来自%d之前的空间。请尝试扫描%d。以后相同:scanf%c而不是scanf%c 不管怎样,你的代码很脏。这看起来像C,而不是C++。 在这里你不想成为一个疯子,但是你应该适当地缩进,尽一切办法避免跳槽。你的程序结构很简单,一个while循环就可以了。您还应该避免显式调用exit1。退出主要用于引发提前终止。在您的情况下,您的程序将正常结束,您应该退出主函数并返回0错误输入将退出程序C++ 我开始学习C++,我写了这个简单的程序,当用户输入错误的数字时,它给出一个选项来重试,然而当用户输入任何字符时,它就给出了重试并直接退出程序的选项,为什么会发生这种情况? `,c++,C++,`扫描%d 我打赌你的问题来自%d之前的空间。请尝试扫描%d。以后相同:scanf%c而不是scanf%c 不管怎样,你的代码很脏。这看起来像C,而不是C++。 在这里你不想成为一个疯子,但是你应该适当地缩进,尽一切办法避免跳槽。你的程序结构很简单,一个while循环就可以了。您还应该避免显式调用exit1。退出主要用于引发提前终止。在您的情况下,您的程序将正常结束,您应该退出主函数并返回0 scanf留下了一些东西,有时您可以通过刷新它来清理这些东西,但在代码中似乎不起作用 看看这个问题:
scanf留下了一些东西,有时您可以通过刷新它来清理这些东西,但在代码中似乎不起作用 看看这个问题: 这基本上告诉你,如果你的方法不起作用,那么你应该改变它。 希望对您有所帮助,您在其中一个问题中也询问了,因此:
#include <iostream>
int main(){
char input;
char try_again;
do {
std::cout << "INPUT ONLY NUMBER:";
std::cin >> input;
// http://en.cppreference.com/w/cpp/string/byte/isdigit
// does not return a bool, you can check that >0
if (std::isdigit(input)) {
// do what you want.
std::cout << "digit\n";
continue;
} else {
// you can as for more imput like:
std::cout << "Not a number, try again?(y/Y):";
std::cin >> try_again;
std::tolower(try_again);
if (try_again == 'y') {
continue; // will start the loop again.
} else {
break; // it will exit the loop.
}
}
} while(true);
return 0;
}
如果用户输入错误的数字,它工作得很好,它只有在用户输入字符时才退出,所以我已经开始学习C++了,这看起来像C,而不是C++。确定你选对了书?无论如何,找一本更好的书!goto有它的应用程序,但是初学者不应该从它开始!1970ies/80ies早已过时,请使用结构化代码!使用代码循环,而不是GOTO语句,但我想知道为什么会发生这种情况?用AcLoop语句重新编写代码,使用C++ IoFrices,而不是C样式。然后再试一次。如果这太复杂了,那就从简单的开始。阿尔索斯见。
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h> //Reauired to use boolean type in C
int main()
{
bool stop = false; //boolean type only has two states: true and false. Very useful for loops!
while(!stop) //Read as "While we don't need to stop, execute the loop's contents
{ //Much easier to read!
printf("INPUT ONLY NUMBER 1 : ");
scanf("%d", &a);
if(a == 1)
{
printf("you entered correctly \n");
printf("do you want to try again? <Y> <N>\n");
scanf("%c", &c);
if(c == 'N' || c == 'n')
{ //We only need to tell the loop when to stop
stop = true; //by setting stop to true
} //The loop's default behavior is to loop execution of its content
}
else
{
sleep(1);
printf("wrong number , do you want to try again? <Y> <N> \n");
scanf("%c" , &b);
if(b=='n'|| b=='N')
{
stop = true; //Same as above
}
sleep(1);
}
}
printf("thank you and goodbye");
return 0;
}
#include <iostream>
int main(){
char input;
char try_again;
do {
std::cout << "INPUT ONLY NUMBER:";
std::cin >> input;
// http://en.cppreference.com/w/cpp/string/byte/isdigit
// does not return a bool, you can check that >0
if (std::isdigit(input)) {
// do what you want.
std::cout << "digit\n";
continue;
} else {
// you can as for more imput like:
std::cout << "Not a number, try again?(y/Y):";
std::cin >> try_again;
std::tolower(try_again);
if (try_again == 'y') {
continue; // will start the loop again.
} else {
break; // it will exit the loop.
}
}
} while(true);
return 0;
}