reading from file sorting and writing to a file

The program is to read a students id number class status and credits. sort out only the undergraduates and assign a class status ie. freshman, sophomore, junior, and senior, based on credits and sort them highest to lowest in a file.

file looks like this

gh56432 u 12
gy56432 g 113
ab45327 a 40
hu54638 u 120
and so on

u = undergraduate
a = associates
g = graduate

the problem i'm having is the program is not writing anything to a file.

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

using namespace std;

enum schoolClass{FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};

const string CREDITS = "CREDITS.txt";
const string SORTED = "SORTED.txt";
const int STUDENT = 1000;
const int ID = 1000;

void Header ();
string GetFileData (string[], schoolClass[], int&, bool&);
void SelectionSort (string[], int);
void DisplayResults (string[], schoolClass[], int, bool&);

int main()
{
	string id [STUDENT];
	schoolClass classStatus[STUDENT];
	bool dataReadError = false;
	int count = 0;
	
	Header();
	
	GetFileData (id, classStatus, count, dataReadError);
	
	if (!dataReadError)
	{
		SelectionSort (id, count);
		DisplayResults(id, classStatus, count, dataReadError);
		
		return 0;
	}
	else 
	  return 5;
	  
}	//end main

void Header()
{
	cout << "this program does this";
	cout << endl << endl;
}	//end header function

string GetFileData (string arrayId[], schoolClass classStatus[], int& count, bool& dataReadError)
{
	
	ifstream inStudentData;
	bool statusSpaceAvailable = true;
	char studentStatus;
	string id;
	char level;
	int credits;
	schoolClass temp;
	int noc;
	
	dataReadError = false;
	
	inStudentData.open(CREDITS.c_str());
	
	if (!inStudentData)
	{
		cout << "Error opening file " << CREDITS << endl;
	    dataReadError = true;
	}
	else
	{
		inStudentData >> id;
		
		while (inStudentData && statusSpaceAvailable)
		{
		    
		    inStudentData >> level >> credits;
		    
		    if (level == 'u')
		    {
		    	while (classStatus)
				{
	 			    if (noc >= 0 && noc <= 29)
					{
   					     level = FRESHMAN;
					}
					else if (noc >= 30 && noc <= 59)
					{
		 	 		 	 level = SOPHOMORE;
 			        }
					else if (noc >= 60 && noc <= 89)
					{
					 	 level = JUNIOR;
				    }
					else
					{
					 	 level = SENIOR;
				    }
			    }
			   // NEED TO CLALL A FUNCTION TO DETERMINE AND RETURN THE CORRECT CLASS LEVEL (ENUM) TO THE TEMPORARY VARIABLE temp
           	   // BASED ON THE NUMBER OF CREDITS 
 		
		       if (count < STUDENT)
		       {
		       	  arrayId[count] = id;
		       	  classStatus[count] = temp;
		       	  count++;
			   }
			   else
			     statusSpaceAvailable = false;
		    }
		    
		    inStudentData >> id;
		    //cout << ;
	    }
	    
	    if (!statusSpaceAvailable)
	      cout << "Error -- too much data in file. only first "
		  	   	  << STUDENT << " students will be used" << endl << endl;
    }
    return id;

}

void SelectionSort (string id[], int count)
{
  int curTop,            // Current top of unsorted list
      tryIdx,            // Position to compare value to
      minPosition;       // Position of smallest value
  string    temp;              // Temp value for swapping

  // for each item in the list (top to bottom)
  for (curTop = 0; curTop < count - 1; curTop++) 
  {
          
     minPosition = curTop;      // start with current top as smallest value

     // find smallest value from curTop down
     for (tryIdx = curTop + 1; tryIdx < count; tryIdx++) 
         if (id[tryIdx] < id[minPosition])
             minPosition = tryIdx;
				
     // if smallest not at curTop, swap with curTop
     if (minPosition != curTop) 
     {
         temp = id[curTop];
         id[curTop] = id[minPosition];
         id[minPosition] = temp;
     }   // end swap

  }  // end for
 
  return;
} // end selectionSort

void DisplayResults (string id[], schoolClass classStatus[], int count, bool& dataReadError)
{
	ofstream sortedOut;
	sortedOut.open (SORTED.c_str());
	
	if (!sortedOut)
	{
		cout << "Error opening file " << CREDITS << endl;
	    dataReadError = true;
	}
	
	for(int index = 0; index < count; index++)
	  cout << id[index] << ' ' << classStatus[index] << endl;
	  sortedOut << id;
	  
	  sortedOut << classStatus;
	  sortedOut << count;
	  
	  return;
	
	
}
Some helpful compiler warnings:
warning: variable 'temp' is uninitialized when used here (line 106)
classStatus[count] = temp;
                     ^~~~

note: variable 'temp' is declared here (line 58)
schoolClass temp;
^


warning: variable 'noc' is uninitialized when used here (line 83)
if (noc >= 0 && noc <= 29)
    ^~~

note: initialize the variable 'noc' to silence this warning (line 59)
int noc;
       ^
        = 0



Your program gets stuck in an infinite loop on line 81.
Last edited on
I cant seem to breat it out of the loop everything i try does not work. Any advise?
Do you need that loop there? Do you need to do what is in the loop multiple times? From what is inside the loop, I would say that the loop is not needed. Ortherwise classStatus needs to become false or you need to break; from the loop somehow.
Topic archived. No new replies allowed.