bubble sort Problem

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
#include<iostream>
#include<fstream>
using namespace std;
struct students{
       int ID;
       int q1;
       int q2;
       int e1;
       int e2;
       int assg1;
       int assg2;
       int assg3;
       };

int searchList(students grades[], int Num_Students, int ID){
                    int index = 0;
                    bool found =false;
                    while (index < Num_Students && !found){
                          if (grades[index].ID == ID){
                                            found = true;
                                            cout << grades[index].q1;
                                            cout << grades[index].q2;
                                            cout << grades[index].e1;
                                            cout << grades[index].e2;
                                            cout << grades[index].assg1;
                                            cout << grades[index].assg2;
                                            cout << grades[index].assg3;
                                            break;
                          }
                          index++;
                    }
                    if( found )
                           return index;
                    else
                            return -1;
}

int searchList2(students grades[], int Num_Students, int ID){
    int LAB =15;
    int index = 0;
    bool found = false;
    double total = 0;
    while (index < Num_Students && !found){
          if (grades[index].ID == ID){
                               found = true;
                               total = (grades[index].q1+grades[index].q2
                               +grades[index].e1+grades[index].e2+grades[index].assg1+
                               grades[index].assg2+grades[index].assg3)*0.1 + ((grades[index].e1+grades[index].e2)/2)*.15 + LAB;
                             }
                             index++;
                           }
                           if( found ){
                               if (total < 60)
                                              cout << "You have an F" << endl;
                                              else if(total <69)
                                              cout << " You have a D" << endl;
                                              else if(total < 79)
                                              cout << "You have a C" << endl;
                                              else if(total < 89)
                                              cout << "You have a B" << endl;
                                              else if( total <= 100)
                                              cout << "You have an A" << endl;
                                              return 1;
                                              }
                                          else
                                            return -1;
}

void showList(int grades[], int num){
     for(int m = 0; m < num; m++)
             cout << grades[m] << " ";
     cout << endl;
}

void sortField( students grades[], int num, string sortThis){
     bool swap;
     int temp;
     do{
         swap = false;
         for(int k = 0; k < (num - 1); k++){
                 if(grades[k].ID > grades[k + 1].ID){
                              temp = grades[k].ID;
                              grades[k] = grades[k + 1];
                              grades[k +1].ID = temp;
                              swap = true;

                 }
         }
     }while (swap);
}

                
int main(){
    
    int ID;
    char command;
    int Num_grades = 65;

    students grades[Num_grades];
    int i =0;
    int Num_Students = 65;
    int sortWhat;
    char sortField;
    string sortList;
    ifstream infile;
    int sortThis;
    infile.open("grades.txt");
    
    while(!infile.eof()){
                infile >> grades[i].ID;
                infile >> grades[i].q1;
                infile >> grades[i].q2;
                infile >> grades[i].e1;
                infile >> grades[i].e2;
                infile >> grades[i].assg1;
                infile >> grades[i].assg2;
                infile >> grades[i].assg3;
                i++;
    }

    ofstream outfile;
    outfile.open("report.txt");
    
    bool exit = false;

    do{
         cout << "1: Publish grade report" << endl;
         cout << "2: Search for student grade by ID" << endl;
         cout << "3: Sort grades" << endl;
         cout << "4: Projected grade" << endl;
         cout << "5: Distribute grades" << endl;
         cout << endl;
         cout << "Enter an operator: ";
         cin >> command;
         
         switch (command) {

           case '1':
                for(int j = 0; j<0;j++){
                        outfile << grades[j].ID;
                        j++; 
                }
           break;

           case '2':
                cout << "Enter an ID" << endl;
                cin >> ID;
                if( searchList( grades, Num_Students, ID ) == -1);
                cout << "Student not found!" << endl; 
                break;

           case '3':
                do{
                   cout << "1: Student ID" << endl;
                   cout << "2: Quiz 1 Grade" << endl;
                   cout << "3: Quiz 2 Grade" << endl;
                   cout << "4: Exam 1 Grade" << endl;
                   cout << "5: Exam 2 Grade" << endl;
                   cout << "6: Assignment 1 Grade" << endl;
                   cout << "7: Assignment 2 Grade" << endl;
                   cout << "8: Assignment 3 Grade" << endl;
                   cout << endl;
                   cout << "Enter field to be sorted(0-7): ";
                   cin >> sortWhat;
                   
                   switch (sortThis){ 
                          case '1':
                               sortField(students grades[], int num, string sortThis);
                               showList(grades[].ID, Num_Students);
                               break;
                          case '2':
                               sortList(grades[].q1, Num_Students);
                               showList(grades[].q1, Num_Students);
                               break;
                          case '3':
                               sortList(grades[].q2, Num_Students);
                               showList(grades[].q2, Num_Students);
                               break;
                          case '4':
                               sortList(grades[].e1, Num_Students);
                               showList(grades[].e1, Num_Students);
                               break;
                          case '5':
                               sortList(grades[].e2, Num_Students);
                               showList(grades[].e2, Num_Students);
                               break;
                          case '6':
                               sortList(grades[].assg1, Num_Students);
                               showList(grades[].assg1, Num_Students);
                               break;
                          case '7':
                               sortList(grades[].assg2, Num_Students);
                               showList(grades[].assg2, Num_Students);
                               break;
                          case '8':
                               sortList(grades[].assg3, Num_Students);
                               showList(grades[].assg3, Num_Students);
                               break;
                          default:
                                  cout << "Invalid Command" << endl;
                   }
                }while( !exit ); 
                break;

           case '4':
                cout << "Enter an ID" << endl;
                cin >> ID;
                if( searchList2(grades, Num_Students, ID) == -1 )
                  cout << "Student not found!" << endl;
                break;

           case '5':
                break;     
           default:
                 exit = true;
           }
    }while( !exit );                                   
    
    infile.close();
    system("PAUSE");
    return 0;
}




