-4

我已经纠正了下面程序中的错误,但仍然得到混合输出。更新的结果如下。请指教。

我将不胜感激任何帮助。相关的代码部分是:

      string student::standing() const
      // post: use selection to return the current standing as either
      //       Freshman, Sophomore, Junior, or Senior.

      {    

          cout  << "Freshman";

      if (my_credits <= 60.00);
      {cout  << "Sophomore";}

      if (my_credits <= 90.00);
      {cout  << "Junior";}

      if (my_credits >= 90.00);
       else
       { cout  << "Senior";}

      }

      string student::name() const
      // post: return the student's name
      {
          {return my_name;}

      }
4

1 回答 1

2

Uhm, first of all your title references one error and your question shows another. Both are legitimate so let's fix both, shall we?

The thing is the C4716 error message tells you exactly what the error is: you just have to read it. It says that the function student::standing() claims to return a string but doesn't. Of course, that's not the only issue. The other problem, which you claim is C2134 (but is likely C4390) is caused because you you put a semicolon after you if which isn't right. This code:

if(my_credits <= 60.00);
{cout  << "Sophomore";}

is the same as this code:

if(my_credits <= 60.00)
    /* do nothing if my_credits are less than or equal to 60 */

{
    /* and then always print "Sophomore" */
    cout << "Sophomore";
}

Chances are that what you really want is this:

string student::standing() const
{    
    if(my_credits >= 90.00)
        return "Senior";

    if(my_credits >= 60.00)
        return "Junior";

    if(my_credits >= 30.00)
        return "Junior";

    return "Freshman"; 
}

For future reference, if you had searched on Google for C4390, you would have landed on an MSDN page describing this error which includes an exact description of the issue, along with the very mistake you made.

Similarly, if you had Googled for C4716, you would have landed on another MSDN page describing that error which again includes an example that's very close to what your mistake.

Finally, in this code:

string student::name() const
{
    {return my_name;}
}

What is the point in those extra curly braces? They don't really hurt anything, but they just serve no conceivable purpose, except to confuse. Remove them:

string student::name() const
{
    return my_name;
}
于 2013-06-01T01:36:19.033 回答