# Thread: Why isn't my program working?

1. ## Why isn't my program working?

The program is suppose to declare a structure of student record:
• Student name – string type
• Average score – float

Use a loop to enter 10 student names and their 3 test scores
Calculate the average of each student
Sort the array into ascending order
Print the student record with the highest average
Print the student with the lowest average

This is the source code that I have created:
Code:
```#include <iostream>
#define TAB '\t'
#define MAX 13
// Creating the structure/array
struct studentrec {
char name[15];
int test1, test2, test3;
float Ave;
};
main() {
struct studentrec testrec[MAX];
int i;	// loop control
// Inputing data into the structure
for (i = 0; i < MAX; i++) {
cout << "Name" << endl;
cin >> testrec[i].name;
cin >> testrec[i].test1;
cin >> testrec[i].test2;
cin >> testrec[i].test3;
// Calculating the average of each student
testrec[i].Ave = (testrec[i].test1 + testrec[i].test2 + testrec[i].test3) / 3;
// Setting the structure
struct studentrec temp;
for (int k = 0; i < MAX; i++) {
for (int j = 0; j < MAX; j++) {
if (testrec[i].Ave > testrec[j+1].Ave) {
temp = testrec [j];
testrec[j] = testrec [j+1];
testrec[j+1] = temp;
} // end of if (testrec[i].Ave > testrec [j+1]) statement
// Printing the structure
for (i+0; i < MAX; i++) {
cout << testrec[i].name
<< testrec[i].test1 << TAB
<< testrec[i].test2 << TAB
<< testrec[i].test3 << TAB
<< testrec[i].Ave;
} // end of for j loop
} // end of for i loop
} // end of for i+0 loop
} // end of for k loop
return (0);
} // end of main```

2. Next time it would be great if you could throw in the errors your getting.

Is it where you declare the struct?

U have:

struct studentrec testrec[MAX];

normally it would be:

studentrec testrec[MAX];

I don't think what you have would work...
What error are you getting...is it in compile, runtime, or is the programming just not doing what you want?

3. u dont specify a return type for main()...it should be int

4. You also need to indent your code properly, to find out what the nesting of all your loops is.

Because it seems to me that you are sorting the whole array each time you add a record.

You want

- the for loop for input

- the two for loops to sort

- the for loop to print