gpt4 book ai didi

c++ - 在 switch case 语句中循环

转载 作者:行者123 更新时间:2023-11-28 06:54:48 25 4
gpt4 key购买 nike

我需要验证 switch case 语句的帮助我需要它来检查用户输入的内容,如果不匹配则拒绝它并告诉他们再次输入。我目前拥有的那个部分有效,但会拒绝第一个数字,然后在尝试输入另一个数字时中断。帮助 如果您需要查看整个程序,请询问。

#include "stdafx.h"
#include "cstdlib"
#include "iostream"
#include "windows.h"
#include "cmath"


using namespace std;

int main(int argc, char* argv[])
{


float ALT0();
float ALT5000();
float ALT10000();
float ALT15000();
float ALT20000();
float ALT25000();
float ALT30000();
float ALT35000();
float ALT40000();
void an_answer(float a);

char Restart;
char op;
float answer;

do
{
cout << "\n\t\t\tOperational Flight Plan\n" << endl;

cout << "For the saftey of everyone on board, there will be 100 kg added to the overall\namount to give the aircraft more fuel incase of a head on wind or delays at the landing airport.\n" << endl;
cout << "Select Altitude the aircraft will fly at: " << endl;
cout << "0 for 0ft\n1 for 5000ft\n2 for 10000ft\n3 for 15000ft\n4 for 20000ft\n5 for 25000ft\n6 for 30000ft\n7 for 35000ft\n8 for 40000ft" << endl;
cin >> op;
switch (op)
{
case'0':
answer=ALT0();
break;

case '1':
answer=ALT5000();
break;

case '2':
answer=ALT10000();
break;

case '3':
answer=ALT15000();
break;

case '4':
answer=ALT20000();
break;

case '5':
answer=ALT25000();
break;

case '6':
answer=ALT30000();
break;

case '7':
answer=ALT35000();
break;

case '8':
answer=ALT40000();
break;
default:
cout << "You must enter a number from 0-8" << endl;
cin >> op;

break;

}

an_answer(answer);

cout << "Do you want to do another calculation? Press Y for yes and anything else to quit.";
cin >> Restart;
} while (Restart=='y' || Restart=='Y');

//system("PAUSE");
//return EXIT_SUCCESS;
}

最佳答案

我敢打赌您在输入数字后会按回车键。您的第一个 cin >> op 读取数字,但您的第二个读取回车键。如果要读取整行,请使用读取整行的函数。

或者,将第二个 cin >> op 移到 switch 语句之前。如果有人在按下回车键之前输入了多个字符,这将中断,但否则会起作用。

关于c++ - 在 switch case 语句中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23322786/

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