help help with loop

i have 2 assignments to do ,so i did the both of the programs but still i have errors and i couldn't do it
this is the first program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
		
	int x;
	int sum;
	
	while(x<10)
	{
		sum=x+1;
		x=0;
		cout << sum << endl;
	}

	return 0;
	
}




and this is the second 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
#include <iostream>

using namespace std;

int main()
{
	int testNum;
	double average;
	double test;
	
	cout << "This program will calculate your average test grade" << endl;
	cout << "Please enter the number of exams you wish to average: ";
	cin >> testNum;
	
	while(testNum>0)
	{
		cout << "Enter a test grade: ";
		cin >> test;
		
		average=average+test;
		testNum--;
	}

	average=average/testNum;

	cout << "Your test average is: " << average << endl;

	if (average<90)
		cout << "You have an A in the class" << endl;
	else if(average<80)
		cout << "You have a B in the class" << endl;
	else if(average<70)
		cout << "You have a C in the class" << endl;
	else if(average<60)
		cout << "You have a D in the class" << endl;
	else
		cout << "You have a F in the class" << endl;
	
		
	return 0;
	
}
What errors are you getting? You should copy and paste them exactly.
This program will calculate your average test grade
Please enter the number of exams you wish to average: 3
Enter a test grade: 80
Enter a test grade: 90
Enter a test grade: 2
Your test average is: inf
You have a F in the class
When you loop through each test grade, you decrease testNum until it reaches 0. Then you divide by testNum. What happens when you divide by 0?

I suggest you leave testNum alone and create a separate variable to keep track of how many test grades have been entered. I'd suggest a for loop.
Topic archived. No new replies allowed.