writing calculations for unknown amount of students

I need to create a program that continuously records data until the user enters '0'

The data that I read in is the student's last name, the year of the student (1-4), the major, the gpa, and the advisors name. I then need to print out how many students are in each year (1-4), the number of students each advisor has, the average gpa for each major, and so on. This is what I have, but I am stuck and think that I might be headed the wrong direction because the only thing working is the loop (not my gpa test calc). Can someone help me out a bit please?

//This program displays information pertaining to students

#include <iostream>
#include <string>


using namespace std;

int main ()
{
int studYear;
double average, studGpa;
string studName, studMajor, studAdvisor, complete;

//Read Data
cout << "1-Freshman 2-Sophmore 3-Junior 4-Senior \n" << endl;
cout << "Enter Year in School: ";
cin >> studYear;
while (studYear>=1) {
cout << "Enter Students Name: ";
cin >> studName;
cout << "Math or CIS Major: ";
cin >> studMajor;
cout << "Enter Students GPA: ";
cin >> studGpa;
cout << "Enter Students Advisor (Johnson or Smith): ";
cin >> studAdvisor;
cout <<endl << endl << "1-Freshman 2-Sophmore 3-Junior 4-Senior \n";
cout << "Enter 0 If No More Students To Input \n" << endl;
cout << "Enter Year in School: ";
cin >> studYear;
}

int sum = 0;
int totalGPA = 0;
do{
if(studGpa != -1) {
sum += studGpa;
totalGPA++;
}
}while (studGpa != -1);

//Calculations
average = sum / totalGPA;


//Print Data
if ( studYear == 1){
cout << endl << endl <<
cout << "Average GPA: " << average << endl;
}


return 0;
Since you aren't using functions, and only variables, I'm guessing you are a beginner in C++. You have it pretty close. I would have rather done functions and made most of the work done in them, but since I don't know your level I kept it to just variables (meaning I have a butt load of them to figure everything you needed in your comments).
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
#include <iostream>
#include <string>

using namespace std; // for the record I hate doing this

int main()
{
	int studYear = 0, smithCnt = 0, johnCnt = 0;
	int year1Cnt = 0, year2Cnt = 0, year3Cnt = 0, year4Cnt = 0;
	int mathCnt = 0, CISCnt = 0, totalMathCnt = 0, totalCISCnt = 0;
	double average = 0.0, studGpa = 0.0, mathGPASum = 0.0, CISGPASum = 0.0;
	string studName, studMajor, studAdvisor, complete;
	
	// read the data
	do{
		cout << "1-Freshman 2-Sophomore 3-Junior 4-Senior\n";
		cout << "Enter 0 if no more students to input\n";
		cout << "Enter year in school: ";
		cin >> studYear;
		if(studYear != 0){
			if(studYear == 1){
				year1Cnt++;
			}else if(studYear == 2){
				year2Cnt++;
			}else if(studYear == 3){
				year3Cnt++;
			}else{
				year4Cnt++;
			}
			cout << "Enter Student's name: ";
			cin >> studName;
			cout << "Math or CIS Major: ";
			cin >> studMajor;
			cout << "Enter students GPA: ";
			cin >> studGpa;
			if (studMajor == "Math"){
				mathCnt++;
				mathGPASum += studGpa;
				totalMathCnt++;
			}else{
				CISCnt++;
				CISGPASum += studGpa;
				totalCISCnt++;
			}
			cout << "Enter sutdent advisor(Johnson or Smith): ";
			cin >> studAdvisor;
			if (studAdvisor[0] == 'J' || studAdvisor[0] == 'j'){
				johnCnt++;
			}else{
				smithCnt++;
			}
		}
	}while(studYear != 0);
	
	cout << "Year 1 students: " << year1Cnt << endl;
	cout << "Year 2 students: " << year2Cnt << endl;
	cout << "Year 3 students: " << year3Cnt << endl;
	cout << "Year 4 students: " << year4Cnt << endl;
	cout << "Johnson has " << johnCnt << " students\n";
	cout << "Smith has " << smithCnt << " students\n";
	average = mathGPASum / totalMathCnt;
	cout << "Math GPA: " << average << endl;
	average = CISGPASum / totalCISCnt;
	cout << "CIS GPA: " << average << endl;
	
	
	return 0;
}
1-Freshman 2-Sophomore 3-Junior 4-Senior
Enter 0 if no more students to input
Enter year in school: 2
Enter Student's name: BHX
Math or CIS Major: CIS
Enter students GPA: 2.3
Enter sutdent advisor(Johnson or Smith): Smith
1-Freshman 2-Sophomore 3-Junior 4-Senior
Enter 0 if no more students to input
Enter year in school: 1
Enter Student's name: Joe
Math or CIS Major: Math
Enter students GPA: 4.0
Enter sutdent advisor(Johnson or Smith): Johnson
1-Freshman 2-Sophomore 3-Junior 4-Senior
Enter 0 if no more students to input
Enter year in school: 3
Enter Student's name: Dan
Math or CIS Major: Math
Enter students GPA: 2.1
Enter sutdent advisor(Johnson or Smith): Johnson
1-Freshman 2-Sophomore 3-Junior 4-Senior
Enter 0 if no more students to input
Enter year in school: 4
Enter Student's name: Bob
Math or CIS Major: CIS
Enter students GPA: 1.1
Enter sutdent advisor(Johnson or Smith): Smith
1-Freshman 2-Sophomore 3-Junior 4-Senior
Enter 0 if no more students to input
Enter year in school: 0
Year 1 students: 1
Year 2 students: 1
Year 3 students: 1
Year 4 students: 1
Johnson has 2 students
Smith has 2 students
Math GPA: 3.05
CIS GPA: 1.7
Last edited on
Yes, I am a beginner. Thank you for your help!
Topic archived. No new replies allowed.