Scoring data from a text file, loops problem

Hi, I'm new to the forums. I just started programming this past September, so I am still a novice.

I am having problems with getting the entire data from the text file.

The text file looks like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
A A
5 
11 
9 
5 
8 
5 
3
-1
R D
11
2
3
12
8
11
9
-1


There is more to the text file, but for now processing those two students is good enough to get me in the right direction.

The program output should look like this.

Name 1  2  3  4  5  6  7  8  9  10  Low  Avg
A.A  5 11  9  5  8  5  3              3  7.2
R.D 11  2  3 12  8 11  9              2  9.2


Right now, all I am able to get out of my code is this.

Name 1  2  3  4  5  6  7  8  9  10  Low  Avg
A.A  5 11  9  5  8  5  3              3  7.2


I have tried several different loops and I am not able to get the program to get the data from the second student when it is done with the first one.

The program: Pretty much what the program does is get the data from the Text file. It puts all the scores of the student in the table, leaves a blank space if the student didn't do a quiz/test, drops the lowest quiz, and then averages the students score.

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

int main()
{
	ifstream scoredata;						
																
 	scoredata.open("ScoreData.txt");	
																
  if(scoredata.fail())					
  {															
	  cout << "File not found. Program terminated." << endl;
	  return 1;
  }

     //Prints the table that indicates names, quiz number, lowest score, and average.
  cout << "Name";
  for (int i = 1; i <= 10; i++)
	  cout << setw(5) << i;
  cout << setw(5) << "Low";
  cout << setw(5) << "Avg";
  cout << endl;
	  

 while(!scoredata.eof())
  {
	  //////////////////////////////////////////////////////////////////

	 char student_first_name, student_last_name;
	 int quiz_score = 0;
	 double a = 0;
	 double b = 0;
	 double average = 0.0;
	 int y = 20;
	 int lowest_score;
	 ///////////////////////////////////////////////////////////////////
	 
	 
	 
								
			
	scoredata >> student_first_name >> student_last_name;
	cout << student_first_name << "." << student_last_name << setw(6);
	
    //Students scores, puts them in the table
	 for(int i = 1; i <= 10; i++)
	{	 

		scoredata >> quiz_score;
		while(quiz_score != -1)
		{
		
		cout << quiz_score << setw(5);
		a++;
		b += quiz_score;
		
		//Gets the Lowest score out of the set
		if(quiz_score < y)
		{
			lowest_score = quiz_score;
			y = quiz_score; 
		}
		else
		y = quiz_score;

		break;
		}

		// If the students didn't do the test/quiz the space is left blank.
		if(quiz_score == -1)
		cout << setw(5) << " ";
																					

	}		
	 	
	 //Lowest Score
	 if(a > 1)
	 cout << setw(5) << lowest_score;
	 else 
	 cout << setw(5) << "N/A";

	 //Average Score and it also drops the lowest score.
	 static_cast<double>(a);
	 static_cast<double>(b);
	 
	 if(a > 1)
	 {
	  a -= 1;
	  average = (b - lowest_score) / a; 
	  cout << fixed << setprecision(1) << showpoint;
	  cout << setw(5) << average << endl;
	 }
	 else if(a == 1)
	 cout << setw(5) << b;
	 break;	

							
							
								
	 ////////////////////////////////////////////////////////////////
	 
  }
 scoredata.close();
 
   cout << endl; 
   

   return 0;
}


All I am missing is a loop, I think, there might be something else I am over looking. but pretty much I need a second loop that starts processing the second kid after the first kid is done processing.

I just need to be pointed in the right direction. :)

Seems some codes need to be reviewed, but check the line 97(break;) first to see if you really want to quit the loop.
Probably not the perfect style, i had not a lot of time to care of this.
Check if the average result and precision is correct.


// st.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;

int main()
{
ifstream scoredata;
scoredata.open("ScoreData.txt");
if(scoredata.fail())
{
cout << "File not found. Program terminated." << endl;
return 1;
} //end if




//Prints the table that indicates names, quiz number, lowest score, and average.

cout << "Name";

for (int i = 1; i <= 10; i++)
cout << setw(5) << i;
cout << setw(5) << "Low";
cout << setw(5) << "Avg";
cout << endl;




//////////////////////////////////////////////////////////////////



char student_first_name, student_last_name;

int quiz_score;

double a;

double b;

double average;


int lowest_score;

///////////////////////////////////////////////////////////////////


while(!scoredata.eof())

{

quiz_score = 0;

a = 0;

b = 0;

average = 0.0;


lowest_score =20;


scoredata >> student_first_name >> student_last_name;

cout << student_first_name << "." << student_last_name << setw(6);



//Students scores, puts them in the table

for(i = 1; i <= 11; i++)

{


if (quiz_score != -1) scoredata >> quiz_score;
if (quiz_score != -1)
{

cout << quiz_score << setw(5);
a++;
b += quiz_score;

//Gets the Lowest score out of the set
if(quiz_score < lowest_score)
{
lowest_score = quiz_score;
}


} //end if quiz != -1

// If the students didn't do the test/quiz the space is left blank.
else
if (i < 11 ) cout << setw(5) << " ";


} //end for i 1 - 10


//Lowest Score
if(a > 1)
cout << setw(5) << lowest_score;
else
cout << setw(5) << "N/A";


//Average Score and it also drops the lowest score.
static_cast<double>(a);
static_cast<double>(b);

if(a > 1)
{
a -= 1;
average = (b - lowest_score) / a;
cout << fixed << setprecision(1) << showpoint;
cout << setw(5) << average << endl;
}
else if(a == 1)
cout << setw(5) << b;



cout << endl;
////////////////////////////////////////////////////////////////

} // end while not end of file


scoredata.close();

cout << endl;

return 0;
} // end main

Last edited on
Topic archived. No new replies allowed.