Help with functions

The professor wants us to create a program that displays the Average of 3 Grades. Each input of the grades must be stored with a function and then the Result of the Average is to be displayed with a void. I ask some questions but he answers that he cannot help with my logic in the programming :/

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <iomanip> 

using namespace std; 
int firstgrade(int a); 
int secondgrade(int b);
int thirdgrade(int c);

void result ();

int main() 
{ 
  string ans = "n"; 
  
   do 
   { 
    cout << "This program will calculate the Average of 3 Grades" << endl << endl; 
        cout << firstgrade;
	cout << secondgrade;
	cout << thirdgrade;
    


    cout << result; 
    cout << "Would you like to repeat the program? (s/n) : "; 
    cin >> ans; 
} while (ans == "s" || ans == "S"); 

return 0;
 
} 

int firstgrade (int a) 
{ 
	int grade1;
	cout << "Enter the first grade :";
	cin >> grade1;

	return grade1; 
}

int segundanota (int b)
{
	int grade2;
	cout << "Enter the first grade :";
	cin >> grade2;

	
	return grade2; 
}

int terceranota (int c)
{
	int grade3;
	cout << "Enter the first grade :";
	cin >> grade3;

	return grade3; 
}

void result ()
{
	int result;
	result = primeranota+segundanota+terceranota/3;
    cout << endl << "The average of the 3 Grades is : " << result << endl;
}

Last edited on
You're using the function wrong.

What's your question?
Last edited on
Well I am not even sure that I can pass the value of one function by calling it in the void function to do the Average. My question is how would I be able to use the function correctly to achieve the result. I have read alot but none gives me a correct idea to use the function in that way.
1
2
3
4
5
6
cout << "This program will calculate the Average of 3 Grades" << endl << endl; 
   a=firstgrade();
b=secondgrade();
c=thirdgrade();

result(a,b,c)


this will solve your problem just use it properly
I still get error that the function does not return a value. Is it because I am using cin inside a int function ?
post your code
I think you need to read up on calling functions. You are using the address of the function not calling the function with the parameters required.
http://www.cplusplus.com/doc/tutorial/functions/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <iomanip> 

using namespace std; 
int firstgrade(); 
int secondgrade();
int thirdgrade();

void result ();

int main() 
{ 
  int a,b,c;
  string ans = "n"; 
  
   do 
   { 
    cout << "This program will calculate the Average of 3 Grades" << endl << endl; 
        cout << firstgrade();
	cout << secondgrade();
	cout << thirdgrade();
    
       a=firstgrade();
       b=secondgrade();
       c=thirdgrade();


    cout << result(int a, int b, int c); 
    cout << "Would you like to repeat the program? (s/n) : "; 
    cin >> ans; 
} while (ans == "s" || ans == "S"); 

return 0;
 
} 

int firstgrade () 
{ 
	int grade1;
	cout << "Enter the first grade :";
	cin >> grade1;

	return grade1; 
}

int secondgrade ()
{
	int grade2;
	cout << "Enter the first grade :";
	cin >> grade2;

	
	return grade2; 
}

int thirdgrade ()
{
	int grade3;
	cout << "Enter the first grade :";
	cin >> grade3;

	return grade3; 
}

void result (int a, int b, int c)
{
	int result;
	result = a+b+c/3;
    cout << endl << "The average of the 3 Grades is : " << result << endl;
}

Last edited on
read the tutorial the guy gave you in the link that will help you a lot
on line 30 you are trying to declare 3 new integers. You don't pass the data type just the variable names/values.
Yes I am reading that link thanks alot. But if the value is being set from within the function then the functions () will stay blank?
Oh yes I see the error on line 30 thanks alot.
I cant figure out why all 3 functions have the same error that they do not return a value. I am not sure if it is because the cin is inside the function or if it is a syntax error. Will continue to read on functions though.
Well what are you trying to do for line 21-23 anyways? Also why do all your functions say enter your first grade?

You might as well just call 1 function 3 times :P

Oh yes sorry that was a error when translating from spanish to english. The program must accept 3 grades to calculate the Average GPA in the void function. But each grade must be within a different function. If not I would just accept the 3 grades with a space and add them to 1 function only.
1
2
3
4
5
6
7
8
9
10
11
cout << "This program will calculate the Average of 3 Grades" << endl << endl; 

    
       a=firstgrade();
       b=secondgrade();
       c=thirdgrade();


   result(a, b, c); 
    cout << "Would you like to repeat the program? (s/n) : "; 
    cin >> ans; 
Yes wwwiii I changed the code to that way because then I would repeat the same if I used it the way I had it. But VB still gives me same error for the 3 functions. It says that the functions are not returning value.
I found the Error was a VB error not a code Error. After exporting the proyect to a new proyect it worked out fine! Thanks alot :)
no problem
Topic archived. No new replies allowed.