【发布时间】:2014-03-13 01:26:06
【问题描述】:
我正在使用 if 语句让用户输入一个 bool 值,如果他们输入 1 则程序继续执行,如果他们输入 0 则我希望程序完全停止运行。这是我正在使用的代码。
bool subscription;
cout << "Would you like to purchase a subscription to our newspaper?\n";
cout << "Enter 1 if yes, and 0 if no. ";
cin >> subscription;
if(subscription == false)
{
cout << "We're sorry you don't want our services.";
//this is where i want the program to stop, after it outputs that line.
}
else if(subscription == true)
{
cout << "\nPlease enter your first and last name. ";
}
我曾尝试在cout 语句之后使用return 0;,但这不起作用,它只会输出语句然后继续执行程序。
我也试过exit();,结果完全一样。
【问题讨论】:
-
if(subscription = false)- 你真的是说if(subscription == false)吗? -
我会编译这将完整的警告,这些会告诉你问题
-
是的,这就是我的意思,但我认为这不能解决问题。
-
这段代码在主函数中吗?你试过 exit(0) 吗?你的意思是你的应用程序没有退出?
-
exit(0)应该可以工作。如果你想要一个异常的进程终止你可以使用abort()这两个函数都在cstdlib