Progress! Then I hit a wall :)

It's a menu. So far I'm just working on the first 2 menu options. Couple of issues:
1. option #1 - Trying to save sorted ID's to an array to then compare to id array in grade file. Not working.
2. When the ID is not in the file for option#2, my 'try again' comment not working right.


HELP!


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

using namespace std;

void insertionSort_byInteger(int arrtosort[], int size);
void insertionSort_ByString(string arrtosort[], int size);

const int MAXSTUDENTS = 30; 

const char FILENAME[40] = "TestScores2.txt";
const char NEWFILENAME[40] = "TestScores_Grades2.txt";
const char SORTEDIDS[40] = "SortedIDs.txt";

int main()
{ 
    
    string fname[MAXSTUDENTS];
    string lname[MAXSTUDENTS];
    string grades[MAXSTUDENTS];
    int ids[MAXSTUDENTS], scores[MAXSTUDENTS], sortids[MAXSTUDENTS];
    ifstream inFile;    
    int id, idx, score, score2, s = 0, avg, totalgrade = 0, numofstudents = 0;
    int num, target, p = 0, percent;
    string grade, grade2, firstName, lastName, fnames, lnames, targetGrade, targetLetter;
    inFile.open(FILENAME);
    
    //read file and get average
    while (s < MAXSTUDENTS)
    {
          inFile >> id;
          inFile >> firstName;
          inFile >> lastName;
          inFile >> score;
          if (score >= 0 && score <= 10)
          {
               totalgrade = totalgrade + score;
               numofstudents++;
          }
          s = s + 1;
    }
    avg = totalgrade/numofstudents;  
    //cout << "The average score is: " << avg << endl << endl;
    inFile.clear();
    inFile.seekg(0);
    
    //read file, generate letter grade and create new file
    ofstream outFile1;
    outFile1.open(NEWFILENAME);
    
    for(idx = 0; idx < MAXSTUDENTS; idx++)
    {
         inFile >> id >> firstName >> lastName >> score;
         if ( score != -1 )
         {
              if (score == avg || score == avg +1)
                   grade = "B";
              else if (score == avg + 2 || score == avg + 3)
                   grade = "A-";
              else if (score >= avg +4 || score == 10 )
                   grade = "A";
              else if (score == avg - 1)
                   grade = "C";
              else if (score >= 0 && score <= (avg - 2))
                   grade = "F";
              outFile1 << id << " " << firstName << " " << lastName << " " << score << " " << grade << endl;
         }
         else
         outFile1 << id << " " << firstName << " " << lastName << " " << score << endl;
         //cout << id << " " << firstName << " " << lastName << " " << score << endl;
    }
    inFile.close();
   
    //read letter grade file and launch menu
    ifstream inFile2;
    inFile2.open(NEWFILENAME);
    idx = 0;
    
    while (idx < MAXSTUDENTS)
    {
         inFile2 >> ids[idx] >> fname[idx] >> lname[idx] >> scores[idx];
         if ( scores[idx] != -1 )
           inFile2 >> grades[idx];
         cout << ids[idx] << " " << fname[idx] << " " << lname[idx] << " " << scores[idx] << " " << grades[idx] << endl;
         idx++;
    }
    inFile2.close();
    cout << endl;
    
    do
    {
         cout << setw(30) << "QUIZ RESULTS MENU     " << endl;
         cout << setw(30) << "***************************" << endl << endl;
         cout << "   1. " << left << setw(30) << "Sort by Student ID" << endl;
         cout << "   2. " << setw(30) << "Search for a Student ID and Display Their Grade" << endl;
         cout << "   3. " << setw(30) << "Search by Letter Grade" << endl;
         cout << "   4. " << setw(30) << "Sort by Numeric Grade" << endl;
         cout << "   5. " << setw(30) << "Calculate Percentage of Students for a Letter Grade" << endl;
         cout << "   6. " << setw(30) << "Sort Alphabetically by Student Last Name" << endl;
         cout << "   7. " << setw(30) << "Exit the Program" << endl << endl;
    
         cout << right << "Enter an option: " ;
         cin >> num;
         cout << endl;
         
         ofstream sortedId;
         ifstream inFile3;
         sortedId.open(SORTEDIDS);
         inFile3.open(NEWFILENAME);
         
         if (num == 1)
         {     
              insertionSort_byInteger(ids, MAXSTUDENTS);
              
              for (idx = 0; idx < MAXSTUDENTS; idx++)
              {     
                    sortedId << sortids[idx] << fnames << lnames << score2;
                    if ( score2 != -1 )
                         sortedId << grade2;
                    cout << sortids[idx] << endl;
                    idx++;
              }
              sortedId.close();
              
              ifstream sortedId;
              sortedId.open(SORTEDIDS);
              
              while (!inFile3.eof())
              {
                    if(sortids[idx] == ids[idx])
                    {
                         cout << ids[idx] << fname[idx] << lname[idx] << scores[idx];
                         if(scores[idx] != -1)
                              cout << grades[idx] << endl;
                    } 
              }
         }   
         else if (num == 2)
         {
    
              cout << "Enter a student ID: ";
              cin >> target;   
              cout << endl;          
              for(idx = 0; idx < s; idx++)
              {
                   inFile2 >> ids[idx] >> scores[idx] >> grades[idx];
                   if(ids[idx] == target)
                   {
                        cout << ids[idx] << " " << fname[idx] << " " << lname[idx] << " " << scores[idx] << " " << grades[idx] << endl;
                   }
                   //else 
                        //cout << "This ID is not found in the list. Please try again." << endl;
              }
              if (ids[idx] != target)
                   cout << "This ID is not found in the list. Please try again." << endl << endl;
                       
         }
         
         inFile2.clear();
         inFile2.seekg(0);

     
    }
    while (num > 0 && num <= 5);

    system("PAUSE");
    return EXIT_SUCCESS;
    
}

