Student statistics. How to read the middle intial of a student

Pages: 12
Prelimary rough draft of a code here that reads the first and the last name from a text file. Suppose, in one of the text files, the user has a middle name. How can I construct this code in such a way that middle name can be read too. I believe I have to make an extra variable too, but not so sure how or where to chunk it in 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

		//Enter in first name and last name in the filetxt
		infile>>firstname;
		infile>>lastname;

		//User gets to type in the test grades and assignment grades
		infile>>testgrade1 >>testgrade2;
		infile>> assign1;
		infile>>assign2;
		infile>>assign3;

	



//**************************************************************************WHILE LOOP OF THE FILEDATA ENTRY******************************************************************************
		while (infile)
		{
			
			//Print out the first and last name of the student
			cout<<left;
			cout<< "\n\n" << setw(11)<< firstname+ " " +lastname;
Program has already been submitted to my teacher but just for general knowelsge, any possible way to read the middle name too from a file.
Knock knock
It would help if you provided a working code sample and the format of the input file.
Hope this helps :( ...

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

}
	//*******************************************************************************************************************************************************************************
	
	//**************************************************CONSOLE OPEN**************************************************************************************************************

	//Outputing data names that will be outputted on the screen. For instance, name, grades, letter grade
		cout<< " Student Statistics: ";
		cout<< "\n\n\n";
		cout<< "Name\t";
		cout<< "\tTest";
		cout<<"\t    Assignments";
		cout<< "\t      Points";
		cout <<"\t  Numeric ";
		cout<<"\tLetter ";
		cout<<endl;
		cout<<"\t\t Avg";
		cout<< "\t        Avg";
		cout<<"\t\t\t    Grd";
		cout<< "\t\t Grd ";
	

		//Enter in first name and last name in the filetxt
		infile>>firstname;
		infile>>lastname;

		//User gets to type in the test grades and assignment grades
		infile>>testgrade1 >>testgrade2;
		infile>> assign1;
		infile>>assign2;
		infile>>assign3;

	



//**************************************************************************WHILE LOOP OF THE FILEDATA ENTRY******************************************************************************
		while (infile)
		{
			
			//Print out the first and last name of the student
			cout<<left;
			cout<< "\n\n" << setw(11)<< firstname+ " " +lastname;

			//Calculate the testaverage. Round it to two decimal places, with trailing zeroes
			testaverage=(testgrade1+testgrade2)/2;
			cout<<setprecision(2);
			cout<<fixed;
			cout<<showpoint;
			cout<<right<<setw(10) <<testaverage;

			//Calculate the assignment average of each student
			assignaverage=(assign1+assign2+assign3)/3;
			cout<< setw(15);
			cout<<assignaverage;

			//Calculate the total points accumalated
			totalpoints= testgrade1+testgrade2+assign1+assign2+assign3;
			cout<< setw(16) <<totalpoints;

			//Calculate the the numericgrade. Add up assignments and test, then divide by 5 to find the numericavg grade
			numericgrade= (testgrade1+testgrade2+assign1+assign2+assign3)/5;
			cout<< setw(12) <<numericgrade;

			
			
			//you will use the if and else statement to calculate the letter grade

			
			//If number is higher then 90. Student gets an A
			if(numericgrade >=90 && numericgrade<=100)
			{
				cout<<setprecision(15);
				cout<<"\t  A";
			}
			//If number is higher then 80. Students gets a B
			else if (numericgrade>=80 && numericgrade<90)
			{
				cout<<setprecision(15);
				cout<<"\t  B";
			}

			//If number is higher then 70. Student gets a C
			else if (numericgrade>=70 && numericgrade<80)
			{
				cout<<setprecision(15);
				cout<<"\t  C";
			}
			//Any lower then 70, gets an F
			else 
			{
				cout<<setprecision(15)<<"\t  F";
			}
			
		//Minimum is declared as 100. If minimum is greater then numeric grade, then numeric grrade will become minimum
		//For instance, numeric grade is 89 and minimum is 100. Minimum is greater then 89. 89 becomes the minimum. 
		//During the next loop, 89 is stored in mininum.

			if(minimum>numericgrade)
				minimum=numericgrade;


		//Maximum is declared at zero. If maxium is less then numeric grade then maxium will become numeric grade.
			if (maximum<numericgrade)
				maximum=numericgrade;

			//Prevent the loop from becoming an infinite loop. Enter in names, scores, etc...again!
			infile>>firstname;
			infile>>lastname;
			infile>> testgrade1;
			infile>>testgrade2;
			infile>> assign1;
			infile>>assign2;
			infile>>assign3;

			//Sum is used in this loop to find the total sum of numeric grades
			sum=sum+numericgrade;
			
			//Add counter++ to determine the total number of students
			counter++;

			
				
		}
		



Knock knock, anyone in the forum?
Double knock knock?
There is no good reason to bump your topic after only a few hours.

If you find your question is not being answered, it would be wise to see if there is something you can do to make it easier to be answered, like simplifying code or providing requested information

cire wrote:
It would help if you provided a working code sample and the format of the input file.


Is a middle name/initial optional?
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290



	//Declare first name and lastname
	string firstname;
	string lastname;


	//Declare The two test grades for students
	double testgrade1;
	double testgrade2;

	//Decllare the three assignment grades for students
	double assign1;
	double assign2;
	double assign3;

	//Declare the counter which will be used to count the total number of students
	int counter;
	counter=0;

	//Declare testaverage and assignment average to find the aveage of test and assignment
	double testaverage;
	double assignaverage;

	//Declare totalpoints accumalted from all of the assignments and test
	double totalpoints;

	//Getting the sum of the entire class in the loop. Sum is set to store the previous value of numbergrade
	double sum;
	sum=0;

	//Numeric grade determined of the student

	double numericgrade;

	//Set minimum to 100 to find the minimum value
	double minimum=100;
	
	//Declare and initilize maximum to find the maximum value of the student
	double maximum=0;

	//Declare average to find the class average
	double average;

	//The user will input an answer regarding which file to open. Three options will be given. A, b, or c
	char answer;
	
	//Declare a string variable that will open up a file wanted by the user
	string filetopen;

	//Declare the ifstream to open the input file
	ifstream infile;

	
	
	//*********************************************************************What File To Open?****************************************************************************************
	
	//Ask the user which file they want to read from
	cout<<"Which file would you like to read data from: (A, B, C)";

	
	//Displays on the console the options of which file are available to read
	cout<<"\n\t\t\tA.) File1.txt";
	cout<<"\n\t\t\tB.) File2.txt";
	cout<<"\n\t\t\tC.) File3.txt";
	cout<<"\n\n\t\t\tFile: ";
	
	//Prompting the user to enter in a choice
	cin>>answer;
	
	//Switch Statement telling the program what to do if the user enters in a specific character
	switch (answer)
	{
		case 'A':
		case 'a':
			cout<<"\n\n\tFile1.txt opening";
			filetopen="File1.txt";
			break;
		case 'B':
		case 'b':
			cout<<"\n\n\tFile2.txt opening";
			filetopen="File2.txt";
			break;
		case 'C':
		case 'c':
			cout<<"\n\n\tFile3.txt opening";
			filetopen="File3.txt";
			break;
		default:
			{cout<<"\n\n\tInvalid Input!";
			return 0;
			}
	}

	// Opening up the file user wanted
	
	infile.open(filetopen);
//************************************************************************ERROR MESSAGE IF FILE DOES NOT OPEN************************************************************************
	
	//In case, file doesnt open. Print out an error message

	if (!infile)
		{
			cout<<"Cannot open the input file" <<"\nProgram Terminates";
			
			//This return1 will exit the program due to error
			return 1;
	
		}
	//*******************************************************************************************************************************************************************************
	
	//**************************************************CONSOLE OPEN**************************************************************************************************************

	//Outputing data names that will be outputted on the screen. For instance, name, grades, letter grade
		cout<< " Student Statistics: ";
		cout<< "\n\n\n";
		cout<< "Name\t";
		cout<< "\tTest";
		cout<<"\t    Assignments";
		cout<< "\t      Points";
		cout <<"\t  Numeric ";
		cout<<"\tLetter ";
		cout<<endl;
		cout<<"\t\t Avg";
		cout<< "\t        Avg";
		cout<<"\t\t\t    Grd";
		cout<< "\t\t Grd ";
	

		//Enter in first name and last name in the filetxt
		infile>>firstname;
		infile>>lastname;

		//User gets to type in the test grades and assignment grades
		infile>>testgrade1 >>testgrade2;
		infile>> assign1;
		infile>>assign2;
		infile>>assign3;

	



//**************************************************************************WHILE LOOP OF THE FILEDATA ENTRY******************************************************************************
		while (infile)
		{
			
			//Print out the first and last name of the student
			cout<<left;
			cout<< "\n\n" << setw(11)<< firstname+ " " +lastname;

			//Calculate the testaverage. Round it to two decimal places, with trailing zeroes
			testaverage=(testgrade1+testgrade2)/2;
			cout<<setprecision(2);
			cout<<fixed;
			cout<<showpoint;
			cout<<right<<setw(10) <<testaverage;

			//Calculate the assignment average of each student
			assignaverage=(assign1+assign2+assign3)/3;
			cout<< setw(15);
			cout<<assignaverage;

			//Calculate the total points accumalated
			totalpoints= testgrade1+testgrade2+assign1+assign2+assign3;
			cout<< setw(16) <<totalpoints;

			//Calculate the the numericgrade. Add up assignments and test, then divide by 5 to find the numericavg grade
			numericgrade= (testgrade1+testgrade2+assign1+assign2+assign3)/5;
			cout<< setw(12) <<numericgrade;

			
			
			//you will use the if and else statement to calculate the letter grade

			
			//If number is higher then 90. Student gets an A
			if(numericgrade >=90 && numericgrade<=100)
			{
				cout<<setprecision(15);
				cout<<"\t  A";
			}
			//If number is higher then 80. Students gets a B
			else if (numericgrade>=80 && numericgrade<90)
			{
				cout<<setprecision(15);
				cout<<"\t  B";
			}

			//If number is higher then 70. Student gets a C
			else if (numericgrade>=70 && numericgrade<80)
			{
				cout<<setprecision(15);
				cout<<"\t  C";
			}
			//Any lower then 70, gets an F
			else 
			{
				cout<<setprecision(15)<<"\t  F";
			}
			
		//Minimum is declared as 100. If minimum is greater then numeric grade, then numeric grrade will become minimum
		//For instance, numeric grade is 89 and minimum is 100. Minimum is greater then 89. 89 becomes the minimum. 
		//During the next loop, 89 is stored in mininum.

			if(minimum>numericgrade)
				minimum=numericgrade;


		//Maximum is declared at zero. If maxium is less then numeric grade then maxium will become numeric grade.
			if (maximum<numericgrade)
				maximum=numericgrade;

			//Prevent the loop from becoming an infinite loop. Enter in names, scores, etc...again!
			infile>>firstname;
			infile>>lastname;
			infile>> testgrade1;
			infile>>testgrade2;
			infile>> assign1;
			infile>>assign2;
			infile>>assign3;

			//Sum is used in this loop to find the total sum of numeric grades
			sum=sum+numericgrade;
			
			//Add counter++ to determine the total number of students
			counter++;

			
				
		}
		
		
		

		//Find the average of students
		average=sum/counter;
//********************************************************************************DATA EMPTY. PRINT OUT 0!**************************************************************************
		//If the file is an empty set, I will set counter=0 and other values to zero.
			if (counter==0)
			{
			average=0;
			minimum=0;
			maximum=0;
//***********************************************************************************************************************************************************************************			
			
			
			}
			
		//Overall Class Statistics output

		//Display Class Statistic on console
		cout<<"\n\n\n\tClass Statistics ";

		//Display The minimum grade out of the whole class and have all the values right manipulated
		cout<<right;

		//Display number of students in class and the calculation of total students
		cout<<"\n\n\t\tNumber: "<< setw(7)<<counter;

		


		//Display the minimum grade out of the class

		cout<<"\n\n\t\tMinimum: " <<setw(6)<<setprecision(2)<<minimum;

		//Display the maximum grade out of the whole class
		cout<<"\n\n\t\tMaximum: " <<setw(6)<<setprecision(2)<<maximum;

		//Display class average 
		cout<<"\n\n\t\tAverage: " <<setprecision(2)<<setw(6)<<average;
		

		//Close the file
		infile.close();
		
		
		
		
	cout<<endl<<endl;
	_getch();
	return 0;
}




