Modifying program slightly.

Okay....Two questions. 1) What would be the best way to convert this app to where the user just types in names until they type "Quit" instead of typing in how many students there should be. 2) How could I break this down into a .h file, source file, and main?

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

#include <iostream>
#include <string>
using namespace std;

//Declare struct
struct GradeBook
{
	string name;	
	int* tests;			
	double average;		
	char grade;		
};

//Functions
int inputStudents();			
string inputName();				
int *inputScores(int);			
double calcAverage(int*, int);	
char calcGrade(double);			
void display(GradeBook*, int);
void displayClassAverage(GradeBook*, int);	

int main()
{

	//Create a dynamic array of the GradeBook structure.
	GradeBook *studentList; 
	int size = inputStudents(); 
	studentList = new GradeBook[size];

	//Create an array to hold the number of test scores for each student.
	int numOfTests = 3;

	for (int count = 0; count < size; count++)
    {
		studentList[count].name = inputName();
		studentList[count].tests = inputScores(numOfTests);
		studentList[count].average = calcAverage(studentList[count].tests, numOfTests); 
		studentList[count].grade = calcGrade(studentList[count].average); 
	}

	display(studentList, size); 
	displayClassAverage(studentList, size);

	delete [] studentList; 
	
	cout << endl;
	
	system("Pause");

	return 0;
}

int inputStudents()
{

	int students; 
	cout << "How many students are there? ";
	cin >> students; 

	return students; 
}

string inputName()
{

	string name; 
	cout << "Enter the student's name: ";
	cin >> name;

	return name; 

}

int *inputScores(int numOfTests)
{

	int *scores; 
	scores = new int[numOfTests]; 

	cout << "Enter the test scores for the student. Press ENTER after each score." << endl;

	for (int count = 0; count < numOfTests; count++)
    {
		cout << "Score " << (count + 1) << ": "; 
		cin >> scores[count];
	}

	return scores; 
}

double calcAverage(int *testScores, int numOfTests)
{

	int total = 0;  
	double average;

	for (int count = 0; count < numOfTests; count++)
    {
		total += testScores[count];
	}
	
	average = total / numOfTests;
	return average;

}

char calcGrade(double average)
{

	char letterGrade; 

	if		(average > 90 && average <= 100)
		letterGrade = 'A';
	else if (average > 80 && average <= 90)
		letterGrade = 'B';
	else if (average > 70 && average <= 80)
		letterGrade = 'C'; 
	else if (average > 60 && average <= 70)
		letterGrade = 'D';
	else if (average >= 0 && average <= 60)
		letterGrade = 'F'; 
	else{  
		cout << "Invalid input!" << endl;
		exit(1); 
	}

	return letterGrade;
}

void display(GradeBook *studentList, int size)
{
    cout << endl;
 
	for (int count = 0; count < size; count++)
		cout << studentList[count].name << " "<< studentList[count].average <<
         " " << studentList[count].grade << endl; 
}

void displayClassAverage(GradeBook *studentList, int size)
{
     int total = 0;
     for (int count = 0; count < size; count++)
     {
         total = studentList[count].average + total;
     }
     cout << endl;
     cout << "Class average is: " << total / size <<
     " " << calcGrade(total / size) << endl;
}


Second question:

create a functions.h and place your function definitions in there:

1
2
3
4
5
6
7
int inputStudents();			
string inputName();				
int *inputScores(int);			
double calcAverage(int*, int);	
char calcGrade(double);			
void display(GradeBook*, int);
void displayClassAverage(GradeBook*, int);


create a functions.cpp and add a line at the top:

#include "functions.h"

then add all your functions into the cpp file excluding the main() one.

Lastly, include the functions.h file in the file where your main function is.
Softrix,

Thanks for the quick reply! I'm not familiar with vectors at all. I would familiarize myself, but this is due at midnight tonight. This is all I have left though. That's what I thought for question 2...but when I do that the whole thing blows up.

Here is a list of errors:

4 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp In file included from FinalMain.cpp
4 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalHeader.h expected unqualified-id before '{' token
4 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalHeader.h expected `,' or `;' before '{' token
N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp In function `int main()':
19 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp `grades' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
19 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp expected `;' before "gradeBook"
23 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp `inputStudents' undeclared (first use this function)
31 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp `inputName' undeclared (first use this function)
32 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp `inputScores' undeclared (first use this function)
33 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp `calcAverage' undeclared (first use this function)
34 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp `calcGrade' undeclared (first use this function)
37 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp `display' undeclared (first use this function)
38 N:\USB Files\USB Files\Spring 2014\C++ Homework\This Final\FinalMain.cpp `displayClassAverage' undeclared (first use this function)
The breaking up into separate files isn't as important as the typing "Quit" to stop accepting names. I want to break it up to make it look cleaner, but that's not a mandatory thing. However, the other part is a specific part of the assignment.

Heres how I did it using your code (everything compiles fine):

Functions.h :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <iostream>
#include <string>
using namespace std;

//Declare struct
struct GradeBook
{
	string name;
	int* tests;
	double average;
	char grade;
};

//Functions
int inputStudents();
string inputName();
int *inputScores(int);
double calcAverage(int*, int);
char calcGrade(double);
void display(GradeBook*, int);
void displayClassAverage(GradeBook*, int);



Functions.cpp

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

#include "Functions.h"

int inputStudents()
{

	int students;
	cout << "How many students are there? ";
	cin >> students;

	return students;
}

string inputName()
{

	string name;
	cout << "Enter the student's name: ";
	cin >> name;

	return name;

}

int *inputScores(int numOfTests)
{

	int *scores;
	scores = new int[numOfTests];

	cout << "Enter the test scores for the student. Press ENTER after each score." << endl;

	for (int count = 0; count < numOfTests; count++)
	{
		cout << "Score " << (count + 1) << ": ";
		cin >> scores[count];
	}

	return scores;
}

double calcAverage(int *testScores, int numOfTests)
{

	int total = 0;
	double average;

	for (int count = 0; count < numOfTests; count++)
	{
		total += testScores[count];
	}

	average = total / numOfTests;
	return average;

}

char calcGrade(double average)
{

	char letterGrade;

	if (average > 90 && average <= 100)
		letterGrade = 'A';
	else if (average > 80 && average <= 90)
		letterGrade = 'B';
	else if (average > 70 && average <= 80)
		letterGrade = 'C';
	else if (average > 60 && average <= 70)
		letterGrade = 'D';
	else if (average >= 0 && average <= 60)
		letterGrade = 'F';
	else{
		cout << "Invalid input!" << endl;
		exit(1);
	}

	return letterGrade;
}

void display(GradeBook *studentList, int size)
{
	cout << endl;

	for (int count = 0; count < size; count++)
		cout << studentList[count].name << " " << studentList[count].average <<
		" " << studentList[count].grade << endl;
}

void displayClassAverage(GradeBook *studentList, int size)
{
	int total = 0;
	for (int count = 0; count < size; count++)
	{
		total = studentList[count].average + total;
	}
	cout << endl;
	cout << "Class average is: " << total / size <<
		" " << calcGrade(total / size) << endl;
}


and finally:

main.cpp

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

#include "Functions.h"

int main()
{

	//Create a dynamic array of the GradeBook structure.
	GradeBook *studentList;
	int size = inputStudents();
	studentList = new GradeBook[size];

	//Create an array to hold the number of test scores for each student.
	int numOfTests = 3;

	for (int count = 0; count < size; count++)
	{
		studentList[count].name = inputName();
		studentList[count].tests = inputScores(numOfTests);
		studentList[count].average = calcAverage(studentList[count].tests, numOfTests);
		studentList[count].grade = calcGrade(studentList[count].average);
	}

	display(studentList, size);
	displayClassAverage(studentList, size);

	delete[] studentList;

	cout << endl;

	system("Pause");

	return 0;
}
What you did looks almost identical to mine, but yours works and mine doesn't... Well thanks so much softrix! Any idea on how to redo the input of the names? Aside from vectors :)

Well if you want to avoid Lists and Vectors the only other way would be to allocate some initial space and extend it as needed when you exceed the amount previously allocated.

If your not comfortable with pointers you may have a problem.

I did something similar in this thread: http://www.cplusplus.com/forum/beginner/130325/

I'm not necessarily against vectors, I just don't know how to use them, and don't have time to learn them before midnight lol.

I did a vector example in this post.

http://www.cplusplus.com/forum/beginner/126134/

Thanks so much Softrix! I will see if I can figure it out. Again, thanks so much for your prompt help!
Your welcome.
Topic archived. No new replies allowed.