void insertionSort_byInteger(int arrtosort[], int size)
{
    int temp = arrtosort[0];
    for(int i = 1; i <= size; i++)
    {
        temp = arrtosort[i];
        int j = 0;
        for(j = i; j > 0; j--)
            if(temp < arrtosort[j - 1])
               arrtosort[j] = arrtosort[j - 1];
            else break;
        arrtosort[j] =  temp;
    }
}
void insertionSort_ByString(string arrtosort[], int size)
{
    string temp = arrtosort[0];
    for ( int i = 1; i <= size; i++ )
    {
        temp = arrtosort[i];
        int j = 0;
        for ( j = i; j > 0; j--)
            if ( temp < arrtosort[j - 1])
               arrtosort[j] = arrtosort[j - 1];
            else break;
        
        arrtosort[j] = temp;
    } 
}
Can you post compiler errors ?
It's actually compiling. No errors now. but I'm doing something wrong in #1 and I can't tell what it is. The issue is between lines 115 - 140 when I try to save the sorted ID's into a new array. That data is not storing properly.
Arrays with a number of elements, size, may have indices ranging from 0 to size-1. I say this, because your insertionSort implementations treat 'size' as a valid index.

Note that in line 117 you sort an array called ids. In the for loop following that call to insertionSort, you process sortids as if it were the array you'd just sorted -- it is not.
Oh that helped! I see the sorted sorted array now. So I have to compare that to the _Grades.txt file to also sort the other items according to the sorted ID. I'm trying to do that with the while loop on line 131, but I'm not getting the output. I don't think I'm looping it correctly?




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

using namespace std;

void insertionSort_byInteger(int arrtosort[], int size);
void insertionSort_ByString(string arrtosort[], int size);

const int MAXSTUDENTS = 30; 

const char FILENAME[40] = "TestScores2.txt";
const char NEWFILENAME[40] = "TestScores_Grades2.txt";
const char SORTEDIDS[40] = "SortedIDs.txt";

