Calling a function with another function

Well our professor gave us a exam. I could not complete it so I have a 0 but I do want to actually learn how to do what he wanted us to acomplish.

This program is to calculate your % of 5 grades and the final grade

Enter grade 1 : (89) Input
Enter grade 2 : (90) Input
Enter grade 3 : (77) Input
Enter grade 4 : (100) Input
Enter grade 15: (100) Input

Your % and grade are: 92% - A
Do you want to exit the program?

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
75
76
77
78
79
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
int promedio(int n1, int n2, int n3, int n4, int n5);
//char nota ( int n1, int n2, int n3, int n4, int n5 ) ; 



int main()

{
string ans = "n";

int nota1, nota2, nota3, nota4, nota5;

do
{
cout << "Este programa es para calcular el promedio de 5 notas y luego escribir la nota final en letra."
<< endl << endl;

cout << "Entre la nota 1: ";
cin >> nota1;
cout << "Entre la nota 2: ";
cin >> nota2;
cout << "Entre la nota 3: ";
cin >> nota3;
cout << "Entre la nota 4: ";
cin >> nota4;
cout << "Entre la nota 5: ";
cin >> nota5;

cout << endl << "El promedio y la nota es el siguiente: " 
<< promedio(nota1,nota2,nota3,nota4,nota5) << nota << endl;



cout << "Desea repetir el programa? (s/n) : ";
cin >> ans;

} while (ans == "s" || ans == "S");
return 0;
}

int promedio (int n1, int n2, int n3, int n4, int n5)
{
int resultado;

resultado = (n1+n2+n3+n4+n5)/5;
return resultado;


}

/*char nota(int n1, int n2, int n3, int n4, int n5, char nota)
{
int resultado;
char nota;

resultado = (n1+n2+n3+n4+n5)/5;
return resultado;

if (resultado >= 90)
{return A;}

else if (resultado <= 89 && resultado >= 80)

{return B;}
else if (resultado <= 79 && resultado >= 70)

{return C;}
else if (resultado <= 69 && resultado >= 60)

{return D;}
else
	return F;
} */
Ahhh sorry I take this class in spanish because I live in Puerto Rico and my code is in spanish but I edited the example to english.

What he wants me to do is call the function promedio to calculate the grade % that was easy.

But after that he wants me to use another function so I can calculate the grade itself. It cant be a function inside another. But when I try to call the results of promedio VS2010 gives me a error. Arghhh mega headache >_>

Sorry for my bad english
thanks for your time
~Voyd
It is fine except for line 36.
 
<< promedio(nota1,nota2,nota3,nota4,nota5) << nota << endl;


The << nota after your call to promedia isn't necessary, and will cause an error because there is no variable called nota.
Ok I edited my code to this

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
int promedio(int n1, int n2, int n3, int n4, int n5);
char nota ( char nota ) ; 
int nota1, nota2, nota3, nota4, nota5;


int main()

{
string ans = "n";



do
{
cout << "Este programa es para calcular el promedio de 5 notas y luego escribir la nota final en letra."
<< endl << endl;

cout << "Entre la nota 1: ";
cin >> nota1;
cout << "Entre la nota 2: ";
cin >> nota2;
cout << "Entre la nota 3: ";
cin >> nota3;
cout << "Entre la nota 4: ";
cin >> nota4;
cout << "Entre la nota 5: ";
cin >> nota5;

cout << endl << "El promedio y la nota es el siguiente: " 
<< promedio(nota1,nota2,nota3,nota4,nota5) << nota << endl;



cout << "Desea repetir el programa? (s/n) : ";
cin >> ans;

} while (ans == "s" || ans == "S");
return 0;
}

int promedio (int n1, int n2, int n3, int n4, int n5)
{
int resultado;

resultado = (n1+n2+n3+n4+n5)/5;
return resultado;


}

char nota(char nota)
{
int resultado;
char A, B, C, D, F, nota ;

resultado = (nota1 + nota2 + nota3 + nota4 + nota5)/5;


if (resultado >= 90)
{
	nota =  A;
	return nota; 
}

else if (resultado <= 89 && resultado >= 80)
{
	nota = B;
    return nota;
}

else if (resultado <= 79 && resultado >= 70)
{
	nota =  C;
	return nota;
}

else if (resultado <= 69 && resultado >= 60)
{
	nota =  D;
    return nota;
}

else
{
	nota = F;
	return  nota;}

} 


But I get this error in VS2010 " errorC2082: redefinition of formal parameter 'nota' line 63 "
In your declaration for char nota(char nota); and its definition, leave the "( )" blank. Also, when you call it on line 36, you need to put "( )" after "<< nota". Otherwise, you are accessing the memory address of the function, which I don't think is what you want =)

EDIT: Also, on line 60, you don't need to make separate variables to store each grade. Just put:
1
2
3
nota = 'A';
//instead of
nota = A;

Those variables aren't initialized anyways, you would get some garbage value and not what you were expecting.

EDIT EDIT: Actually, change the "char nota(char nota)" to "char nota (int nota)". When you call nota on line 36, put the grade inside of the "( )" when you call nota.

If I were you, I would change:
1
2
cout << endl << "El promedio y la nota es el siguiente: " 
<< promedio(nota1,nota2,nota3,nota4,nota5) << nota << endl;

to...
1
2
int somename = promiendo(nota1, nota2, nota3, nota4, nota5);
cout << somename << nota(somename) << endl;
Last edited on
Thanks ModShop :D great help!!
Heres a practice excersise that is included in the book we are using. In my opinion it should work but I dont know why when I compile it gives me a weird error >_>

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <conio.h>

using namespace std;
int cResults (int n1);
int fResults (int n2); 
int n1, n2;

int main()
{
string ans; 
int menu;
float fahrenheit, celcius;

cout << "This application converts Fahrenheit to Celcius and Vice Versa." << endl <<endl;

do 
{
cout << "-----MENU-----" << endl;
cout << "[0] - Exit" << endl;
cout << "[1] - Convert from Fahrenheit to Celcius" << endl;
cout << "[2] - Convert from Celcius to Fahrenheit" << endl;
cout << "Option: ";
cin >> menu;

if (menu == 1) 
{ 
//Input
cout << "Enter degrees in Fahrenheit: " ;
cin >> n1;
cout << " " << endl;


//Display C
cout << "The temperature in Celcius is " << cResults(int n1) << endl; 
cout << " " << endl;
cout << " " << endl;
}

else if (menu == 2) 
{
//Input
cout << "Enter degrees in Celcius: " ;
cin >> n2;
cout << " " << endl;


//Display F
cout << "The temperature in Fahrenheit is " << fResults(int n2) << endl; 
cout << " " << endl; 
cout << " " << endl;
}

else 

{
cout << "Do you want to exit the program? (y/n) : ";
cin >> ans;
cout << " " << endl; 
cout << " " << endl;
}
}
while (ans != "y" && ans != "Y");

return 0;
}

cResults(int n1)
{

//Calculate C
int resultado;
resultado = (n1 - 32) * 0.55 ;
return resultado;

}

fResults(int n2)
{

//Calculate F
int resultado;
resultado = (n2 * 1.8) + 32 ;
return resultado;
} 
You're not using your functions properly. Read this:
http://www.cplusplus.com/doc/tutorial/functions/
Also, n1 and n2 should not be global. They belong inside main()
Thanks for the help will read the article :)
Topic archived. No new replies allowed.