Cire,

Apologize for being too hasty. I did post a small version of my code on this post; however, seem it wasn't that clarified enough.Also, I didnt want to post my entire code earlier since it would have overwhelmed some of the user on this forum. Anyways, I have posted the entire code and im aware you didn't ask me for that. As for your question, yes the middle name is optional and I was thinking using a cin.peak function but not sure how to implement it.
The code was fine, however it doesn't tell us the format of the input in the file which is the most important thing when devising a strategy for extracting information from a file.
This might a really idiotic response by me, but what do you mean format...is it not already formatted in my code? In the file the user will enter in first name, last name, test1 grade, test2 grade, assignment 1 grade, assignment 2 grade, assignment3 grade in this order. Can you give me a hint possibly of what I can use...a basic hint for a beginner :/ please :)
This might a really idiotic response by me, but what do you mean format...


I mean the format of the file. Does the name occur on its own line in the file? Does all the data for one record occur on one line in the file? How is the data organized in the file? What is its format?

The way the file is read it might have a single space between each item you wish to extract.. or a newline, or a tab, or any number or combination of those between each item.

Assuming the format looks something like the following for each record:

first [optional middle name(s)] last g1 g2 g3\n


Code to read/display the names at close to the same level of the code you've written might look like the following:

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
#include <iostream>
#include <string>

