Wie zu verwenden, WENN in der Switch Anweisung

Anwenden, wenn die Bedingung im Switch-Anweisung, ich soll für die Berechnung der Durchschnittlichen: aber ich habe versucht, die am besten um das Problem zu lösen, aber noch immer keine Ausgabe von Switch-Anweisung.
Ich bin Anfänger C++,

#include <iostream>

using namespace std;

int main()
{
    //declaration of variables.
    int sub1,sub2,sub3, sub4,sub5,total,avg;

    //accepting marks from user in each subject
    cout << " Enter Programming in C++ Marks: " << endl;
    cin >> sub1;
    cout << " Enter Software Engineering Marks : " << endl;
    cin >> sub2;
    cout << " Enter Personal Communication Marks : " << endl;
    cin >> sub3;
    cout << " Enter Database Application Marks: " << endl;
    cin >> sub4;
    cout << " Enter Computer Concept Marks: " << endl;
    cin >> sub5;

    //calculatin sum of marks obtained in each subject
    total = (sub1 + sub2 + sub3 + sub4 + sub5);

    //calculating the average marks
    avg = total / 5;

    //starting of if condition for finding out grades of total subjects.

    switch (avg){
      case 1:{
        if ((avg >= 80) && (avg <= 100))
        {
            cout << " Your Average is: " << avg << endl;
            cout << "You Grade is A+ " << endl;
        }
        break; 
      }
      case 2:{
        if ((avg >= 70) && (avg <= 79))
        {
            cout << " Your Average is: " << avg << endl;
            cout << " Your grade is A " << endl;
        }    
        break;
      } 
      case 3:{
        if ((avg >= 60) && (avg <= 69))
        { 
            cout << " Your Average is: " << avg << endl;
            cout << " Your Grade is B+  " << endl;
        }   
        break;  
      }
      case 4:{
        if ((avg >= 50) && (avg <= 59))
        {
            cout << " Your Average is: " << avg << endl;
            cout << " Your Grade is C+ " << endl;
        }
        break;
      }
      case 5: {
        if ((avg >= 40) && (avg <= 49))
        {
            cout << " Your Average is: " << avg << endl;
            cout << " Your Grade is  C- ! " << endl;
        }
        break;    
      }
      case 6: {
        if ((avg >= 30) && (avg <= 39))
        {
            cout << " Your Average is: " << avg << endl;
            cout << " Your Grade is  D ! " << endl;
        }
        break; 
      }

      default:{ 
        if ((avg >= 100) && (avg <= 29))
        {
            cout << " Your Average is: " << avg << endl;
            cout << " Your Grade is  F, So you are Fail in the class ! " << endl;
            break;
        }
      }
    }

    system("pause");
}
Ich denke, das haben Sie missverstanden die Verwendung von switch-case. Entfernen Sie den Schalter Fall, und Sie erhalten die gewünschte Ausgabe.
Vielen Dank Für Ihre Wertvolle Antwort. Ich weiß, der code ist falsch Eingegeben, aber ich möchte klar ich hab nur switch-Anweisung nur.

InformationsquelleAutor Mustafa Muhammad Yousif | 2012-07-16

Schreibe einen Kommentar