Hi guys, i am trying to write a function that will return character
'F' if mark is 0 - 39
'E' if mark is 40 - 49
'D' if mark is 50 - 69
'C' if mark is 60 - 69
'B' if mark is 70 - 79
'A' if mark is 80 - 100
#include <iostream>
using namespace std;
void funcSymb(int mark, int symbol)
{
if (mark >= 80 && mark == 100)
symbol = 'A';
else if (mark >= 70 && mark == 79)
symbol = 'B';
else if (mark >= 60 && mark == 69)
symbol = 'C';
else if (mark >= 50 && mark == 59)
symbol = 'D';
else if (mark >= 40 && mark == 49)
symbol = 'E';
else
symbol = 'F';
return symbol;
}
int main()
{
int mark;
char symbol;
cout << "Enter mark out of 100: ";
cin >> mark;
symb = symbol(mark);
cout <<"Symbol corresponding to " << mark << " is " << symb << endl;
In order to get a function to return a value (a character in this case) you need to declare the function to be of that type. char funcSymb(int mark)
In the function you can then either declare a local varaible to build the result, or have a return statement each time you determine the return value. Personally I find the former is clearer, but there is an element of personal taste here.
Options
1 2 3 4 5 6 7 8 9 10
char funcSymb(int mark)
{
char symbol;
if (mark >=80 && mark <=100) //NB <= 100 not ==100
symbol = 'A';
elseif (mark>=70) //No need to check <=79, as handled by previous if statement
symbol = 'B';
elseif...
...
}
or
code]
char funcSymb(int mark)
{
if (mark >=80 && mark <=100)
return 'A';
else if (mark>=70)
return 'B';
else if...
}
[/code]
You also need to change the char symbol in main to char symb as that is the variable you are using.
You may want to include a specific check and result for mark >100, as with your code it would result in a 'F', and in mine a 'B'.