使用if和else语句的c ++运行时错误

时间:2013-01-09 18:57:26

标签: visual-c++

所以这个程序可以根据你输入的百分比告诉你你的成绩,我基本上只使用if和else语句对它进行编码。该程序一直工作到59%,然后当我输入任何超过该百分比的东西时,它不起作用。因为在程序中只是不会告诉我59%之后的成绩。没有任何东西可以帮助!!!

P.S。我知道可能有一种更简单的方法可以让我编程,但我想练习if和else语句......

//
//exercise 1.
//you will enter in your percent and it will anounce your grade.
//create a program so that it will notify the user of their letter grade
//0-59 F 60-69 D 70-79 C 80-89 B 90-100 A
//a=user input
//For whatever reason the program seems to only work up to 59% and after that it doesn't work.



int a;
#include <iostream>
using namespace std;
int main()
{
cout<< "Enter in the percent of your grade and \n I will tell you your grade"<<endl;
cin>>a;
if(a==100)
{
    cout<<"you scored a perfect A";
}
else 
    if(a<=59)
    {
        if(a<0)
        {
            cout<<"your really stupid";
        }
        else
            cout<<"you failed";
    }
    else
        if(a>=60)
        {
            if(a<=69)
            {
                cout<<"You got a D";
            }
        }
        else
            if(a>=70)
            {
                if(a<=79)
                {
                    cout<<"you got a C";
                }
            }
            else
                if(a>=80)
                {
                    if(a<=89)
                    {
                        cout<<"you got a B";
                    }
                }
                else
                    if(a>=90)
                    {


                            cout<<"you got an A";

                    }


}

3 个答案:

答案 0 :(得分:1)

问题是你正在检查一个&gt; = 60 ......它是不是(假设a = 75)。但是,它没有被任何其他条件语句捕获。通过以下代码中的评论更好地解释。

#include <iostream>
using namespace std;

int a;
int main()
{
cout<< "Enter in the percent of your grade and \n I will tell you your grade"<<endl;
cin>>a;
if(a==100)
{
    cout<<"you scored a perfect A";
}
else 
    if(a<=59)
    {
        if(a<0)
        {
            cout<<"your really stupid";
        }
        else
            cout<<"you failed";
    }
    else
        if(a>=60) // 75 >= 60
        {
            if(a<=69) // But 75 is > 69
            {
                cout<<"You got a D";
            }
        }
        // ONLY REACHES THIS POINT IF a < 60
        else
            if(a>=70)
            {
                if(a<=79)
                {
                    cout<<"you got a C";
                }
            }
            else
                if(a>=80)
                {
                    if(a<=89)
                    {
                        cout<<"you got a B";
                    }
                }
                else
                    if(a>=90)
                    {
                            cout<<"you got an A";

                    }
}

这可能是一种更好的方法:

int main()
{
    cout<< "Enter in the percent of your grade and \n I will tell you your grade"<<endl;
    cin>>a;
    if(a==100)
    {
        cout<<"you scored a perfect A";
    }
    else 
    {
        if(a<=59)
        {
            if(a<0)
            {
                cout<<"your really stupid";
            }
            else
                cout<<"you failed";
            }
        else
        {
            if (a >= 60 && a <= 69)
            {
                cout << "D";
            }
            else if (a >= 70 && a <= 79)
            {
                cout << "C";
            }
            else if (a >= 80 && a <= 89)
            {
                cout << "B";
            }
            else
            {
                cout << "A";
            }
        }
    }
}

答案 1 :(得分:0)

我认为你在第一个之后缺少括号。

试试这个:

//
//exercise 1.
//you will enter in your percent and it will anounce your grade.
//create a program so that it will notify the user of their letter grade
//0-59 F 60-69 D 70-79 C 80-89 B 90-100 A
//a=user input
//For whatever reason the program seems to only work up to 59% and after that it doesn't work.



int a;
#include <iostream>
using namespace std;
int main()
{
cout<< "Enter in the percent of your grade and \n I will tell you your grade"<<endl;
cin>>a;
if(a==100)
{
    cout<<"you scored a perfect A";
}
else
{ 
    if(a<=59)
    {
        if(a<0)
        {
            cout<<"your really stupid";
        }
        else
            cout<<"you failed";
    }
    else
        if(a>=60)
        {
            if(a<=69)
            {
                cout<<"You got a D";
            }
        }
        else
            if(a>=70)
            {
                if(a<=79)
                {
                    cout<<"you got a C";
                }
            }
            else
                if(a>=80)
                {
                    if(a<=89)
                    {
                        cout<<"you got a B";
                    }
                }
                else
                    if(a>=90)
                    {


                            cout<<"you got an A";

                    }

}
} // end main

答案 2 :(得分:0)

在您的代码中,一旦到达if(a>=60),如果a的值不小于70,则不输出任何内容,并且代码退出条件,因为它找到了真实的情况。至于为什么你不能让它在59以上做任何事情,我不知道,因为它看起来应该有效。但是,通过将内部if块与其父项组合,您可以获取代码以检查a是否在10个百分点的组内,如果它不在该组中,则转到下一个。以下是如何执行此操作的示例。

//
//exercise 1.
//you will enter in your percent and it will anounce your grade.
//create a program so that it will notify the user of their letter grade
//0-59 F 60-69 D 70-79 C 80-89 B 90-100 A
//a=user input
//For whatever reason the program seems to only work up to 59% and after that it doesn't work.



int a;
#include <iostream>
using namespace std;
int main()
{
  cout<< "Enter in the percent of your grade and \n I will tell you your grade"<<endl;
  cin>>a;
  if(a==100)
  {
      cout<<"you scored a perfect A";
  }
  else if(a<=59)
  {
      if(a<0)
      {
          cout<<"your really stupid";
      }
      else
      {
          cout<<"you failed";
      }
  }
  else if(a>=60 && a<=69)
  {
      cout<<"You got a D";
  }
  else if(a>=70 && a<=79)
  {
      cout<<"you got a C";
  }
  else if(a>=80 && a<=89)
  {
      cout<<"you got a B";
  }
  else if(a>=90)
  {
      cout<<"you got an A";
  }
}