Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;int开关语句_C++_Switch Statement - Fatal编程技术网

C++ c++;int开关语句

C++ c++;int开关语句,c++,switch-statement,C++,Switch Statement,在过去的一个半小时里,我一直在做一个整数的转换,我知道如何用字符进行转换,但这对我来说似乎很难。任何建议都将被感激。我的问题是,我不能接受这个转换目前所做的超过100分的分数 int testScore; cout <<"Enter your test score and I will tell you \n"; cout <<"the letter grade you earned "; cin

在过去的一个半小时里,我一直在做一个整数的转换,我知道如何用字符进行转换,但这对我来说似乎很难。任何建议都将被感激。我的问题是,我不能接受这个转换目前所做的超过100分的分数

    int testScore;                     
    cout <<"Enter your test score and I will tell you \n";
    cout <<"the letter grade you earned ";
    cin >> testScore;

    switch(testScore/10)
{ 
    case 10:
    case 9:
        cout <<"Your grade is A.\n";
    break;
    case 8: 
        cout <<"Your grade is B.\n";
    break;
    case 7: 
        cout <<"Your grade is C.\n";
        break;
    case 6: 
            cout << "Your grade is D.\n";
        break;
    case 5: 
            cout << "Your grade is F.\n";
        break;

    default:
        cout << "That score isn’t valid\n";

    }
inttestscore;

cout您正在除以10.0,这是一个双精度数,无法编译。这必须更改为10。 此外,您应该在switch语句之前使用if语句检查它是否在有效范围内

#include<iostream>
#include<iomanip>
using namespace std;
int main() 
{

int testScore;
cout <<"Enter your test score and I will tell you \n";
cout <<"the letter grade you earned \n";
cin >> testScore;

if (testScore<=100 && testScore>=0)
    switch(testScore/10)
    {
        case 10:
        case 9:
            cout <<"Your grade is A.\n";
            break;
        case 8:
            cout <<"Your grade is B.\n";
            break;
        case 7:
            cout <<"Your grade is C.\n";
            break;
        case 6:
            cout << "Your grade is D.\n";
            break;
        case 5:
            cout << "Your grade is F.\n";
            break;

        default:
            cout << "That score isn’t valid\n";
    }
else
    cout <<"That score isn't valid\n";

return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
int测试分数;

我只是决定这样做,谢谢你的回答

int testScore;                     
    cout <<"Enter your test score and I will tell you \n";
    cout <<"the letter grade you earned ";
    cin >> testScore;

if (testScore >= 0 && testScore <=100)
{

        switch(testScore/10)
    { 
        case 10:
        case 9:
            cout <<"Your grade is A.\n";
        break;
        case 8: 
            cout <<"Your grade is B.\n";
        break;
        case 7: 
            cout <<"Your grade is C.\n";
            break;
        case 6: 
                cout << "Your grade is D.\n";
            break;
        default: 
                cout << "Your grade is F.\n";

        }

}

else 
    cout <<"That score isn't valid" << endl;
inttestscore;

cout您真的应该使用“if”而不是“switch”来执行此操作。类似于以下代码(未测试):

if(testScore>=0&&testScore=90)
等级=‘A’;
否则如果(测试分数>=80)
等级=‘B’;
否则如果(测试分数>=70)
等级=‘C’;
否则如果(测试分数>=60)
等级='D';
其他的
等级=‘F’;

我尝试了这个,它仍然显示了一个A级的数字100-109,这应该是一个注释,这不能回答这个问题。C++(和许多其他语言),将整数
109
除以整数
10
返回
10
,这将触发第一个
案例
。如果您不希望人们输入大于
100的数字,您需要在某个地方强制执行。例如,在您使用的
cin
行和
开关
状态之间t、 很公平。不过,在回答这个问题时,可以使用if语句if(testScore=0)如果为true,则执行switch语句;如果为true,则输出测试分数无效false@Borat.sagdiyev你应该把它放在你的答案中。一个浮点数上的开关,所以我不确定它如何能够接受超过100的分数。如果你需要检查值的范围,开关通常是错误的工作工具。你认为inpu会发生什么t为“90”?分数为110将导致
案例
值为11,除
默认
案例外,您没有考虑该值。
if (testScore >=0 && testScore <= 100)
{
    char grade;

    if (testScore >= 90)
        grade = 'A';
    else if (testScore >= 80)
        grade = 'B';
    else if (testScore >= 70)
        grade = 'C';
    else if (testScore >= 60)
        grade = 'D';
    else
        grade = 'F';

    cout << "Your grade is " << grade << endl;
}
else
{
    cout << "Score of " << testScore << " is not valid" << endl;
}