bool readAndDisplayRecord(std::istream& is)
{
    std::cout << "Name: ";

    float grade;
    while (!(is >> grade))      // A name is not a number.
    {
        is.clear(); // clear the stream's error state.
        std::string name;
        if (!(is >> name))
            return false;

        std::cout << name << ' ';
    }

    std::cout << "\nGrades:\n\t" << grade << '\n';
    for (unsigned i = 0; i < 2; ++i)
    {
        if (!(is >> grade))
            return false;

        std::cout << '\t' << grade << '\n';
    }

    return true;
}


Demo: http://ideone.com/l3HAEc

Thank you for your response Cire. I partially do understand your code but not completely. I have tried manipulating my code around and doing something like this? Do you mind helping me improve this part of the code or I guess help me use a function like get, peek, putback.Also, that was the correct format I wanted :)

1
2
3
4
5
6
7
8
9
10
11
12
                              infile>>firstname;
		infile>>lastname;
			if (str.size()==1)
				middlename=lastname;

		//User gets to type in the test grades and assignment grades
		infile>>testgrade1 >>testgrade2;
		infile>> assign1;
		infile>>assign2;
		infile>>assign3;



One more thing, bool readAndDisplayRecord(std::istream& is)

what do the words inside the parenthesis mean. istream would be the same as cin, correct? and what is & is???

