Hello I have this code and I cant seem to get it to work. The loop loops for three times and then the addCourse is suppose to add all three selections. The problem is the addCourse function only adds the last bit for all three selections. Example :

If I select 1 2 3 then it is suppose to give out:

  1. Maths
  2. Quantum
  3. Reality

Instead it gives out:

  1. Reality
  2. Reality
  3. Reality

(All three is the case 3 which is selected last)

How do I sort this problem. Thank you. Below is the code.

for(int p = 0; p < 3; p++)
{
    cout << "Please select a course:" <<endl;
    cout<<"1. Maths\n";
    cout<<"2. Quantum\n";
    cout<<"3. Reality\n";
    cin >> coursepick;
    switch (coursepick)
    {
    case 1: course= "Maths";
    case 2: course= "Quantum";
    case 3: course= "Reality";
    default: break;
    }
    cout << "Please insert the course mark:" <<endl;
    cin >> mark;
    cin.ignore();
    phys[0]->addCourse(course,mark);
}
有帮助吗?

解决方案

switch (coursepick)
{
case 1: course= "Maths"; break;
case 2: course= "Quantum"; break;
case 3: course= "Reality"; break;
default: break;
}

if you don't break at the end of each case, it just continue with the next, and the next, and then until case 3 and break on default.

其他提示

In C++, case statements automatically move on to the next. You must insert break after each case.

switch (coursepick)
{
    case 1: course= "Maths"; break;
    case 2: course= "Quantum"; break;
    case 3: course= "Reality"; break;
    default: break;
}

In your switch statement you need a break statement in each case. Without a break statement C will fall to the next case. You case statement is equivalent to:

course= "Maths";
course= "Quantum";
course= "Reality";

Instead it needs to be:

switch (coursepick)
{
    case 1: course= "Maths"; break;
    case 2: course= "Quantum"; break;
    case 3: course= "Reality"; break;
    default: break;
}

As mentioned in the other posts, you need to end each case with the keyword "break;" if you don't want it to trickle down into the next case.

switch (coursepick)
{
    case 1: course= "Maths"; break;
    case 2: course= "Quantum"; break;
    case 3: course= "Reality"; break;
    default: break;
}

Switch statements have this behavior because sometimes you want it to filter through multiple cases. This saves you from having to duplicate code when multiple cases have the same result. The following example shows the equivalent of if case 'a' OR case 'A', do this. If case 'b' OR 'B', do this.

switch (input)
{
    case 'a':
    case 'A': text = "Letter A"; break;

    case 'b':
    case 'B': text = "Letter B"; break;

    default: text = "Not A or B"; break;
}

Dude Can you add Break tag for all the case statements. It Should be like this

switch (coursepick)
    {
    case 1: course= "Maths";
            break;
    case 2: course= "Quantum";
            break;
    case 3: course= "Reality";
            break;
    default: break;
    }

If you don't mention it will be through all the cases and finally in case three course will be over written by Reality.

So break is necessary for case statements

As it was already said you have to use break for any case label.

switch (coursepick)
{
case 1: course= "Maths"; break;
case 2: course= "Quantum"; break;
case 3: course= "Reality"; break;
default: break; 
}

But in any case the code is invalid bacause if the user will enter a number outside the acceptable range variable course will not be assigned. However you will use this unassigned variable in statement

phys[0]->addCourse(course,mark);

Also it is not clear why the variable is defined outside the loop.

I would write the loop the following way

enum { Maths = 1, Quantum, Reality };

for ( int p = 0; p < 3; p++ )
{
    do
    {
        cout << "\nPlease select a course:" <<endl;
        cout<<"1. Maths\n";
        cout<<"2. Quantum\n";
        cout<<"3. Reality\n";

        cin >> coursepick;

        if ( !( Maths <= coursepick && coursepick <= Reality ) )
        {
            cout << "Error. Invalid input. Please repeat" << std::endl;
        }
    } while ( !( Maths <= coursepick && coursepick <= Reality ) ); 

    switch (coursepick)
    {
        case Maths: course= "Maths"; break;
        case Quantum: course= "Quantum"; break;
        case Reality: course= "Reality"; break;
        default: abort();
    }

    cout << "Please insert the course mark:" << endl;
    cin >> mark;
    cin.ignore();
    phys[0]->addCourse( course, mark );
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top