cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : functions
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

post  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;
}
| Last edited on
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
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';
   else if (mark>=70)                 //No need to check <=79, as handled by previous if statement
      symbol = 'B';
   else if...
   ...
}

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

1
2
3
4
5
6
7
char returingChar()
{
char result;
......
......
return result;
}


|
DiptenduDas (82)
Ur logic for the same should be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char funcSymb(int mark)
{
char 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;
}


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);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

char funcSymb(int mark)
{
char 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 symb;

cout << "Enter mark out of 100: ";
cin >> mark;
symb = symbol(mark);
cout <<"Symbol corresponding to " << mark << " is " << symb << endl;

return 0;
}
| Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

char funcSymb(int mark)
{
char 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';
}


int main()
{
int mark;
char symb;

cout << "Enter mark out of 100: ";
cin >> mark;
symb = symbol(mark);
cout <<"Symbol corresponding to " << mark << " is " << symb << endl;

return 0;
}
|
Grey Wolf (536)
Line 29 should be: symb = funcSymb(mark);

and you should have return symbol; after line 18.
| Last edited on

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us