Scoring data from a text file, loops problem

(Sorry I posted this here and on the other forum, but I was not quite sure which one was best to use.)

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 << endl;
	 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. :)
Last edited on
for(int i = 1; i <= 10; i++)

Your loop only runs to line 10 of the text file and exits.
Thats why you only get the first kid result .

//Edit: This might not be right . I will try to run the code
Last edited on
Line 97: you have a break causing you to exit the loop after the iteration. Why is it there? It doesn't appear to be needed.
That was part a previous loop that I had, lines 44 through 96 were all nested inside a loop I made but didn't work, it was an accident, I didn't remove it.

//edit: I fixed it.

I guess I do need it... If I take it off, all the program does is this
"00 000 00 00 00 00 00 00 00"
Last edited on
So does the problem still persist?
yes. If I take the break off, all it does is print in the console a bunch of zeros with no end.

with the break it prints the first students score and average.
What is the "for i, 1-10" loop for? It appears that your "while quiz_scores != 1" loop is what you want.
It just creates a table, it puts all the quizes/tests in order, from 1-10, if the student didn't take all 10 then it only shows the ones he took.
You need to rethink you project. There aren't always 10 grades to read. You should keep reading grades until one of them is less then 0, not read exactly 10.
Topic archived. No new replies allowed.