Not sure how to Proceed...

I have to do this last part for my assignment:

Create a for loop that takes in the user's input.
Create a for loop that reads integer values off of a file named ExamScore.txt
Create a for loop that creates random numbers from 1-100

(Original directions)
o Write a switch statement where cases are defined by the variable choice. Under each case, write the appropriate for-loop to get the scores from the chosen way and store them in the array Exam.
o To get a random real number between 0 and 100 (including the end values), use
x = (rand()%1001)/10.0;
o To read from a file named ExamScore.txt write the following declaration above main() and use f_cin instead of cin.
ifstream f_cin(“ExamScore.txt”, ios::in);
o Before you run the program, you must create a file named ExamScore.txt. To do that either use Notepad or open a file in the compiler editor and enter the exam scores (as many as SIZE) and save the file with the name ExamScore.txt.
o Make sure you include the file <fstream> in “stdafx.h”
o Run the program with NumOfExams as 3. Each time choose a different option.

Here's the code:

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "stdafx.h"

#define LENGTH 10
#define FormatLength 4

void GetClassInfo(int &, int &);
void GetScore(float *, int);
void ReadMin(float *); 
float ComputeAvg(float *, int );
float FindHighestScore(float *, int);
float FindLowestScore(float *, int);
int FindNum(float *, int , float);
void Print(float *, int); 
void PrintResults(int,  float, float, float, int *);

ofstream f_cout("Project2_PartC.txt", ios::out);
ifstream f_cin("ExamScore.txt", ios::in);


int main()
{
	// Storage
	float *Exam, avg, highest, lowest, Min_Pass = 0, Min[5];
	int SIZE, Num[5], NumOfExams, ExamCounter;


	GetClassInfo(SIZE, NumOfExams);
	Exam = new float [SIZE];

	// Input

	for (int i = 0; i < NumOfExams; i++)
	{

	GetScore(Exam, SIZE); 
	ReadMin(Min);

	// Process
	avg = ComputeAvg(Exam, SIZE);
	highest = FindHighestScore(Exam, SIZE);
	lowest = FindLowestScore(Exam, SIZE);

	    for (int j = 0; j < 5; j++)
	    {
		    Num[j] = FindNum(Exam, SIZE, Min[j]);
	    }

	// Output
	
	cout << "Exam: "<< i + 1;

	Print(Exam, SIZE);
	PrintResults(SIZE, avg, highest, lowest, Num);
	}

	return 0;
}

void GetClassInfo(int &n, int &Exams)
{

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

}

// Input

void GetScore(float *Ex, int n) // reads the scores entered
{
	int choice;
	//float scores;

	cout << endl << endl;
	cout << "Where do I get these scores?\n\n";
	cout << "1) User enters the scores\n" 
	<< "2) Read the scores from a file\n" << "3) Generate the scores randomly\n\n";
	cout << "Enter your choice [1 or 2 or 3]: ";
	cin >> choice;

	switch (choice) // Part D calls for a Switch Statement to allow the user to select a choice
	{
	case 1:
		for (int i=0; i<n; i++)
	    {
			cout << "Enter grades: ";
			cin >> Ex[i];
	    }
		break;

	case 2:
		for (int i=0; i<n; i++)
		{
			ifstream file;
			file.open("ExamScore.txt");
			while (file >> i) 
			{
				cout << fixed << showpoint << setprecision(1) << i << endl;
			}
			//cout << "Enter grades: ";
			//f_cin >> Ex[i];
	    }
		break;

	case 3:
		for (int i = 0; i < n; i++)
		{
			Ex[i] = (rand()%1001)/10.0; 
			cout << Ex[i] << " ";
		}
		break;
	}
}

void ReadMin(float *min) // stores values for getting an A, B, C, D, and passing.
{
	cout << endl << endl;
	cout << "Enter the minimum score for an A, B, C, D, and passing: " << endl;

	for (int i = 0; i < 5; i++)
	{
		cin >> min[i];
	}
	//fixed << showpoint << setprecision(1)
	cout << endl << endl;
	f_cout << "GRADE DISTRIBUTION:\n\n";
	f_cout << "A: " << fixed << showpoint << setprecision(1) << min[0] << setw(FormatLength) << " ";
	f_cout << "B: " << min[1] << setw(FormatLength) << " ";
	f_cout << "C: " << min[2] << setw(FormatLength) << " ";
	f_cout << "D: " << min[3] << setw(FormatLength) << " ";
	f_cout << "P: " << min[4];
	cout << endl << endl;
}


// Process
float ComputeAvg(float *Ex, int n) // Computes the average
{
	int i;
	float sum = 0;
		for (i = 0; i < n; i++)
		{
			sum = sum +Ex[i];
		}
			return (sum/n);
}


float FindHighestScore(float *Ex, int n) //Finds the highest score
{
	float high = Ex[0];

	for (int i = 1; i < n; i++)
	{
		if (high < Ex[i])
			high = Ex[i];	
	}

	return high;
}


float FindLowestScore(float *Ex, int n) // Finds the lowest score
{
	float low = Ex[0];

	for (int i = 1; i < n; i++)
	{
		if (low > Ex[i])
			low = Ex[i];	
	}

	return low;
}


int FindNum(float *Ex, int n, float Min_P)
{
	int i, count = 0;
	for(i=0; i<n; i++)
	{
		if(Ex[i] >= Min_P)
			count++;
	}

	return count;
}


// Output

void Print(float *Ex, int n)//by ref, by value
{
	for (int i = 0; i < n; i++)
	{
		if(i % LENGTH == 0) 
			cout << endl << endl;
		cout << setw(FormatLength) << " " << Ex[i];
	}
}


void PrintResults(int n,  float av, float high, float low, int *Num) // Prints the average, highest and lowest score, number who passed and failed, and how many got A, B, C, and D.
{ //cout << fixed << showpoint << setprecision(1);
	f_cout << endl << endl;
	f_cout << "Average Score is ......: " << setw(FormatLength) << av << endl;
	f_cout << "Highest is ............: " << setw(FormatLength) << high << endl;
	f_cout << "Lowest is .............: " << setw(FormatLength) << low << endl;
	f_cout << "Number Passed is ......: " << setw(FormatLength-1) << Num[4] << endl;
	f_cout << "Number Failed is ......: " << setw(FormatLength-1) << n - Num[4] << endl;

	for (int i = 0; i < n; i++) // Uses a for loop to go through each score entered
	{
		switch (i) // Determines if the score is an A, B, C, or D and adds it to the number of students who got 'X' grade.
		{
		case 0:
			f_cout << "Number with an A.......: " << setw(FormatLength-1) << Num[i] << endl;
			break;

		case 1:
			f_cout << "Number with a B........: " << setw(FormatLength-1) << Num[i] - Num[0] << endl;
			break;

		case 2:
			f_cout << "Number with a C........: " << setw(FormatLength-1) << Num[i] - Num[1] << endl;
			break;

		case 3:
			f_cout << "Number with a D........: " << setw(FormatLength-1) << Num[i] - Num[2]  << endl;
			break;
		}
	}
	cout << endl << endl; 
}


How do I read the integers into the file and how do I make my integers random every time the program is run?

EDIT: I figured out the random part.
Last edited on
How do I read the integers into the file

This makes no sense, are asking to read from a file or write to a file.

Looking at your code, it doesn't look like you have opened the files to read or write.
1
2
3
4
ifstream InputFile;
ofstream OutputFile;
InputFile.open ("infile.txt");
OutputFile.open ("outfile.txt");


your code in "void PrintResults" looks good to write to a file.
Last edited on
Topic archived. No new replies allowed.