using functions

Hello guys, I have been working on this all weekend.
Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Design the following functions in the program:
● calcAverage—This function should accept five test scores as arguments and return the average of the scores.
Score Letter Grade
90–100 A
80–89 B
70–79 C
60–69 D
Below 60 F
and where do I put this system pause?

thanks for your help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  
#include <iostream>
#include <cstdlib>
 
using namespace std;
 
float calcAverage(int s1, int s2, int s3, int s4, int s5)
 {
        //Fill in code
        float averageScore, sum;
 
        sum = (s1 + s2 + s3 + s4 + s5);
 
        averageScore = sum / 5;
        
        return averageScore;
 
 }// end of function calcAverage 
Last edited on
I'm sorry. I don't understand the question. What does system pause have to do with what you've posted?
how many errors does your code generate?
I dnt get the semantics of what you have written.....I have the solution but i need to understand wat your errors are and your semantics...and system pause...why??
Last edited on
as far as i understand it. you want to make a program that takes 5 inputs and then gives them a grade.
so i have tried to solve it for you and give you something to compare to.

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
#include <iostream>
#include <conio.h>

using namespace std;

double calcAverage();
void showGrade();
double intArray[5] = {};

void main(){
	double userInput;
	for ( int i = 0 ; i<5 ; i++ ){
		cin >> userInput;
		if ( userInput < 100 && userInput > 0 ){
		intArray[i] = userInput;
		}
		
	}
	cout << "the average is: " << calcAverage() << endl;
	showGrade();
	getch();
	
}

double calcAverage(){
	double total = 0;
	for ( int i = 0 ; i<5 ; i++ ){
		total = total + intArray[i];
	}
	return (total / 5);
}

void showGrade(){
	for ( int i = 0 ; i<5 ; i++ ) {
		if ( intArray[i] >= 90 ){
			cout << intArray[i] << " equals grade A" << endl;
		}
		else if ( intArray[i] < 90 && intArray[i] >= 80 ){
			cout << intArray[i] << " equals grade B" << endl;
		}
		else if ( intArray[i] < 80 && intArray[i] >= 70 ){
			cout << intArray[i] << " equals grade C" << endl;
		}
		else if ( intArray[i] < 70 && intArray[i] >= 60 ){
			cout << intArray[i] << " equals grade D" << endl;
		}
		else if ( intArray[i] < 60 ) {
			cout << intArray[i] << " equals grade F" << endl;
		}
	}
}

i have written it fast and as easy as i could think off so there may be some bugs. have a look what i have made and try to understand it. and most important try things you like and experiment with it.

most of the code i have use is explained in the tutorials on this website and forum.

feel free to ask questions and use any code i just posted.

i hope this helps you with your problem.

also i dont understand the system pause. i use getch() to keep the cmd open to look at the results and when pressed enter or any other key (i think) the cmd closes.

sainsay

this is the output:

20
60
70
80
90
the average is: 64
20 equals grade F
60 equals grade D
70 equals grade C
80 equals grade B
90 equals grade A
closed account (iAk3T05o)
@sainsay: you are using 3 things that are not standard/bad practice.
1) global variables
2) void main()
3) #include <conio.h>
@Nathan2222: thanks for pointing out the mistakes i make. i am still a beginner a programing and C++ is only my second language. and my first, java, i have have not mastered. not even close to be really good at it.

1) global variables, i know it is not really smart to use them but i also tried to keep it more or less simple for people who have less knowledge and using functions that have some parameters might not be as easy to understand in my opinion. ( and i am kinda lazy )

2) int is can also be used for main can it? could you give me a example on how you would do it?

3) conio.h is used for getch() i don't know an easy way to make it different in my coding.
closed account (iAk3T05o)
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
#include <iostream>

double calcAverage (double scoreArr[])
{
double aveScore = 0;

for (int i = 1; i < 6; ++i)
{
aveScore += scoreArr[i] / 5;
}

std::cout << std::endl;

std::cout << "Average Score: " << aveScore << aveScore << std::endl;

return aveScore;
}

void getGrade (double scoreArr[])
{

for (int i = 1; i < 6; ++i)
{

if (scoreArr[i] >= 0 && scoreArr[i] <= 100)
{

if (scoreArr[i] >= 90 && scoreArr[i] <= 100)
{
std::cout << scoreArr[i] << " = A" << std::endl;
}

else if (scoreArr[i] >= 80 && scoreArr[i] <= 89.99)
{
std::cout << scoreArr[i] << " = B" << std::endl;
}

else if (scoreArr[i] >= 70 && scoreArr[i] <= 79.99)
{
std::cout << scoreArr[i] << " = C" << std::endl;
}  

else if (scoreArr[i] >= 60 && scoreArr[i] <= 69.99)
{
std::cout << scoreArr[i] << " = D" << std::endl;
}

else if (scoreArr[i] < 60)
{
std::cout << scoreArr[i] << " = F" << std::endl;
}

}

}

}

int main()
{

double score [6];

for (int i = 1; i < 6; ++i)
{
std::cout << "Enter score " << i << ": ";
std::cin >> score [i];
}

calcAverage (score);

std::cout << std::endl;

getGrade (score);

std::cout << std::endl;

system ("pause"); //this is where you put it but i never use it

return 0;
}


main() has to be int because a successful program has to return 0;.

If you don't understand anything in my code, feel free to ask, please.
Last edited on
Why is it that you dont have to declare your functions? Because they are above main()?
That feels strange for me. Because Java is very flexible with "public static void main ()."

I did not know that you could use arrays in this way. I will have a go tomorrow trying out the code that is newish to me. Everything else is understandable.

Thanks for the help.
Thanks for the help
@Nathan2222

Using system("pause") or any other system function is bad practice and should not be done.
Last edited on
closed account (iAk3T05o)
@jamerack: i know that. Why do you think i commented ". . . but i never use it". But i don't see any one line alternative to system ("cls") (hopefully it isn't in Qt).

@sainsay: it's a personal choice. Sometimes i create functions like you did (if a previous function uses the next function) or i do it like i did.
Glad you understood it and i actually learnt something from your code :).
For your problem, when I looked at the example, it didn't look as though it needed a board, and all you had to do was say that the piece moved forward.
@Nathan2222
As a replacement for system("CLS"), you could do something like so cout << "/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n/n";.
Last edited on
closed account (iAk3T05o)
@jamerack: that's not a replacement. It's inefficient and all it does is create multiple newlines whereas system("cls") clears the screen.
It serves the same purpose if you just want a clear screen
closed account (iAk3T05o)
It takes you to another part of the screen. Different from clearing the screen you are looking at without changing position.
It is a minor price to pay for not bogging down the system. I don't feel there is a need to mention how each system uses a different command for it. If you are using it for personal fun programming than it is fine, but if it is for commercial use, it is a big no-no. Either way, there will rarely be a time when C++ is used for GUI.
Last edited on
closed account (iAk3T05o)
I work on one system and no bogging down has occurred. Your method may bog the user down. Besides, it's a console thing. It's not like i have the slightest like for it. I'll soon move to Qt and start building gui apps. By then, i won't have to worry about std::cin, std::cout and the system("").
Maya, blender, luxrender etc. use Qt for their gui. Some game engines use C++ for their gui so C++ isn't rarely used.
Last edited on
We are agreeing on the subject, so I feel no need for the argument to continue.
Topic archived. No new replies allowed.