Reputation: 21
I am trying to print out the mark and the letter grade on the same line but I am unable to call a void function in the cout statement, is there a way to do this? Also I understand I can call it on the next line after the cout statement but I need them to print on the same line.
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;
}
Upvotes: 2
Views: 10710
Reputation: 5950
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;
}
Just change the return-type to string
, and change cout <<
to 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";
}
Output
95 Your Letter Grade is A+
88 Your Letter Grade is A
50 Fail
Upvotes: 0
Reputation: 129434
You need to change the printLetterGrade
such that it does return something. E.g.
const char *printLetterGrade(float mark)
{
if ( ... )
return "A+";
if ( ... )
return "A";
...
}
(Of course, at this point, you may want to call the function something else too)
Upvotes: 6
Reputation: 22520
You can not cout
the result of a void
function or cast the void
to something to be used on the cout
, since there is nothing being returned.
If all you needed is the side effect of your function printLetterGrade
and that the side effect take place exactly at a certain point in the cout
sequence, you can change its return to something trivial that does not print,
e.g., You can change the return type from void
to char *
or string
and return an empty string ""
wherever you had a return;
statement (if any).
Upvotes: 0
Reputation: 48635
If you want to print something on the same line, just don't print the 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;
}
Upvotes: 0
Reputation: 1421
modify your cout statement as
cout<<printLetterGrade(marks[x])<<marks[x];
Also remove the char letter = printLetterGrade(marks[x]);
line, it seems unnecessary.
Edit: the modified code will then be:
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;
}
Upvotes: -1
Reputation: 490
Print both in same function
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;
}
Upvotes: 0