如何在cout语句中调用void函数?

时间:2016-01-23 19:08:32

标签: c++

我试图在同一行打印出标记和字母等级,但我无法在cout语句中调用void函数,有没有办法做到这一点?另外我理解我可以在cout语句之后的下一行调用它,但我需要它们在同一行上打印。

void printLetterGrade(float mark)
{
    float grade = mark;

    if (grade >= 90)
        cout << "Your Letter Grade is A+" << endl;

    else if ((grade >= 85) && (grade <= 89))
        cout << "Your Letter Grade is A" << endl;

    else 
        cout << "fail" << endl;

}

float calculateClassStats(float marks[], int length)
{
    const int arrayCount = length;
    for (int x = 0; x < arrayCount; x++){
        char letter = printLetterGrade(marks[x]);
        cout << marks[x] << letter << endl;
    }
    return 0;
}

6 个答案:

答案 0 :(得分:6)

您需要更改printLetterGrade,以便 返回某些内容。例如。

const char *printLetterGrade(float mark)
{
   if ( ... )
      return "A+";
   if ( ... )
      return "A";
   ...
}

(当然,此时,您可能还希望将该功能称为其他内容)

答案 1 :(得分:0)

在同一功能中打印

void printLetterGrade(float mark)
{
    float grade = mark;

    if (grade >= 90)
        cout <<mark<< "Your Letter Grade is A+" << endl;

    else if ((grade >= 85) && (grade <= 89))
        cout <<mark<< "Your Letter Grade is A" << endl;

    else 
        cout << "fail" << endl;

}

float calculateClassStats(float marks[], int length)
{
    const int arrayCount = length;
    for (int x = 0; x < arrayCount; x++){
        printLetterGrade(marks[x]);            
    }
    return 0;
}

答案 2 :(得分:0)

如果您想在同一行打印某些内容,请不要打印endl

void printLetterGrade(float mark)
{
    float grade = mark;

    if (grade >= 90)
        cout << "Your Letter Grade is A+" << endl;

    else if ((grade >= 85) && (grade <= 89))
        cout << "Your Letter Grade is A" << endl;

    else 
        cout << "fail" << endl;

}

float calculateClassStats(float marks[], int length)
{
    const int arrayCount = length;
    for(int x = 0; x < arrayCount; x++) {

        cout << marks[x] << " "; // don't print endl, just a space
        printLetterGrade(marks[x]); // goes on same line
    }
    return 0;
}

答案 3 :(得分:0)

您无法GET / GET /favicon.ico cout函数的结果或将void转换为void上要使用的内容,因为没有任何内容被返回。< / p>

如果您需要的只是函数cout的副作用,并且副作用恰好发生在printLetterGrade序列中的某个点,您可以将其返回更改为一些微不足道的行为不打印,

例如,您可以将返回类型从cout更改为voidchar *,并在您拥有string语句的任何地方返回空字符串""(如果有的话。

答案 4 :(得分:0)

Click Element

只需将返回类型更改为float calculateClassStats(float marks[], int length) { const int arrayCount = length; for (int x = 0; x < arrayCount; x++) { cout << marks[x] << printLetterGrade(marks[x]) << endl; } return 0; } ,然后将string更改为cout <<

return

输出

string printLetterGrade(float mark)
{
    float grade = mark;

    if (grade >= 90)
        return " Your Letter Grade is A+";

    else if ((grade >= 85) && (grade <= 89))
        return " Your Letter Grade is A";

    else 
        return " Fail";

}

答案 5 :(得分:-1)

将您的cout语句修改为

 cout<<printLetterGrade(marks[x])<<marks[x];

同时删除char letter = printLetterGrade(marks[x]);行,似乎没必要。

编辑:修改后的代码将是:

void printLetterGrade(float mark)
{
float grade = mark;

if (grade >= 90)
    cout << "Your Letter Grade is A+" << endl;

else if ((grade >= 85) && (grade <= 89))
    cout << "Your Letter Grade is A" << endl;

else 
    cout << "fail" << endl;

}

float calculateClassStats(float marks[], int length)
{
const int arrayCount = length;
for (int x = 0; x < arrayCount; x++){
    cout<<marks[x];
    printLetterGrade(marks[x]); 
}
return 0;
}