gpt4 book ai didi

c++ - Switch case 程序在某个点不断循环。想不通

转载 作者:行者123 更新时间:2023-11-30 20:45:54 25 4
gpt4 key购买 nike

我想我已经尽力了,但是当我按选项传递3时,它总是显示为选项3。案例1,2,3按预期工作,但4,5,6,7,8,9, 10 将自动像情况 3 一样出现。那么您认为缺少什么?

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdio.h>
using namespace std;


int pgk, ch1, ch2; // variables for the Menu1 and Menu2
char ch3; // Variable for menu 3


int main()
{
printf("1, Final Lab\n"); // first option
printf("2, Exit\n");// second option
cin >> ch1; // computer is going to scan the option from keyboard
// Switch change the option depending of the number we put in the keyboard
switch (ch1)
{
case 1:
{
goto Menu2; // if the user enters 1, it is going to send to send it to do the operations
}
case 2:
{
goto End; // if the user enters 2, it is going to end the program.
}

Menu2:
printf(" --------------------------Menu-------------------\n");
printf("1:Pounds to kilograms\n");
printf("2:inches to centimeters\n");
printf("3:cups to milliters\n");
printf("4:seconds to minutes \n");
printf("5:Knots to mph\n");
printf("6:tip calculator\n");
printf("7:Grade average\n");
printf("8:target heart rate calculator\n");
printf("9:discount calculator\n");
printf("10:end\n");
cin >> ch2; // enter the option the user wants
switch (ch2) // Options
{
case 1:
{
system("cls");
int pkg;
printf("Choose to convert pounds to kg or kg to pounds. Press 0 for 1st option and press any other number for 2nd option.\n");
scanf_s("%f", &pkg);
_asm
{
MOV EBX, pkg;
CMP pkg, 0;
JE pounds_kilos;
JMP kilos_pounds;
}

pounds_kilos:
float nguypounds, nguyconvert1, nguyanswer1;
nguyconvert1 = 2.204622f;
printf("Enter the amount of pounds you would like to convert to kilograms\n");
scanf_s("%f", &nguypounds);

_asm
{

FLD nguypounds;
FDIV nguyconvert1;
FST nguyanswer1;
}
printf("Your enter number of pounds to kilograms is %f\n", nguyanswer1);
system("pause");
goto Menu3;

kilos_pounds:
float nguykilos, nguyconvert2, nguyanswer2;
nguyconvert2 = 2.204622f;
printf("Enter the amount of kilograms you would like to convert to pounds\n");
scanf_s("%f", &nguykilos);

_asm
{
FLD nguykilos;
FMUL nguyconvert2;
FST nguyanswer2;
}
printf("Your enter number of kilograms to pounds is %f\n", nguyanswer2);
goto Menu3;

}

case 2: // if the user enters 2
{

system("cls");
int pkg;
printf("Choose to convert inches to centimeters or centimeters to inches. Press 0 for 1st option and press any other number for 2nd option.\n");
scanf_s("%f", &pkg);
_asm
{
MOV EBX, pkg;
CMP pkg, 0;
JE inches_centimeters;
JMP centimeters_inches;
}

inches_centimeters:
float nguyinches, nguyconvert3, nguyanswer3;
nguyconvert3 = 2.54f;
printf("Enter the amount of inches you would like to convert to centimeters\n");
scanf_s("%f", &nguyinches);

_asm
{

FLD nguyinches;
FMUL nguyconvert3;
FST nguyanswer3;
}
printf("Your enter number of inches to centimeters is %f\n", nguyanswer3);
goto Menu3;
}
centimeters_inches:
float nguycentimeters, nguyconvert4, nguyanswer4;
nguyconvert4 = 0.39370079f;
printf("Enter the amount of centimeters you like to convert to inches\n");
scanf_s("%f", &nguycentimeters);

_asm
{
FLD nguycentimeters;
FMUL nguyconvert4;
FST nguyanswer4;
}
printf("Your enter number of centimeters to inches is %f\n", nguyanswer4);

goto Menu3;

}
case 3: // if the user enter 3
{
system("cls");
int pkg;
printf("Choose to convert cups to milliters or milliters to cups. Press 0 for 1st option and press any other number for 2nd option.\n");
scanf_s("%f", &pkg);
_asm
{
MOV EBX, pkg;
CMP pkg, 0;
JE cups_milliters;
JMP milliters_cups;
}

cups_milliters:
float nguycups, nguyconvert5, nguyanswer5;
nguyconvert5 = 236.58823f;
printf("Enter the amount of cups you would like to convert to milliters\n");
scanf_s("%f", &nguycups);

_asm
{

FLD nguycups;
FMUL nguyconvert5;
FST nguyanswer5;
}
printf("Your enter number of cups to milliters is %f\n", nguyanswer5);
system("pause");

goto Menu3;

milliters_cups:
float nguymilliters, nguyconvert6, nguyanswer6;
nguyconvert6 = 236.58823f;
printf("Enter the amount of milliters you like to convert to cups\n");
scanf_s("%f", &nguymilliters);

_asm
{
FLD nguymilliters;
FDIV nguyconvert6;
FST nguyanswer6;
}
printf("Your enter number of milliters to cups is %f\n", nguyanswer6);
goto Menu3;

}
case 4: // if the user enters 4
{
system("cls");
int pkg;
printf("Choose to convert seconds to minutes or minutes to seconds. Press 0 for 1st option and press any other number for 2nd option.\n");
scanf_s("%f", &pkg);
_asm
{
MOV EBX, pkg;
CMP pkg, 0;
JE sec_min;
JMP min_sec;
}
sec_min:
float nguyseconds, nguyconvert7, nguyanswer7;
nguyconvert7 = 60;
printf("Enter the amount of seconds you would like to convert to minutes\n");
scanf_s("%f", &nguyseconds);

_asm
{

FLD nguyseconds;
FDIV nguyconvert7;
FST nguyanswer7;
}
printf("Your enter number of seconds to minutes is %f\n", nguyanswer7);
goto Menu3;

min_sec:
float nguyminutes, nguyconvert8, nguyanswer8;
nguyconvert8 = 60;
printf("Enter the amount of minutes you like to convert to seconds\n");
scanf_s("%f", &nguyminutes);

_asm
{
FLD nguyminutes;
FMUL nguyconvert8;
FST nguyanswer8;
}
printf("Your enter number of minutes to seconds is %f\n", nguyanswer8);
system("pause");
goto Menu3;
}
case 5:
{
system("cls");
int pkg;
printf("Choose to knots to mph or mph to knots. P=ress 0 for 1st option and press any other number for 2nd option.\n");
scanf_s("%f", &pkg);
_asm
{
MOV EBX, pkg;
CMP pkg, 0;
JE knots_mph;
JMP mph_knots;
}
knots_mph:
float nguyknots, nguyconvert9, nguyanswer9;
nguyconvert9 = 1.15f;
printf("Enter the amount of knots you would like to convert to mph\n");
scanf_s("%f", &nguyknots);

_asm
{

FLD nguyknots;
FMUL nguyconvert9;
FST nguyanswer9;
}
printf("Your enter number of knots to mph is %f\n", nguyanswer9);
goto Menu3;

mph_knots:
float nguymph, nguyconvert10, nguyanswer10;
nguyconvert10 = 1.15f;
printf("Enter the amount of mph you like to convert to knots\n");
scanf_s("%f", &nguymph);

_asm
{
FLD nguymph;
FDIV nguyconvert10;
FST nguyanswer10;
}
printf("Your enter number of minutes to seconds is %f\n", nguyanswer10);
system("pause");
goto Menu3;

}
case 6:
{
system("cls");
float nguybill, nguytip, nguyamount;
printf("Please enter the total amount of your bill\n");
scanf_s("%f", &nguybill);
printf("Now add the percentage you would like to tip. PLEASE ENTER IN PERCENTAGE IN DECIMAL FORM!\n");
scanf_s("%f", &nguytip);

_asm
{
fld nguybill
fld nguytip
fmul nguybill
fadd st(0), st(1)
fst nguyamount
}
printf("the the toal amount after tip is %f\n", nguyamount);
goto Menu3;

}
case 7:
{
system("cls");
float nguytest1, nguytest2, nguytest3, nguyave, nguytotave;
nguyave = 3;
printf("Please enter the grade of your first test.\n");
scanf_s("%f", &nguytest1);
printf("Please enter the grade of your second test.\n");
scanf_s("%f", &nguytest2);
printf("Please enter the grade of your thrid test.\n");
scanf_s("%f", &nguytest3);
_asm
{
fld nguytest1
fld nguytest2
fld nguytest3
fadd st(0), st(1)
fadd st(0), st(2)
fdiv nguyave
fst nguytotave
}
printf("Your average grade is %f", nguytotave);
goto Menu3;
}
case 8:
{
system("cls");
float nguyage, nguyageconst, nguymaxhr, nguytarghr, nguymulmax;
nguyageconst = 220;
nguymulmax = .70f;
printf("Please enter the total amount of your age to find your max heart rate and target heart rate\n");
scanf_s("%f", &nguyage);

_asm
{
fld nguyage
fld nguyageconst
fsub nguyage
fst nguymaxhr
}

_asm
{
fld nguymaxhr
fmul nguymulmax
fst nguytarghr
}
printf("your max heart rate should be %f beat per minute\n", nguymaxhr);
printf("and your target heart rate should be %f beats per minute\n", nguytarghr);
goto Menu3;

}
case 9:
{
system("cls");
float nguyogprice, nguydis, nguyoff, nguynamount;
printf("Please enter the oringal price\n");
scanf_s("%f", &nguyogprice);
printf("Now enter the given discout. PLEASE ENTER IN PERCENTAGE IN DECIMAL FORM!\n");
scanf_s("%f", &nguydis);

_asm
{
fld nguyogprice
fld nguydis
fmul nguyogprice
fst nguyoff
}

_asm
{
fld nguyoff
fld nguyogprice
fsub st(0), st(1)
fst nguynamount
}
printf("The new sale price is %f\n", nguynamount);
goto Menu3;

}
case 10: // if the user enters 5
{
goto End; //go to end
}
}

Menu3: // This menu is going to ask if the user wants to continue the program
printf(" -------------------------Question---------------------\n"); // display message
printf("Do you want to continue the program Y/N\n"); // display message
cin >> ch3; // enter yes or no
switch (ch3) // conditional
{
case 'y': // if the user enters 'y'
{
goto Menu2; // go to menu 2 and continue the program
}
case 'Y': // if the user enters 'Y'
{
goto Menu2; // go to menu 2 and continue the program
}
case 'n': // if the user enters 'n'
{
goto End; // finish the program
}
case 'N': // if the user enters 'N'
{
goto End; // finish the program
}


}

End: // menu to end the program
system("pause"); // pause the system
return 0;// end program

}

最佳答案

case 3 之前的右大括号被错误地放置在那里,它关闭了 switch 语句。因此输入 3 到 9 都以情况 3 结束。

无意冒犯,但由于可维护性和效率较差(编译器的优化机会),在非常特定的上下文之外使用 goto 长期以来一直被认为是极其糟糕的编程实践。投入时间和精力来理清代码和重组程序将会得到返回。

关于c++ - Switch case 程序在某个点不断循环。想不通,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37372129/

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