如何为无效输出设置代码 [英] How do i set my code up for a invalid output

查看:70
本文介绍了如何为无效输出设置代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个程序,当你输入一个0到100之间的数字时,它给你一个字母等级和平均值。我缺少一个用户输入0到100之外的数字。输出假设看起来像这个



示例输出1(输入无效,输入用粗体斜体表示):

GPA转换

- - - - - - -

输入你的成绩: -10.2

你还没有输入0到100之间的成绩。结束课程。



我试过的每一个代码都不会导致如果我将无效数字设置为低于0或超过100,我认为可以达到以上目的。



我尝试过:



  #include   <   iostream  >  
#include < 字符串 >
使用 命名空间标准;

int main()
{

float 等级;
cout.precision( 2 );

cout<< GPA转换<< ENDL;
cout<< --------------<< ENDL;
cout<< 输入您的成绩:;
cin>>年级;
cout<< ENDL;

if (等级< = 100 && grade> = 96
cout<< 您将收到一个字母等级A,其GPA为4.00。;

if (等级< = 95 && grade> = 90
cout<< 您将收到字母等级A-,GPA为3.70。;

if (等级< = 89 && grade> = 87
cout<< 您将收到B +的字母等级,其GPA为3.30。;

if (等级< = 86 && grade> = 84
cout<< 您将收到B级的字母等级,其GPA为3.00。;

if (等级< = 83 && grade> = 80
cout<< 您将收到字母等级为B-,GPA为2.70。;

if (等级< = 79 && grade> = 77
cout<< 您将收到C + + 2.30 GPA的字母等级。;

if (等级< = 76 && grade> = 74
cout<< 您将收到一个字母等级为C且2.00 GPA。;

if (等级< = 73 && grade> = 70
cout<< 您将收到一个C级的字母等级,其GPA为1.70。;

if (等级< = 69 && grade> = 67
cout<< 您将收到D +的字母等级,其GPA为1.30。;

if (等级< = 66 && grade> = 64
cout<< 您将收到一个字母等级为D且1.00 GPA。;

if (等级< = 63 && grade> = 60
cout<< 您将收到D-的字母等级,0.70 GPA。;

if (等级< = 59 && grade> = 0
cout<< 您将收到一个字母等级为F且0.00 GPA。;






return 0 ;
}

解决方案

参见语句和流程控制 - C ++教程 [ ^ ]了解选择语句 if else


< blockquote>'find right interval'通常以这种方式实现:



  int 等级;  //  查看代码,成绩应为整数。  
// ...
if (grade> 100
{ // 错误,输入超出范围,太高
}
其他 if (grade> = 96
{ / / 这处理96< = grade< = 100
cout<< 您将收到一个字母等级A,其GPA为4.00。;
}
其他 如果(等级> = 90
{ // 这处理90< = grade< = 95
// ..
}
// ..
else 如果(等级> = 0
{ // 这处理0< = grade< = 59
}
else
{ // 错误,输入超出范围,太低
}


i made a program that gives you a letter grade and average when you enter a number from 0 to 100. I'm missing one piece where the user inputs a number outside of 0 to 100. the output is suppose to look like this

Example output 1 (invalid input, input is in bolded italics):
GPA Conversion
- - - - - - -
Enter your grade: -10.2
You have not entered an grade between 0 and 100. Ending program.

every code i tried doesn't result in what im suppose to get above if i put an invalid number below 0 or over 100.

What I have tried:

 #include <iostream>
 #include <string>
 using namespace std;

int main()
{

    float grade;
    cout.precision(2);

    cout << "GPA Conversion" << endl;
    cout << "--------------" << endl;
    cout << "Enter your grade: ";
    cin >> grade;
    cout << endl;

    if ( grade <=100 && grade  >=96)
        cout << "You will receive a letter grade of A with a 4.00 GPA.";

    if ( grade <=95 && grade  >=90)
        cout << "You will receive a letter grade of A- with a 3.70 GPA.";

    if ( grade <=89 && grade  >=87)
        cout << "You will receive a letter grade of B+ with a 3.30 GPA.";

    if ( grade <=86 && grade  >=84)
        cout << "You will receive a letter grade of B with a 3.00 GPA.";

    if ( grade <=83 && grade  >=80)
        cout << "You will receive a letter grade of B- with a 2.70 GPA.";

    if ( grade <=79 && grade  >=77)
        cout << "You will receive a letter grade of C+ with a 2.30 GPA.";

    if ( grade <=76 && grade  >=74)
        cout << "You will receive a letter grade of C with a 2.00 GPA.";

    if ( grade <=73 && grade  >=70)
        cout << "You will receive a letter grade of C- with a 1.70 GPA.";

    if ( grade <=69 && grade  >=67)
        cout << "You will receive a letter grade of D+ with a 1.30 GPA.";

    if ( grade <=66 && grade  >=64)
        cout << "You will receive a letter grade of D with a 1.00 GPA.";

    if ( grade <=63 && grade  >=60)
        cout << "You will receive a letter grade of D- with a 0.70 GPA.";

    if ( grade <=59 && grade  >=0)
        cout << "You will receive a letter grade of F with a 0.00 GPA.";






    return 0;
}

解决方案

See Statements and flow control - C++ Tutorials[^] to learn about the selection statements if and else.


The 'find right interval' is usually implemented this way:

int grade; // looking at you code, grade should be an integer. 
//...
if (grade > 100) 
{// error, input out of range, too high
}
else if (grade >= 96) 
{// this handles 96 <= grade <= 100
  cout << "You will receive a letter grade of A with a 4.00 GPA.";
}
else if (grade >= 90)
{// this handles 90 <= grade <= 95
 //..
}
//..
else if ( grade >= 0)
{ // this handles 0 <= grade <= 59
}
else
{ // error, input out of range, too low
}


这篇关于如何为无效输出设置代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