gpt4 book ai didi

c++编码选择开关代码上的每个案例

转载 作者:太空宇宙 更新时间:2023-11-03 10:44:27 24 4
gpt4 key购买 nike

所以我有一个项目,这是我的代码,但我在这段代码中遇到问题,而不是只选择一个,而是选择所有可能的情况,所以帮助我,这是代码,我也不知道那是怎么发生的,我仍在寻找发生这种情况的原因,但也许我需要专业帮助,所以这就是代码

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main()
{
char one, two, three, four;
double a, b, c, d;



cout << "What do you want to find on the square?" << endl;
cout << "A. Area" << endl;
cout << "B. Side" << endl;
cout << "C. Diagonal" << endl;
cout << "D. Perimeter" << endl;
cin >> one ;
one = toupper(one);
switch (one)
{
case 'A':
{ cout << "What is Given" << endl;
cout << "S. Side" << endl;
cout << "D. Diagonal" << endl;
cout << "P. Perimeter" << endl;
cin >> two;
two = toupper(two);
switch (two)
{
case 'S':
{
cout << "Enter Measure of the side." << endl;
cin >> a;
a= a*a;
cout << "The Answer is " << a << endl;

}
case 'D':
{
cout << "Enter Measure of the diagonal" << endl;
cin >> a;
a= pow( a/sqrt(2), 2);
cout << "The Answer is " << a << endl;

}
case 'P':
{
cout << "Enter measure of Perimeter" << endl;
cin >> c;
c= pow(c/4, 2);
cout << "The Answer is " << c << endl;
}
default :
{
}
}
}
}


return 0;
}

最佳答案

你需要

 break

在每个案例之后。像这样:

 case 'S':
{
cout << "Enter Measure of the side." << endl;
cin >> a;
a= a*a;
cout << "The Answer is " << a << endl;
break;
}

对于每种情况。否则它将通过所有案例而不会中断,这就是您的案例中发生的事情

关于c++编码选择开关代码上的每个案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25417587/

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