这个程序有什么问题

时间:2010-02-22 06:48:10

标签: c++

#include <iostream> 
using namespace std;

int main () 
{
    double testscore;
    cout << " Enter your test score and i will tell you the letter grade you earned ";
    cin >> testscore;
    switch (testscore)
    {
        case (testscore < 60.0):
            cout << "your grade is F.\n";
            break;
        default:
            cout << "that score isnt valid\n.";
    }
return 0;
}

7 个答案:

答案 0 :(得分:3)

有几个问题:

  • 您在}的末尾错过了右括号main()。不确定这是故意还是剪切和粘贴错误。

  • return位于switch语句内,这意味着非默认情况不会返回该函数。由于函数返回int,这将导致编译错误。

  • 您无法启用此类表达式。请改用if语句。

答案 1 :(得分:2)

您只能将switch与整数类型一起使用。将您的开关更改为if

(你错过了结束switch大括号。)

答案 2 :(得分:1)

您尚未关闭switch语句块。

答案 3 :(得分:1)

您不允许任何人通过该课程。

答案 4 :(得分:0)

您不能将表达式作为案例标签;它必须是编译时已知的常量值。

答案 5 :(得分:0)

一些语法错误以及在C中切换的要点只能用于整数和布尔值。

#include <iostream> 
using namespace std;

int main () 
{
    double testscore;
    cout << " Enter your test score and i will tell you the letter grade you earned ";
    cin >> testscore;
    if (testscore < 60.0) {
            cout << "your grade is F.\n";
    }
    else {
      cout << "that score isnt valid\n.";
    }

    return 0;
}

答案 6 :(得分:0)

switch要求括号中的整数表达式,而程序中不是这种情况。 每种情况也应该是整数或常量字符表达式。