c++ - 如何立即停止/结束程序?

标签 c++ c++11 visual-c++ c++14 c++17

询问用户是否要返回主菜单,如果用户输入n / N,则仅在应立即结束程序时才进入下一个解决方案。下面显示的是我用于程序的代码。请帮我就如何结束时立即没有选择他/她的选择程序的解决方案。非常感谢!

void number()

int b=0;
int groupChoice=0;
float ave[groupChoice];
int trials[groupChoice];
float result,sumRes,dAve;
int sumTry=0;
char choice;

cout << "\nNUMBER OF TRIALS" << endl;
cout << "\nHow many groups? ";
cin >> groupChoice;
for (int j=0;j<groupChoice;j++)
{
    cout << "Average distance for group " << j+1 << ": ";
    cin >> ave[j];
    cout << "No. of trials for group " << j+1 << ": ";
    cin >> trials[j];
}
cout << "\nGroups\t\tAve. Distance(x)\tNo. of trials(w)\tx(w)" << endl;
for (int i=0;i<groupChoice;i++)
{
    result=ave[i]*trials[i];
    cout << "Group " << i + 1 << "\t\t" << ave[i] << "\t\t\t" << trials[i] << "\t\t\t" << result << endl;
    sumTry=sumTry+trials[i];
    sumRes+=result;
}
cout << "\t\t\t\t\tSum = " << sumTry << "\t\tSum = " << sumRes << endl;
dAve = sumRes / sumTry;
cout << "Distance Average is " << dAve << endl << endl;
b=0;
while(b==0)
{
    cout << "Would you like to return to main menu? [Y or N]: ";
    cin >> choice;
    if (choice=='Y'||choice=='y')
    {
        b++;
        system("cls");
        a=0;
        main();
    }
    else if (choice=='N'||choice=='n')
    {
        b++;
        break;
    }
}

最佳答案

您可以使用以下方式终止程序

void std::exit( int exit_code );

例如
std::exit(EXIT_SUCCESS);

https://en.cppreference.com/w/cpp/utility/program/exit

关于c++ - 如何立即停止/结束程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231998/

相关文章:

C++ 将 int 和 string 转换为 char*

c++ - 在比较值时解析两个文件

来自模板参数的 C++ 通用模板方法名称

c++ - CRT 语言环境临界区死锁

python - C1083 无法打开包含文件 : "Python.h": No such file or directory

c++ - 在堆或堆栈上分配对象

c++ - 将 C 代码合并到同一个 R 包中

c++ - std::end 如何知道数组的结尾?

C++11 std::thread 给出错误:没有匹配的函数来调用 std::thread::thread

c++ - 有没有办法指示 C++ 编译器跳过当前文件的其余部分?