gpt4 book ai didi

c++ - 如何 “restart”程序

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

我是一个刚开始使用C++编写代码已经有几个星期了。我已经编写了一个程序来拥有一个菜单,用户可以从中选择执行不同任务的选项。我有两个问题:首先,我该如何做,以便在执行任务后将用户发送回菜单;其次,当用户分配变量(即您所称的名字)时,我该如何做?保持一致?

#include <iostream>
using namespace std;
int main() {

int choice;
float no0,no1,no2,no3,sum0,ave,pi,rad,areaC,base,height,areaT;
pi=3.142;
cout<<"______________________________________________\n";
cout<<"| MENU |\n";
cout<<"|--------------------------------------------|\n";
cout<<"|1. Calculate the average of 4 numbers. |\n";
cout<<"|2. Calculate the area of a circle. |\n";
cout<<"|3. Calculate the area of a triangle. |\n";
cout<<"|4. Print 'Hello World! |\n";
cout<<"|--------------------------------------------|\n";
cout<<"Enter a number to make a selection... ";

cin>>choice;

switch (choice)
{
case 1:

cout<<"Enter 4 numbers to find their average: ";
cin>>no0>>no1>>no2>>no3;
sum0=no0+no1+no2+no3;
ave=sum0/4;
cout<<"The average of your 4 numbers is: "<<ave<<"\n";
break;
}
switch (choice)
{
case 2:

cout<<"Enter the radius of a cirlce to find its area: ";
cin>>rad;

areaC=pi*rad*rad;

cout<<"The area of your circle is:"<<areaC<<"\n";

break;

}
switch (choice)
{
case 3:

cout<<"Enter the base and height of a triangle to find its area.""\n";
cout<<"Base: "; cin>>base;
cout<<"Height: "; cin>>height;

areaT=base*height*0.5;

cout<<"The area of your triangle is: "<<areaT<<"\n";

break;
}

switch (choice)

{
case 4:

cout<<"Hello World!";

}

return 0;
}

最佳答案

您可以添加一个无限循环,该循环将使您的用户无限期地返回到程序的开头。如果要停止,可以添加一个将active设置为false的案例。

我还修复了您的开关盒。就像有人提到的,不必为每种情况输入switch-程序将自动找到正确的路径。

做这样的事情:

#include <iostream>
using namespace std;
int main() {
bool active = true;
while(active)
{

int choice;
float no0,no1,no2,no3,sum0,ave,pi,rad,areaC,base,height,areaT;
pi=3.142;
cout<<"______________________________________________\n";
cout<<"| MENU |\n";
cout<<"|--------------------------------------------|\n";
cout<<"|1. Calculate the average of 4 numbers. |\n";
cout<<"|2. Calculate the area of a circle. |\n";
cout<<"|3. Calculate the area of a triangle. |\n";
cout<<"|4. Print 'Hello World! |\n";
cout<<"|5. Quit. |\n";
cout<<"|--------------------------------------------|\n";
cout<<"Enter a number to make a selection... ";

cin>>choice;

switch (choice)
{
case 1:

cout<<"Enter 4 numbers to find their average: ";
cin>>no0>>no1>>no2>>no3;
sum0=no0+no1+no2+no3;
ave=sum0/4;
cout<<"The average of your 4 numbers is: "<<ave<<"\n";
break;

case 2:

cout<<"Enter the radius of a cirlce to find its area: ";
cin>>rad;
areaC=pi*rad*rad;
cout<<"The area of your circle is:"<<areaC<<"\n";
break;

case 3:

cout<<"Enter the base and height of a triangle to find its area.""\n";
cout<<"Base: "; cin>>base;
cout<<"Height: "; cin>>height;
areaT=base*height*0.5;
cout<<"The area of your triangle is: "<<areaT<<"\n";
break;

case 4:

cout<<"Hello World!";
break;

case 5:

active = false; // Could even just return 0 here
break;

} // End Switch
} // End Loop

return 0;
}

关于c++ - 如何 “restart”程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60116195/

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