-5

I have to make a program that displays the value for given expressions in C++. So, I decided to make a switch/case and use it for calculating and displaying them! However, I have some problem with an error I get, which I can't solve...

#include <iostream>

using namespace std;

int main()
{

    for(int i = 0; i < 6; i++){
        int x = 5, y = 8;

        switch case(i){

            case 0:
            x+=(++x)+(x++);
            cout << "x+=(++x)+(x++) = " << x << endl;
            break;

            case 1:
            x+=++y;
            cout << "x+=++y = " << x << endl;
            break;

            case 2:
            x+=2*x++;
            cout << "x+=2*x++ = " << x << endl;
            break;

            case 3:
            x=--y+x--+x;
            cout << "x=--y+x--+x = " << x << endl;
            break;

            case 4:
            x-=(-y)%3;
            cout << "x-=(-y)%3 = " << x << endl;
            break;

            case 5:
            y+=--y+x-y%x--;
            cout << "y+=--y+x-y%x-- = " << y << endl;
            break;

            case 6:
            x+=++y---x+y++;
            cout << "x+=++y---x+y++ = " << x << endl;
            break;

            default:
            cout << "Wrong value." << endl;
            break;
        }

    }

    return 0;
}

The line which returns an error is

switch case(i){

And also, there are two warnings, variables 'x' and 'y' not being used, but I do use them in each case. Should I separately declare them in each case? They must have the value 5 and 8 as beginning value for each case.

4

4 回答 4

4

正如错误试图告诉你的那样,switch caseC 是不合法的。
你需要switch(i).

于 2013-10-16T21:26:03.507 回答
2
switch case(i){ 

不是有效的语法。

它应该是

switch(i) {

由于此错误导致内部大小写条件不可见,因此它会向您发出警告,variables 'x' and 'y' not being used

于 2013-10-16T21:26:47.613 回答
2

我认为你是在其他语言环境中长大的。正确的语法是:

switch(i) {
   // case statements
   // optional default statement
}
于 2013-10-16T21:26:49.753 回答
1

请检查 C++ switch 语句语法:

switch (i) {
    ....
}
于 2013-10-16T21:29:41.260 回答