Help with homework

The question for the program is


Write a program that uses a loop to ask the user to input a total of 5 test scores between 0 and 100 inclusive. Test each input and do not accept numbers out of range. Add each valid test score to a total and compute the average to 2 decimal places after exiting the loop. Display the average score.

This sounds so simple to do but I messed it up. I need to create a loop for inputting the test scores and use a loop to test the scores to ensure theyre between 0-100. I'm posting the program I made with my teachers comments on it.



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
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	//define variables 
	double num1, num2, num3, num4, num5;
	int minscore = 0;
	int maxscore = 100;
	//first test value
	//**** teacher input **** There should be an outer loop here for
	//inputting the scores
	cout << "Enter the first test score ";
	cout << "\n(Value within the range ";
	cout << minscore << " - " << maxscore << "): ";
	cin >> num1;
	//validate
	//**** teacher input **** This is ok for validating the input
	while ((num1 < 1) || (num1 > 100))
	{
		cout << "\nTest scores should be between " << minscore << " - " << maxscore;
		cout << "\nEnter the first test score: ";
		cin >> num1;
	}
	//second test value
	// *** Dont need 
	cout << "\nEnter the second test score ";
	cout << "\n(Value within the range ";
	cout << minscore << " - " << maxscore << "): ";
	cin >> num2;
	//validate
	while ((num2 < 1) || (num2 > 100))
	{
		cout << "\nTest scores should be between " << minscore << " - " << maxscore;
		cout << "\nEnter the second test score: ";
		cin >> num2;
	}
	//third test value
	// *** Dont need
	cout << "\nEnter the third test score ";
	cout << "\n(Value within the range ";
	cout << minscore << " - " << maxscore << "): ";
	cin >> num3;
	//validate
	while ((num3 < 1) || (num3 > 100))
	{
		cout << "\nTest scores should be between " << minscore << " - " << maxscore;
		cout << "\nEnter the third test score: ";
		cin >> num3;
	}
	//fourth test value
	// *** Dont need
	cout << "\nEnter the fourth test score ";
	cout << "\n(Value within the range ";
	cout << minscore << " - " << maxscore << "): ";
	cin >> num4;
	//validate
	while ((num4 < 1) || (num4 > 100))
	{
		cout << "\nTest scores should be between " << minscore << " - " << maxscore;
		cout << "\nEnter the fourth test score: ";
		cin >> num4;
	}
	//fifth test value
	// *** Dont need
	cout << "\nEnter the fifth test score ";
	cout << "\n(Value within the range ";
	cout << minscore << " - " << maxscore << "): ";
	cin >> num5;
	//validate
	while ((num5 < 1) || (num5 > 100))
	{
		cout << "\nTest scores should be between " << minscore << " - " << maxscore;
		cout << "\nEnter the fifth test score: ";
		cin >> num5;
	}
	// *** Above not needed, once done with validation add to total
	//finding average
	double addedscore = num1 + num2 + num3 + num4 + num5; // *** End outer loop
	double avgscore = addedscore/5;
	//set precision
	cout << fixed << showpoint << setprecision(2);
	//display average score
	cout << "\nThe average score of the inputted test scores is: " << avgscore << "%. ";
	
	return 0;
}
Last edited on
Hello wubbits,

The first question I have is was this code given to you to use and make work or can it be changed?

Second question. Do you know about functions yet?

Andy

Edit:
Last edited on
Hello Andy,

I wrote this code without knowing that I had to write a loop for the inputs. So it is able to be changed.

Yes I know what functions are and how to use them on a lower understanding, but I am not allowed to use them because this is from a chapter before learning of functions. I'm not allowed to use more advanced code than what was part of this chapter and those prior.

I dont understand what he means I need an outer loop for the inputs.


I need to create a loop for inputting the test scores and use a loop to test the scores to ensure theyre between 0-100.

Are you sure about two loops?

To me it looks like:
* Get 5 good values
* Compute something

Add each valid test score to a total and compute the average to 2 decimal places after exiting the loop.

No need to actually store the values. Only the total counts.

double total = 0.0
size_t count = 0
WHILE we don't have 5 good values
  read value
  IF value is good
  THEN add value to total and increment count

use total


For example:
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
#include <iostream>

class Foo {
  double total {};
  size_t count {};
public:
  void add( double val ) {
    total += val;
    ++count;
  }
  size_t bar() const { return count; }
  double gaz() const { return total; }
};

bool valid( double x ) {
  return true; // surely not always?
}

int main() {
  Foo foo;
  double value {};
  while ( foo.bar() < 5 && std::cout << "Hit me " && std::cin >> value ) {
    if ( valid( value ) ) foo.add( value );
  }
  std::cout << foo.gaz();
}

(That obviously is not the code that you are looking for. The question is, do you see anything useful in it?)
Last edited on
Hello keskiverto,

I'm not really sure what to look at, I'm still relatively new to c++. I understand you used a function within it but I cant use a function for this program I am trying to figure out. This chapter is only basic things and this chapter focuses on loops. I just dont fully understand the difference between some loops. And I understand the idea of nested loops but it eventually gets way too confusing for me to follow.


I suppose you can use array in you code. If you can't use arrays than the code will get lengthy.
This is the code I can come up with :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std ;
int main ()
{
	int marks[5] ;
	double sum=0 , average=0 ;
	for (int i=0 ; i<5 ; i++)
	{
		cout << "Enter Marks of Student " << i+1 << " : " ;
		cin >> marks[i] ;
		if (marks[i]>0 && marks[i]<=100)
		{
			sum=sum+marks[i] ;
		}
		else 
		{
			cout << "Invalid Marks Entered !!!" << endl ;
			i-- ;
		}
	}
	average=sum/5 ;
	cout << endl << "Average : " << average ;
}


And heres a sample output :

Enter Marks of Student 1 : 23
Enter Marks of Student 2 : 46
Enter Marks of Student 3 : 0
Invalid Marks Entered !!!
Enter Marks of Student 3 : -5
Invalid Marks Entered !!!
Enter Marks of Student 3 : 24
Enter Marks of Student 4 : 345
Invalid Marks Entered !!!
Enter Marks of Student 4 : 23
Enter Marks of Student 5 : 103
Invalid Marks Entered !!!
Enter Marks of Student 5 : 100

Average : 43.2
Hello Hammad Ali,

Thank you for the help. The idea of the for loop with the if/else worked for it.

This code worked to input and find the average properly within the parameters. Thank you all.

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
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	double total, average;
	for(int counter = 0; counter < 5; counter++)
	{
		int num = 0;
		cout << "Enter a test score (1-100): " << endl;
		cin >> num;
		if(num > 0 && num <=100)
		{
			total +=num;
		}
		else
		{
			cout << "Invalid test score" << endl;
			counter--;
		}
	}
	average = total / 5;
	cout << fixed << setprecision(2);
	cout << "The average of all the test scores is " << average << "% " << endl;
	return 0;
}


This code worked to input and find the average properly within the parameters.

What happens if the user enters something like "joke"?

Topic archived. No new replies allowed.