Should have posted this earlier but this is the format of my file


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

John mad A	90	70	90	95	80
Mary Ball	75	90	85	100	90
Abdul C	55	80	80	80	80
Maya D	95	80	100	80	80
Renee E	80	80	90	80	80
Hung Fu Khan	55	90	75	100	90
Lana G	85	90	100	90	90
Gerald H	35	60	55	60	80
Tyrone I	60	60	50	60	80
Rajiv J	85	75	95	80	80
Gabriela K	90	85	100	90	90
Randy KAS L	95	80	100	85	90
Andrea M	75	80	80	85	80
Irina N	100	95	100	100	90
Jose O	85	90	90	90	80
That data seems to have each field separated by a tab character. The individual parts of the name are separated by spaces.

So you could use getline() with a delimiter of '\t' to get the whole of the name.
Unless you really need to manipulate the name further, you could just keep the whole string as it is.
Chervil, so I can basically declare string name and do this following process.

1
2
3
4

string fullname;
getline(infile,fullname);
Almost.
Quote: "use getline() with a delimiter of '\t'"

You need to specify the delimiter for getline. Like this:
getline(infile, fullname, '\t');

If you omit the parameter, it uses the default of '\n'.
Just to refresh my mind. The getline (infile,full name) it will read a string until new line is found?

In your code, it will read a string until tab is found, correct chervil? :)
Just curious, first time I'm actually learning to use getline in this way. Can I add another delimiter or is there a another restriction. If I can how can I?
Pages: 12