int main()
{ 
    
    string fname[MAXSTUDENTS];
    string lname[MAXSTUDENTS];
    string grades[MAXSTUDENTS];
    int ids[MAXSTUDENTS], scores[MAXSTUDENTS], sortids[MAXSTUDENTS];
    ifstream inFile;    
    int id, idx, score, score2, s = 0, avg, totalgrade = 0, numofstudents = 0;
    int num, target, p = 0, percent;
    string grade, grade2, firstName, lastName, fnames, lnames, targetGrade, targetLetter;
    inFile.open(FILENAME);
    
    //read file and get average
    while (s < MAXSTUDENTS)
    {
          inFile >> id;
          inFile >> firstName;
          inFile >> lastName;
          inFile >> score;
          if (score >= 0 && score <= 10)
          {
               totalgrade = totalgrade + score;
               numofstudents++;
          }
          s = s + 1;
    }
    avg = totalgrade/numofstudents;  
    //cout << "The average score is: " << avg << endl << endl;
    inFile.clear();
    inFile.seekg(0);
    
    //read file, generate letter grade and create new file
    ofstream outFile1;
    outFile1.open(NEWFILENAME);
    
    for(idx = 0; idx < MAXSTUDENTS; idx++)
    {
         inFile >> id >> firstName >> lastName >> score;
         if ( score != -1 )
         {
              if (score == avg || score == avg +1)
                   grade = "B";
              else if (score == avg + 2 || score == avg + 3)
                   grade = "A-";
              else if (score >= avg +4 || score == 10 )
                   grade = "A";
              else if (score == avg - 1)
                   grade = "C";
              else if (score >= 0 && score <= (avg - 2))
                   grade = "F";
              outFile1 << id << " " << firstName << " " << lastName << " " << score << " " << grade << endl;
         }
         else
         outFile1 << id << " " << firstName << " " << lastName << " " << score << endl;
         //cout << id << " " << firstName << " " << lastName << " " << score << endl;
    }
    inFile.close();
   
    //read letter grade file and launch menu
    ifstream inFile2;
    inFile2.open(NEWFILENAME);
    idx = 0;
    
    while (idx < MAXSTUDENTS)
    {
         inFile2 >> ids[idx] >> fname[idx] >> lname[idx] >> scores[idx];
         if ( scores[idx] != -1 )
           inFile2 >> grades[idx];
         cout << ids[idx] << " " << fname[idx] << " " << lname[idx] << " " << scores[idx] << " " << grades[idx] << endl;
         idx++;
    }
    inFile2.close();
    cout << endl;
    
    do
    {
         cout << setw(30) << "QUIZ RESULTS MENU     " << endl;
         cout << setw(30) << "***************************" << endl << endl;
         cout << "   1. " << left << setw(30) << "Sort by Student ID" << endl;
         cout << "   2. " << setw(30) << "Search for a Student ID and Display Their Grade" << endl;
         cout << "   3. " << setw(30) << "Search by Letter Grade" << endl;
         cout << "   4. " << setw(30) << "Sort by Numeric Grade" << endl;
         cout << "   5. " << setw(30) << "Calculate Percentage of Students for a Letter Grade" << endl;
         cout << "   6. " << setw(30) << "Sort Alphabetically by Student Last Name" << endl;
         cout << "   7. " << setw(30) << "Exit the Program" << endl << endl;
    
         cout << right << "Enter an option: " ;
         cin >> num;
         cout << endl;
         
         ofstream sortedId;
         ifstream inFile3;
         sortedId.open(SORTEDIDS);
         inFile3.open(NEWFILENAME);
         
         if (num == 1)
         {     
              insertionSort_byInteger(ids, MAXSTUDENTS-1);
              
              for (idx = 0; idx < MAXSTUDENTS; idx++)
              {     
                    sortedId << sortids[idx] << fnames << lnames << score2;
                    if ( score2 != -1 )
                         sortedId << grade2;
                    idx++;
              }
              sortedId.close();
              
              ifstream sortedId2;
              sortedId2.open(SORTEDIDS);
              
              while (!inFile3.eof())
              {
                    if(sortids[idx] == ids[idx])
                    {
                         cout << ids[idx] << fname[idx] << lname[idx] << scores[idx];
                         if(scores[idx] != -1)
                              cout << grades[idx] << endl;
                    } 
              }
         }   
         else if (num == 2)
         {
    
              cout << "Enter a student ID: ";
              cin >> target;   
              cout << endl;          
              for(idx = 0; idx < s; idx++)
              {
                   inFile2 >> ids[idx] >> scores[idx] >> grades[idx];
                   if(ids[idx] == target)
                   {
                        cout << ids[idx] << " " << fname[idx] << " " << lname[idx] << " " << scores[idx] << " " << grades[idx] << endl;
                   }
                   //else 
                        //cout << "This ID is not found in the list. Please try again." << endl;
              }
              if (ids[idx] != target)
                   cout << "This ID is not found in the list. Please try again." << endl << endl;
                       
         }
    
    }
    while (num > 0 && num <= 5);

    system("PAUSE");
    return EXIT_SUCCESS;
    
}

void insertionSort_byInteger(int arrtosort[], int size)
{
    int temp = arrtosort[0];
    for(int i = 1; i <= size; i++)
    {
        temp = arrtosort[i];
        int j = 0;
        for(j = i; j > 0; j--)
            if(temp < arrtosort[j - 1])
               arrtosort[j] = arrtosort[j - 1];
            else break;
        arrtosort[j] =  temp;
    }
}
void insertionSort_ByString(string arrtosort[], int size)
{
    string temp = arrtosort[0];
    for ( int i = 1; i <= size; i++ )
    {
        temp = arrtosort[i];
        int j = 0;
        for ( j = i; j > 0; j--)
            if ( temp < arrtosort[j - 1])
               arrtosort[j] = arrtosort[j - 1];
            else break;
        
        arrtosort[j] = temp;
    } 
}


Just some ideas to reorganise your code a bit better.

You have a lot of code in main, it would be better to make more use of functions, as shown by your comments.

Lines 33 - 46 could be in an Average function.
Lines 51 - 92 Could this be combined into 1 function - do you really need to write an intermediate file?
LInes 96 - 107 A ShowMenu function
Lines 115 - 162 Have a switch to take care of each menu option, instead of all the else if staements. Each case should call a function. This is much more scalable.

Instead of the outer do loop, use a while loop controlled by a bool variable:

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
bool Quit = false;
unsigned Option;

while (!Quit) {
     ShowMenu();

     cout << right << "Enter an option: " ;
     cin >> Option;

     switch(Option) {
           case 1:
                    SortStudentId();
                     break;
           case 2:
               //Option 2 function  call

           case 3:
               //Option 3 function call

           //similar for options 4 to 6

           case 7:   //user wants to exit
                Quit = true;
                 break;

           default:
                  std::cout << "Invalid option" << std::endl;
                   break;
     }
return EXIT_SUCCESS;

} //end of main 


You can use return statements in the cases, but his way allows the program to do other things after the switch.

If you do all these things, your program will be much nicer, IMO.

HTH & Happy silly season :D cheers


Topic archived. No new replies allowed.