简体   繁体   中英

Why is my switch statement not working properly?

In my switch statement there's 3 options 2 are working properly and the third one shows what's I wrote in second instead of its own code.

I tried using case 1: , case 2: but it says that case 2: is not in the switch statement.

int main()

{
int choice;
char y;

do
{
    cout << "1. BSEE" <<endl;
    cout << "2. BSIE-ICT" <<endl;
    cout << "3. BGT-AT" <<endl;
    cout << endl;
    cout << "Choose your Course" <<endl;
    cout << endl;
    cin >> choice;
    cout << endl;
   switch (choice)
{

cout << "Welcome to GPA Calculator" <<endl;
cout << endl;
cout << "Grade Equivalent (A=1, B=1.25, C=1.5, D=1.75, E=2., F=2.25, G=275, H=2.75, I=3, J=5" << endl;


double numericGrade[10];
string courses[10] = { "BES1", "CHEMENG", "CHEMLAB", "ESW", "GEC5", "GEC6", "GEC7", "MATHENG", "NSTP", "PE" };
int creditForCourses[10] = { 1, 3, 1, 2, 3, 3, 3, 3, 3, 2 };
double gpa = 0.00;
int i;

    for (i = 0; i < 10; i++) {
    if (letterGrade[i] == "A") {
        numericGrade[i] = 1;
        }
        else
        if (letterGrade[i] == "B") {
        numericGrade[i] = 1.25;
        }
        else
        if (letterGrade[i] == "C") {
        numericGrade[i] = 1.5;
        }

 }

    for (i = 0; i < 10; i++) {
        gpa += (numericGrade[i] * creditForCourses[i]);
    }

    gpa /= 27;

    cout << "Your GPA is: " << gpa << endl;

    break;
}
{


string letterGrade[10]; 
double numericGrade[10];
string courses[10] = { "GEC4", "GEC5", "HE1E", "HE1EL", "ICT1E", "ICT1EL", "NSTP1", "PE1", "PIE1", "PI2" };
int creditForCourses[10] = { 3, 3, 3, 1, 3, 3, 3, 2, 3, 3 };
double gpa = 0.00;
int i;
  }

    for (i = 0; i < 10; i++) {
    if (letterGrade[i] == "A") {
        numericGrade[i] = 1;
        }
        else
        if (letterGrade[i] == "B") {
        numericGrade[i] = 1.25;
        }
        else
        if (letterGrade[i] == "C") {
        numericGrade[i] = 1.5;
        }

 }

    for (i = 0; i < 10; i++) {
        gpa += (numericGrade[i] * creditForCourses[i]);
    }

    gpa /= 27;

    cout << "Your GPA is: " << gpa << endl;

    break;
}
while (y=='y'||y=='Y');
cout<<"The Program will now be terminated, thank you";
getch();
}

cut some of the codes because it's too long but it's all the same of the first { } just the names of the subjects are different

the swith statement format is

switch(choice) 
{
    case 1:
      //make first operation
      break;
    case 2:
    case 3:
      //make second operation
      break;
    default:
       //make else operation
}

which is equivalent to

if (choice == 1)
   //make first operation
else if (choice == 2 || choice == 3)
   //make second operation
else
   //make else operation

apparently your code does not follow format of switch case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM