gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:23:02 26 4
gpt4 key购买 nike

询问用户是否要返回主菜单,如果用户输入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/

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