为什么我的程序忽略了switch语句?

Hamzah Hashmi |

我的程序忽略了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();

}

Shreevardhan

您的代码有很多需要重构的地方,并且也有很多错误。更正和重构

#include <iostream>
#include <algorithm>
#include <array>
using namespace std;

int main() {
    array<int, 10> arr;
    int c;
    cout << "Enter all the elements\n";
    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 choice\n";
        return 1;
    }
    return 0;
}

请参阅LIVE VERSION

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我的 if 语句忽略我的变量?

为什么我的if else语句被忽略

为什么我的代码中的 if 语句被忽略?

为什么当我输入正确的输入时switch语句被忽略

为什么if语句被忽略?

为什么我的程序在#之后忽略参数?

为什么我的switch语句在C ++程序中不能正确输出星号?

为什么 Python 会忽略我的输入并跳过我的 if 语句?

为什么我的 If 语句在我的 For 循环中被忽略?

为什么我的程序不断跳过 if 语句?

为什么我的if语句中的条件被忽略?C#

为什么我需要指定 $switch default 语句而它是可选的?

为什么我的单元测试未能通过switch语句?

为什么我的JavaScript switch语句不起作用?

为什么我不能在类中放置 switch 语句

为什么我的 switch 语句只运行一次?

为什么我的switch语句参数需要布尔值?

为什么我的python程序会忽略/跳过某些情况

为什么计算机会忽略我的程序?

为什么我的程序忽略了 115 行代码

为什么switch语句仅运行默认值而忽略其余条件?

为什么if语句优先于switch语句?

为什么我的程序使用switch总是默认

为什么我的if语句在switch语句中总是评估为false?

在我的 if-else 语句之后,Switch 语句被忽略

为什么我的for循环被忽略?

为什么循环内的firebase语句被忽略?

为什么JSLint向我警告我的switch-case语句格式错误?

为什么我的return语句会忽略python函数中的其余代码?