gpt4 book ai didi

c++ - 打破 while 循环重复

转载 作者:行者123 更新时间:2023-11-30 03:48:10 27 4
gpt4 key购买 nike

我想要实现的是用户输入一个值,然后输入一个测量值。然后将其放置为最小、最大或介于两者之间。保留输入了多少个值的计数。以及以米为单位的所有值的总和。

程序最初可以运行,但是当我输入换行符时,程序会重复同一行。我想要的是结束打印第 129 和 130 行的程序。这是我的代码。

int main()
{
double var1; //the variable entered by the user
double sum; //the sum of calculations to convert into centimeters
double total=0/100; //converts to meters
int e=0; //will be used to count how many numbers were entered
string measurment; //the users desired measurement input
string centimeter = "cm";
string meter = "m"; //these are used to compare the users measurement input
string inch = "in";
string foot = "ft";
char d='t';//this will be used to break the loop (t is just the default)
char a='a';
char c='c';
char m='m';//char a-f are used for a switch
char i='i';
char f='f';
double small=20000;
double large=0;
double cm=1;
double me=100;//centimeters
double in=2.54; //centimeters
double ft=12; //inches

//the following code creates a break when the character is entered.
while(d!='q')//break rule
{
if(false)
break;

cout<<"please enter a double and a unit of measurement."<<'\n';
cin >>var1>>measurment;
e++;

// the following portion of code sets the char for the switch which
// which will be used in the following code. it will also perform the
// the math which will calculate between distances.

if (measurment==centimeter)
{
a=c;
sum=var1*cm;
}
else if (measurment==meter)
{
a=m;
sum=var1*me;
}
else if (measurment==inch)
{
a=i;
sum=var1*in;
}
else if (measurment==foot)
{
a=f;
sum=(var1*ft)*in;
}
else
{
cout<<"I am sorry. But, that is not a valid measurement for this program."<<'\n';
}

//the following code places the number entered into either
//smallest largest or in between.

if (sum<small)
{
small=sum;
total+=sum;
switch(a){
case'c':
cout<<small/cm<<centimeter<<" is the smallest measurement so far."<<'\n';
break;
case'm':
cout<<small/me<<meter<<" is the smallest measurement so far."<<'\n';
break;
case'i':
cout<<small/in<<inch<<" is the smallest measurement so far."<<'\n';
break;
case'f':
cout<<(small/in)/ft<<foot<<" is the smallest measurement so far."<<'\n';
break;
}
}
else if (sum>large)
{
large=sum;
total+=sum;
switch(a){
case'c':
cout<<large/cm<<centimeter<<" is the largest measurement so far."<<'\n';
break;
case'm':
cout<<large/me<<meter<<" is the largest measurement so far."<<'\n';
break;
case'i':
cout<<large/in<<inch<<" is the largest measurement so far."<<'\n';
break;
case'f':
cout<<(large/in)/ft<<foot<<" is the largest measurement so far."<<'\n';
break;
}
}
else if(sum>small&&sum<large)
{
total+=sum;
switch(a){
case'c':
cout<<var1<<centimeter<<" is neither the longest or shortest measurement."<<'\n';
break;
case'm':
cout<<var1<<meter<<" is neither the longest or shortest measurement."<<'\n';
break;
case'i':
cout<<var1<<inch<<" is neither the longest or shortest measurement."<<'\n';
break;
case'f':
cout<<var1<<foot<<" is neither the longest or shortest measurement."<<'\n';
break;
}
}
}
//after the break, this should be printed to screen
cout<<"Of a total of "<<e<<" entries. "<<small<<meter<<" is the smallest length."<<'\n';
cout<<"And "<<large<<meter<<" is the largest length. "<<total<<meter<<" is the total length."<<'\n';

return 0;
}

如果代码太长,我们深表歉意。我不太确定你需要什么。我检查了许多帖子和网站并尝试了不同的中断代码但无济于事。我卡住了。

最佳答案

也许我遗漏了一些明显的东西,但打破 while 循环的正确方法是使条件失败。中断用于 FOR 循环,因为它们可以是非条件循环。无需跳出 while 循环,您只需创建变量

d ='q'

您想在哪里休息一下。请注意,中断仍然有效以中断 switch 语句,因此如果您需要,请将这些中断保留在那里。

对于任何拼写错误或者如果我遗漏了一个明显的问题,我很抱歉,我是移动的,可能遗漏了什么

关于c++ - 打破 while 循环重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33325588/

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