How do I fix my loop?

How do I fix my code to where the student and assignment numbers aren't set as just single digits? It continues to repeat the input of the number of students and the number of assignments.
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
97
98
99
100
101
102
103
104
105
106

 #include "pch.h"
#include <iostream>
#include "cmath"
#include "iomanip"
using namespace std;
double find_avg(double, double);
double pts_poss(int);
double ent_grd(int, int);

int main()
{
	int student, cnt1, cnt2, grdnum;
	double grade , totalgrd, pnts, totalpnts, average;

	cnt1 = 1;
	totalpnts = 0;

	cout << "Enter the number of students: ";
	cin >> student;

	if (student <= 0)
	{
		cout << "\nError: The number of students must be greater than zero" << endl;
		system("PAUSE");
		return 0;
	}
	else
		cout << "Enter the amount of grades for each student: ";
	cin >> grdnum;
	
	if (grdnum < 0)
	{
		cout << "/nError: Number of grades must be greater than zero" << endl;
		system("PAUSE");
		return 0;
	}
	else

		do
		{
			pnts = pts_poss(cnt1);

			if (pnts < 0)
			{
				cout << "\nError: Grades must be positive " << endl;
			}
			else
			{
				totalpnts = totalpnts + pnts;
				cnt1++;
			}
		} while (cnt1 <= grdnum);

		cout << "\nAssignment " << grdnum << " is worth a total of " << totalpnts << " points" << endl;
		cnt1 = 1;

		while (cnt1 <= student)
		{
			totalgrd = 0;
		

			for (cnt2 = 1; cnt2 = grdnum; cnt2++)

			{
				grade = ent_grd(cnt1, cnt2);

				if (grade < 0)
				{
					cout << "\nError: Grade must be postive";
					cnt2--;
						
				}
				else
				{
					totalgrd = totalgrd + grade;
				}

			} 
			average = find_avg(totalgrd, totalpnts);
			cout << "\nAverage for student " << cnt1 << " is " << average << endl;
			cnt1++;
			system("PAUSE");
		}
}
double pts_poss(int cnt1)
{
	double pnts = 0;
	cout << "\nHow many points is assignment " << cnt1 << " worth: ";
	cin >> pnts;
	return pnts;
}

double ent_grd(int studentnum, int grdnum)
{
	double grade = 0;
	cout << "Enter student " << studentnum << " grade for assignment " << grdnum << " : ";
	cin >> grade;
		return grade;
  }
double find_avg(double totalpnts, double totalpnts_poss)
{
	double average;
	average = (totalpnts / totalpnts_poss) * 100;
	return average;
}
Last edited on
Please edit so we can read your code properly.
http://www.cplusplus.com/articles/jEywvCM9/
Hello Xenataron

You should have a look at your for loop on line 63:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (cnt2 = 1; cnt2 = grdnum; cnt2++) //HERE
{
    grade = ent_grd(cnt1, cnt2);

    if (grade < 0)
    {
        cout << "\nError: Grade must be postive";
	cnt2--;
						
    }
    else
    {
	totalgrd = totalgrd + grade;
    }
}


For each iteration of the loop you set cnt2 to one and then set grdnum to 1 as well so you're repeating the loop indefinitely.

To correct that, you just need to do this:
for (cnt2 = 1; cnt2 <= grdnum; cnt2++)

Hope this helps, have a good day.
Thank you very much! I can't believe I overlooked that.
My pleasure :)
Topic archived. No new replies allowed.