help with loops!! please!!

I'm working on this program but I am not sure how to end it. Its suppose to end after the user inputs 3 scores but I'm not sure how to do that and after that it asks the user if it wants to add more scores. If anyone could please help me out with this I would really appreciate it. here is the code I have so far

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
95
96
#include<iostream>
using namespace std;

int main(void)
{
	do
	{
		// 1. Do Average
		// 1.1. Get statististics
		// 1.1.1 Initialize Statistics Accumulators
		int sum = 0;
		int count = 0;

		// 1.1.2 Accumulate Statistics (loop)
		do
		{
			// 1.1.2.1 Get Scores (loop)
			int score;
			do
			{
	
				// 1.1.2.1.1 Input Scores
				cout << "Enter a Score: ";
				cin >> score;
				cin.ignore(99,'\n');

				
				
				// 1.1.2.1.2 Check Error
				if ( cin.fail() )
				{
					cin.clear();
					cin.ignore(99,'\n');
					cout << "Please enter a number." << endl;
					continue;
				}
				if ( score < 0 ) // 
				{
					cout << "Negative scores are not allowed." << endl;
					continue;
				}
				break;
			} while (true);

			// 1.1.2.2 Accumulate Score to statistics
			sum += score;
			count += 1;

		} while (true);

		// 1.2 Calculate adjusted average score
		int average = sum / count; // 
	

		// 1.3 Output adjusted average
		cout << "The adjusted average score is " << average << endl; // 

		// 2. Ask to Do Again (loop)
		bool doAgain;
		do
		{
			// 2.1 Input User Answer.
			cout << endl << "Do you wish to enter another score? ";
			char userAnswer;
			cin >> userAnswer;
			cin.ignore(99,'\n');

			// 2.2 Check Error
			if ( cin.fail() )
			{
				cin.clear();
				cin.ignore(99,'\n');
				cout << "Bad input, try again." << endl;
				continue;
			}
			switch ( userAnswer )
			{
			case 'y': case 'Y': doAgain = true; break;
			case 'n': case 'N': doAgain = false; break;
			default:
				cout << "Please anwer 'y' or 'n'." << endl;
				continue;
			}
			break;
		} while (true);
		if (!doAgain ) break;
	} while (true);

	return 0;
}





Your code is really stretched out, couldn't you just remove a bunch of those while statements as well? And this is too difficult for me to look at without going "oooooh" so I'll just simplify it a bit.
And try a " for while loop" which goes something like this:

1
2
3
4
5
for (int x=1 ; x<=3 ; x++){ //This means that x starts at one and increases by one every single time this loop is executed. This loop will execute three times since I said "x<=3".
//Put the thing you wish to be repeated here and to solve it do it outside this loop. Bit more simple than what you did.
}



And after a afterthought I solved the program in a more simply manner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#include<iostream>
using namespace std;