I need help with above program. The program is supposed to read ID#'s for students as well as grades from a file called "grades.txt" After the program reads the the file it displays a menu as follows: 0. Publish(publish contents of file to output file called "report.txt"); 1. Search(program prompts user to enter ID and display the grades for that specific student); 2. Sort( ask user which field to sort records: valid fields are 0-7 and then print specific records in ascending order like user types 4 and shows all students Exam 2 grades); 3. Projected Grade(calculate official grade/display using formula: (quiz1+quiz2+exam1+exam2+assignment1+assignment2+assignment3)*0.1+((exam1+exam2)/2)*0.15+LAB =15 as well as display letter grade: A=90-100, B = 80-89, C= 70-79, D =60-69, F = <60); 4. Distribution(display number of A's, B's, C's, D's and F's in class based on students scores). The "grades.txt" file looks similar to this: 116807 65 31 41 19 15 72 11 // ID quiz1 quiz2 exam1 exam2 assignment1 assignment2 assignment3. The problem is my bubble sort function call "sortField." It won't compile and I don't know why.

Everybody must have the same assignment.
Last edited on
What is/are the compile error(s)?

The function as is won't work anyway.

This would be a fantastic candidate for a boost::multi_index_container! One nasty, 60-line long declaration for the container of students and then you get all the sorting for free!
I am only supposed to use a selection or bubble sort. Anyway, the case 1 in the switch statement "sortThis" is where the problem is. I am trying to call the "sortField" function but it won't compile. The error I get for this is "sortField can't be used as a function". So, is there something else that I am missing?
Last edited on
You have to remove the types (student, int, string) from the call. It looks to the compiler like you are trying to declare a function, not call it.
So, is the "sortField"call supposed to look like this: sortField(grades[], num, sortThis);
sortField( grades, num, sortThis )

except that num is undeclared in main() [you want the number of students in the array... you seem to have a variable that tracks that] and sortThis is declared as an int in main() but the function expects a string instead.
OK, my new case looks like this:
1
2
3
 switch (sortThis){ 
                          case '1':
                               sortField(Num_Students[], int num, sortThis);

Now, the error is: "switch character not an integer" but the variable sortThis in " int main()" is declared as a string. So, should I have a different variable in the switch statement?
Last edited on
Yes, you can only use an integer. You could use the strtol() function, wich converts a string to an integer :(http://www.cplusplus.com/reference/clibrary/cstdlib/strtol.html)
Keep in mind that if the value of the string is invalid, it will convert 0 to the integer. Make sure to check or that's the case, since it's probably not what you want.
I wrote the entire program in under 300 lines of text (my tool says 230 lines of code) using boost::lambda, boost::lexical_cast, and boost::multi_index::multi_index_container.

Despite the shortness of it it takes almost 30 seconds to compile on 3.0GHz machine!
Fun little project...
Topic archived. No new replies allowed.