for loops help please!


This is the problem I am trying to figure out:
A student wants to be able to figure out their average in each of their classes. Each class has a different number of assignments. The program needs to ask the user how many classes they have. Then, for each class, it must ask how many assignments there are for that class. It will then ask the user to input a grade for each of the assignments. After the last assignment for a class is entered, the program will display the average for that class before continuing on to the next class. After the last class is complete, the program will display the average of all assignments for all classes (overall average).

Everything looks pretty alright to me but it is telling me my break isn't in my loop... can I get a little bit of help with this, this is my first time working with loops and it is confusing me.
thanks!!

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
#include <iostream>
using namespace std;
int main(void)
{
int totalClasses;
int classs;
int grades;
int totalGrades;
int average;
int totalGrades=0;
int totalAssignments=0;
int numAssignemnts;
int numAssignments=0;
int numClass=0;
cout << "Please input how many classes\n";
cin >> totalClasses; 
for (int classs =1; totalClasses >= classs; classs++);
	{
	cout << "Please enter how many assignments for this class\n";
	cin >> numAssignments;
	numAssignments + totalAssignments == totalAssignments;
	cout << "How many grades for this class?\n";
	cin >> totalGrades;
	cout << "Please enter a grade\n";
	cin >> grades;
	grades + totalGrades == totalGrades;
	cout << "Your average for this class is:\n" << grades/numAssignments;
	break; // it is being used in a loop isnt it.... ?
	}
cout << "Your total average is:\n" << totalGrades/totalAssignments;
return 0;
}
Here's what I see:
1. totalGrades is declared twice. (delete the one on line 8)
2. numAssignments + totalAssignments == totalAssignments; should probably be: totalAssignments = totalAssignments + numAssignments; or more concisely: totalAssignments += numAssignments;
3. Line 17 should not have a semi-colon. That's why the break statment is triggering an error.

There are a few other issues,
regarding the break, it is in the for loop and will have an effect... but you don't want it there. Without conditions, it just exits the loop as soon as you get to that point in the loop.
Thank you. To make the break conditions should I put them in parentheses after break similarly to how you do for ( ) ?
For this scenario you don't need break conditions. A break will cause you to prematurely exit a for loop. It's a useful escape if you have to quit due to an emergency or if you will be stuck in an infinite loop otherwise.

Maybe this wil help.
http://cplusplus.com/doc/tutorial/control/
Check out the sections on for and break;. Skip the section on goto.


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

int main()
{
    int totalClasses, totalAssignments = 0, totalGrades = 0;
    
    cout << "Input the number of classes\n";
    cin >> totalClasses;
    
    for (int classs = 0; classs < totalClasses; ++classs)
    {
        int classAssignments, classGrades = 0;
        cout << "How many assignments for this class?: \n";
        cin >> classAssignments;
        
        for (int assignment = 0; assignment < classAssignments; ++assignment)
        {
            int grade;
            cout << "Enter the grade: \n";
            cin >> grade;
            classGrades += grade;
        }
        
        cout << "Your average for this class is: " << (float)classGrades/(float)classAssignments << endl;
        totalAssignments += classAssignments;
        totalGrades += classGrades;
    }
    
    cout << "Your total average is: " << (float)totalGrades/(float)totalAssignments << endl;
}
Last edited on
Awesome thank you very much
Topic archived. No new replies allowed.