int main()
{
for (x=1;x<=3;x++){
int score,sum; //Declaring things inside the loop.
cout<<"\nPlease input score #"<<x<<"."<<endl; //Getting the user to input the score 3 times.
cin>>score; 
sum=score+sum; //Getting the total sum of all the scores combined
}//ending the for loop
int sum=sum/3; //Getting the average if you need only 3 scores to be inputted
cout<<"The total average is "<<sum<<".";
Last edited on
thanks for your reply. But I'm confused i'm sorry I am new to C++ and to be honest with you I am not understanding how to use loops. this program I'm working on is asking me to use loops to calculate an adjusted average score. There must be at least three scores inputed and the program should also ask and allow the user to input additional scores before calculating the adjusted average score.
I'm a freshman taking C++ so I'm new at it as well, don't worry.

What you were doing was a do while loop.
This means to do WHILE (value==is something) or do WHILE(value!=isn't something) the ! is called a "not", meaning not.

You could have just done a WHILE loop, the only difference between these two is that it checks the condition at the back or the front.

If you want it to continue, I suppose you could always 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
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
using namespace std;

int main()
{
ctr=3 // I've replaced the three over here so I can change it later on without it going back

while (y!=1)
{

for (x=1;x<=ctr;x++){
int score,sum; //Declaring things inside the loop.

cout<<"\nPlease input score #"<<x<<"."<<endl; //Getting the user to input the score 3 times.

cin>>score; 

sum=score+sum; //Getting the total sum of all the scores combined
//ending the for loop
}
cout<<"Do you want to input another score? Input 2 if yes, input 1 if no";
cin>>y;
//This is so you can get out of the while loop.
int x=1 // This is so if you want to add another statement, you could just input ctr to the times you want to add values again.
int totalctr=ctr+totalctr; //Making it so that you can get the average for the amount of values implemented
if (y==2)
{
cout<<"Enter how many times you want to enter the input";
cin>>ctr;
}
}

int sum=sum/totalctr; //Getting the average.

cout<<"The total average is "<<sum<<"."; // is now outside the while statement, the statement needs to be satisfied before you can go here.
}


May have messed up somewhere, try to check my code. What I did BASICALLY is just use the same for loop, but make it so the first value the "x" have to go to is 3. I did this by making sure that ctr is represented by 3 at the beginning.

Then I let you manipulate ctr and put x at 0 to refresh the for loop, NOT refreshing the sum.

While doing this I made totalctr (total counter) how many times the program will be executed so I could get the average.

Kind of a brain twizzler isn't it? Tell me if you need more help.
Last edited on
what does this mean ctr=3 I havent seen this ctr??
I just added it. Ctr isn't any specific name, It's just a variable name I like to use. Ctr=3 in this program means that the for loop will be executed 3 times,
for (x=1;x<=ctr;x++){
I guess the first thing as a newbie using loops, is not to use infinite loops. Advanced coders use them sometimes, but only in certain valid situations where they know what they are doing.

Check out the tutorial page on this website, and while you are at it, read some of the reference pages as well. They are both at the top left of this page.

With loops, there are several parts - initialisation, increment and most importantly an end condition.

With Maxim Podolski
post there are some minor improvements that could be made.

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
#include<iostream>


using std::cin;
using std::cout;
using std::endl;


int main() {
	
	//declare variables outside for loop so they are availble afterwards
	
	int Counter;
	const unsigned SIZE = 3;//Number of scores to input, and array sze
	double Score = 0.0; //double not ints
	double Sum = 0.0; 
	double ScoreArray[SIZE];
	
	for (Counter = 0; Counter < SIZE; Counter++) {
		
		
		cout<<"\nPlease input score #"<<Counter + 1<<"."<<endl; 
                              
		cin>>Score; //There is no error checking using the cin.fail bit
		
		ScoreArray[Counter] = Score; //Store everything in an array
		Sum += Score; //Getting the total sum of all the scores combined
	}//ending the for loop
	
	double  Average = Sum / SIZE; //Integer division was no good - automatic
								  //cast to double
	
	cout << "The Sum is " << Sum << endl;
	cout << "The  average is "<<Average << endl;
	cout << "The number of scores was " << SIZE;

	return 0; //this is not absolutely necessary but I do it anyway
}


Instead of having the magic number of 3 throughout the code, it is better to make it a const variable at the start of the program, then refer to this variable where necessary.

With the for loop:
for (x=1;x<=3;x++){

The normal idiom for doing something 3 times is this, and combining the const variable:

1
2
3
4
5
6
const unsigned SIZE = 3;
for (int x = 0; x < SIZE; x++){

//your code here

}


I stored the values in array, this means you can calculate other things like standard deviation, residuals or whatever.

As the comment says - there is no error checking on the input. It is a very good idea to do this - always validate input.

Hope all goes well

Edit:

With the values in the array, you could also print them out - use a for loop to do so.
Last edited on
Oh thanks!
And we all love magic number 3.
"it is better to make it a const variable at the start of the program, then refer to this variable where necessary." I made it a variable before all the loops and so forth, and then I was able to change it, so I THINK I did what you are saying.
thanks for your replys!! is there a way of doing this without using arrays? I am very slow at this but your feedback is very appreciated!!
There is, but arrays are a very good idea, so learn as much as you can about them.

You can also use the STL vector, which is just like an array, but with some advantages.

I suggest you look at the reference pages on this site- at the top left of this page.
You know.
Ariana, I feel jealous, no one even looked at the help I tried to post on the forum earlier. You should make a tutorial of how to get responses.
And as was stated, yeah, you can do it WITHOUT arrays, but you may want to. If this is some school assignment, just get it over with by now and quickly just finish this, but if you are going to use it I'd suggest the way TheIdeasMan suggested for the benefits already listed.
Gosh, I don't even have to do anything.
ok thank you!! TheIdeasMan and Maxim Podolski your feedback was very helpful!! :)
Topic archived. No new replies allowed.