First thing is to follow the excellent advice of
apw5020.
Go through your code line by line.
Remove commented out code.
Put the braces "{" "}" where they should be according to one of the two main styles. Choose one of them and apply it consistently.
Indent your code as per the bracing style you have chosen.
When your code is tidy you will probably see your mistakes straight away. If you can't, see them come back to us but you do need to do your own housekeeping before sharing your code with strangers and asking them to try and read it.
Here is your code in something approaching a readable form:
cpp
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cmath>
using namespace std;
int main() {
int grade[4];
int temp;
bool more_students;
char choice, name, lgrade;
int totstudents;
// Check to see if there are any students.
cout << " Are there any students? <Y> or <N> \n";
cin >> choice;
more_students = (choice == 'Y' || choice == 'y');
cout << " Enter Last and First name : ";
cin >> name;
// Enter and Calculate grade.
for( int g=0; g<4; g++){
cout << " Enter grade: " << g + 1 << endl;
cin >> grade[g];
}
// Find the average.
int sum = 0;
double average;
for( int k=0; k<4; k++) {
sum += grade[k];
}
average = sum / 4.0;
for( int i=1; i<4; i++) {
for( int m=0; m<4-i; m++) {
if( grade[m] > grade[m+1] ) {
temp = grade[m];
grade[m] = grade[m+1];
grade[m+1] = temp;
}
}
if(( average <= 100) && ( average >= 0)) {
if(average >= 90) {
lgrade = 'A';
} else if(average >= 80) {
lgrade = 'B';
} else if(average >= 70) {
lgrade = 'C';
} else if(average >= 65) {
lgrade = 'D';
} else {
lgrade = 'F';
}
return lgrade;
}
cout << " Are there any more students? <Y> or <N> \n";
cin >> choice;
more_students = (choice == 'Y' || choice == 'y');
more_students = more_students + 1;
totstudents += more_students;
for( int h=0; h<4; h++) {
cout << name << setw(5) << grade[h] << setw(5)
<< average << setw(5) << lgrade;
}
cout << " There were " << totstudents << " students processed \n";
system("pause");
return 0;
}
Now that we can see what is in there a couple of things jump out.
What is this intended to do?
cpp
return lgrade;
And this?
cpp
more_students = (choice == 'Y' || choice == 'y');
more_students = more_students + 1;
This post has been edited by janotte: 7 Jun, 2009 - 05:30 PM