Need help with a loop I made.

So, the assignment I have is to repeatedly ask a user if they would like to add tests and then create the average for the scores combined that they have added. Everything is compiling and running smooth, however the average I'm getting is wrong. I can't seem to figure out what to change. Please help and thank you!

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
 
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>

using namespace std;

int main ()
{
        int n, count, a;
        float sum, avg;

        sum = 0;
        cout<<"How many test scores would you like to enter?:";
        cin>>a;
        cout<< "Enter score: " ;
        cin>> n;

        char choice;
        for(count=1; count<=n; count++) {
                cout<< "Would you like to enter another score? (y/n): ";
                cin>>choice;
        if(choice =='y')


        {
                cout<< "Enter score: ";
                cin>>n;
        if ( cin.fail() || n<1 || n>100) {
                cout<< "Please enter a number 1-100!\n";
        }
        sum = sum + n;
        }
        else break;
        }

        avg = sum / n; //adds up the test scores and creates an average.
        cout << "The average test score is " << avg <<endl;




        return 0;
        }


Thank you in advance!
closed account (48T7M4Gy)
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
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>

int main()
{
	int n = 0, count, a; ///
	float sum, avg;

	sum = 0;
	std::cout << "How many test scores would you like to enter?:";
	std::cin >> a;
	/// std::cout << "Enter score: ";
	///std::cin >> n;

	char choice;
	for (count = 0; count < a; count++) { ///
		/// std::cout << "Would you like to enter another score? (y/n): ";
		/// std::cin >> choice;
		/// if (choice == 'y')
		{
			std::cout << "Enter score: ";
			std::cin >> n;
		///	if (std::cin.fail() || n < 1 || n>100) {
				std::cout << "Please enter a number 1-100!\n";
		///	}
			sum = sum + n;
		}
		//else break;
	}

	avg = sum / a; ///
	std::cout << "The average test score is " << avg << std::endl;
return 0;
}


Your main problem is your line 38
How do I get it to include the first input in the average? It seems like it is only taking the average of what comes in the loop? Thank you for the help by the way!
closed account (48T7M4Gy)
Unless I've misunderstood what you are trying to do, if you say you want the average of 3 numbers then that's what happens. Don't forget count starts at zero. The first input comes at lines 23 and 24 - why have it earlier - some people say you need a 'priming read' which is wrong in a lot of cases and this is one.

eg if I say I would like 3 test scores, then I get asked 3 times eg 4,5 and 6 and the answer is 15/3 =5. QED

BTW The reason I commented out various lines was just to make debugging easier for me and not to trash your work. :)
Last edited on
closed account (48T7M4Gy)
This is the output I get. Obviously the prompts need to be in the right sequence but the average of 1,2,3 & 4 is 2.5!!

C:\
How many test scores would you like to enter?:4
Enter score: 1
Please enter a number 1-100!
Enter score: 2
Please enter a number 1-100!
Enter score: 3
Please enter a number 1-100!
Enter score: 4
Please enter a number 1-100!
The average test score is 2.5

C:\




Last edited on
Thanks a bunch kemort! Yeah, I realized I was dividing it by n instead of a so it was getting off. No problem, your way actually does look a lot neater haha I was just surprised you didn't use [ using namespace std] at the header.
You can also do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
	int count = 0;
	float n, avg = 0.0f;
	char a;
	do
	{
		cout << "Please Enter a number [1-100]:";
		cin >> n;
		avg += n;
		count += 1;
		cout << "Would you like to enter another score? [y/n]:";
		cin >> a;
	} while (a == 'y');

	avg /= count;

	cout << "The average test score is " << avg << endl;
}


Thus you don't need to worry about how many tests you need to do.
Wow, I didn't realize there are so many ways to write a program! Thanks for the help everyone! The only questions I have left is, how do I also use the cin.fail if they enter a letter or anything besides a number? If I have the user input a letter such as k score on the test it exits the program?
closed account (48T7M4Gy)
Cheers chree3pO,

If there was only one way then all we'd have to do is have a good book or photographic memory - pretty boring.

using namespace std is a quick way for small programs but the fail-safer way is to avoid it due to potential conflicts. Check it out on the forums here or generally google it when you get time.

Keep the error checking and just put you lines back in and test as you go line by line. Basically another while loop based on failure - keep 'em going around until they get it right - politely of course. You're on the right track.

BTW The idea of not worrying how many numbers there are and letting the computer count them as you go as Lazaro points out is the 'premium' version.
Last edited on
Topic archived. No new replies allowed.