functions
| velaphi (30) | |||
| 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; return 0; } | |||
| Faldrax (181) | |||
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
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'. | |||
| DiptenduDas (82) | |||
| Hi, If you want to return character from a function then the return type of the same should be char. like
| |||
| DiptenduDas (82) | |||
Ur logic for the same should be
Also U need to take care
symb = symbol(mark);as symb is not defined. | |||
| velaphi (30) | |||
Thanx guys, the function is perfect now. How can i fix this statement symb symb = symbol(mark);
| |||
| DiptenduDas (82) | |||
| U are trying to call a method which U have not defined rather U have defined the method in the name funcSymb. So do accordingly... | |||
| velaphi (30) | |||
would you kindly help me to fix this please. It says symbol is not declared.
| |||
| Grey Wolf (536) | |||
Line 29 should be:
symb = funcSymb(mark);and you should have
return symbol; after line 18. | |||
This topic is archived - New replies not allowed.
