为什么我的程序忽略了switch语句?
Why is my program ignoring the switch statement?
我的程序忽略了switch语句。当我运行它时,我输入我的输入,它只是跳到默认的"无效响应"。无论我输入什么,它都是默认的,完全跳过switch语句的情况。我检查了代码,看起来完全没问题。
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stdio.h>
using namespace std;
void ave();
void lar();
void sma();
void med();
void sorta();
void sortd();
int n;
int n1;
int n2;
int n3;
int n4;
int n5;
int n6;
int n7;
int n8;
int n9;
int n10;
int list[10] = {n1, n2, n3, n4, n5, n6, n7, n8, n9, n10};
int av;
int la;
int sm;
int me;
int sa;
int sd;
bool wayToSort(int i, int j) { return i > j; };
int main()
{
int c;
char q;
cout<< "Enter the first integer";
cin>> n1;
cout<< "Enter the second integer";
cin>> n2;
cout<< "Enter the third integer";
cin>> n3;
cout<< "Enter the fourth integer";
cin>> n4;
cout<< "Enter the fifth integer";
cin>> n5;
cout<< "Enter the sixth integer";
cin>> n6;
cout<< "Enter the seventh integer";
cin>> n7;
cout<< "Enter the eighth integer";
cin>> n8;
cout<< "Enter the ninth integer";
cin>> n9;
cout<< "Enter the tenth integer";
cin>> n10;
cout<< "1 - If you wish to find the average of the integers, press 1 n";
cout<< "2 - If you wish to find the largest integer, press 2 n";
cout<< "3 - If you wish to find the smallest integer, press 3 n";
cout<< "4 - If you wish to find the median number, press 4 n";
cout<< "5 - If you wish to sort the integers in ascending order, press 5 n";
cout<< "6 - If you wish to sort the integers in descending order, press 6 n";
//c = getchar();
cin>> c;
switch (c)
{
case '1':
ave();
break;
case '2':
lar();
break;
case '3':
sma();
break;
case '4':
med();
break;
case '5':
sorta();
break;
case '6':
sortd();
break;
default:
cout<< "Invaid choice";
return 0;
}
void ave();
{
av = (n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9 + n10) /10;
cout<< "The average of the numbers is " << av << endl;
}
void lar();
{
la = n1;
if (n2 > la)
la = n2;
else if (n3 > la)
la = n3;
else if (n4 > la)
la = n4;
else if (n5 > la)
la = n5;
else if (n6 > la)
la = n6;
else if (n7 > la)
la = n7;
else if (n8 > la)
la = n8;
else if (n9 > la)
la = n9;
else if (n10 > la)
la = n10;
cout<< "The largest integer is " << la << endl;
}
void sma();
{
sm = n1;
if (n2 < sm)
sm = n2;
else if (n3 < sm)
sm = n3;
else if (n4 < sm)
sm = n4;
else if (n5 < sm)
sm = n5;
else if (n6 < sm)
sm = n6;
else if (n7 < sm)
sm = n7;
else if (n8 < sm)
sm = n8;
else if (n9 < sm)
sm = n9;
else if (n10 < sm)
sm = n10;
}
void med();
{
sort(list, list + 10);
for (size_t i = 0; i != 10; ++i)
me = 0.5 * (list[n/2] + list[n/2-1]);
}
void sorta();
{
sort(list, list + 10);
cout<< "The sorted array looks like this " << endl;
for (size_t i = 0; i != 10; ++i)
cout<< list[i] << " ";
}
void sortd();
{
vector<int> intVec = { n1, n2, n3, n4, n5, n6, n7, n8, n9, n10};
sort(intVec.begin(), intVec.end(), wayToSort);
for (int i : intVec)
cout<< i << " ";
}
cout<< "n Quit (y/n)";
cin>> q;
q = getchar();
}
你实际上不是在调用一个函数:
case '1':
void ave();
break;
实际上你只是在声明一个函数。
它应该是:
case '1':
ave(); // call function `ave()`
break;
对于您的switch
中的所有其他情况也是如此。
可能还有其他问题,但是你还没有给我们看MCVE,所以很难判断。
你的代码有很多需要重构的地方,也有很多错误。修正和重构它
#include <iostream>
#include <algorithm>
#include <array>
using namespace std;
int main() {
array<int, 10> arr;
int c;
cout << "Enter all the elementsn";
for (int & i : arr) cin >> i;
cout<< "1 - If you wish to find the average of the integers, press 1 n";
cout<< "2 - If you wish to find the largest integer, press 2 n";
cout<< "3 - If you wish to find the smallest integer, press 3 n";
cout<< "4 - If you wish to find the median number, press 4 n";
cout<< "5 - If you wish to sort the integers in ascending order, press 5 n";
cout<< "6 - If you wish to sort the integers in descending order, press 6 n";
cin >> c;
switch (c) {
case 1:
cout << "The average of the numbers is " << accumulate(arr.begin(), arr.end(), 0) / arr.size() << endl;
break;
case 2:
cout << "The largest integer is " << *max_element(arr.begin(), arr.end()) << endl;
break;
case 3:
cout << "The smallest integer is " << *min_element(arr.begin(), arr.end()) << endl;
break;
case 4:
nth_element(arr.begin(), arr.begin() + arr.size() / 2, arr.end());
cout << "The median number is " << arr[arr.size() / 2] << endl;
break;
case 5:
sort(arr.begin(), arr.end());
for (int i : arr) cout << i << " ";
cout << endl;
break;
case 6:
sort(arr.begin(), arr.end(), greater<int>());
for (int i : arr) cout << i << " ";
cout << endl;
break;
default:
cerr << "Invaid choicen";
return 1;
}
return 0;
}
参见实时版。
相关文章:
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 如何将 if else 语句重写为 switch 语句
- Switch 语句(字符串)一直选择默认值,除非其为零
- C++ switch 语句:对案例进行分组,并针对每个案例给出特定的说明
- 任意大小的 constexpr 数组是否可以用作 switch 语句中的案例?
- 默认标签显然在 switch 语句中不起作用
- C++如何在 switch 语句中放置两个参数
- 如何在 c++ 中从 switch 语句返回不同类型的变量
- 在编译时自动生成用于稀疏数组索引的switch语句
- C++ Switch 语句中的数学运算
- 变量不能在 lambda 中隐式捕获,并且没有使用 switch 语句指定捕获默认值
- 在任何案例之前,在 switch 语句中的使用声明是否合法?
- 使用 switch 语句将不同的函数作为类属性分配 C++
- 为什么我的 switch 语句没有响应一半的情况?
- 如何从映射中获取值并在 C++ 的 switch 语句中使用它
- switch 语句导致发送两条消息
- C++ Switch 语句在中断后执行
- switch语句,两种类似情况不同
- switch 语句和对象隐式 int 转换
- switch 语句结束和下一条语句之间的时间是否具有确定性(与路径无关)