Can't seem to get this code to work.

Hello Everybody

I been learning by myself C++ programming for a month now and I'm now taking classes to learn more about it and how to use it. My teacher gave us our 3rd C++ assignment. She ask us to take 10 students grades and enter them. The program is then, suppose to tell what their grade is. After that, we're suppose to put in the grade the program gave us and the program converts it into a letter grade (like A+,A, B+,B, C+,C, etc). Finally, it suppose to show the student's average and the course average.

I posted this on Sunday and got a lot of help (Thanks guys!!!!) and I modify the code after that to match the project but there's still something werid with it.

I've been trying to type a code that's does this, and I was able to run it but not the way it suppose to be. Here my code:

#include <iostream.h>
#include <iomanip>

using namespace std;

int main()

{
int grade = 0.0;
short studentID = 10;
const int numTests = 1;
char courseId = ' ';
double classAverage = 0;
int numStuds;
double courseSumScores = 0.0;


//Enter Course Name

cout << "Enter Course ID Number (1->Business Law): ";
cin >> courseId;

if (courseId == '1')
cout << "Course Type Accepted";

else //default
cout << "Invalid Course Type";

//end if

cout << endl;

//Enter Student ID

cout << "Number of students ID#---> " << flush;
cin >> studentID;
studentID = toupper(studentID);

switch (studentID)
{
case 1234:
cout << "Student Grade is: 87";
break;

case 2345:
cout << "Student Grade is: 76";
break;

case 3456:
cout << "Student Grade is: 45";
break;

case 4567:
cout << "Student Grade is: 89";
break;

case 5678:
cout << "Student Grade is: 103";
break;

case 6789:
cout << "Student Grade is: 83";
break;

case 7890:
cout << "Student Grade is: 72";
break;

case 8901:
cout << "Student Grade is: 57";
break;

case 9012:
cout << "Student Grade is: 95";
break;

case 0123:
cout << "Student Grade is: 98";
break;

default:
cout << "Invalid Student ID";
} //end switch

cout << setprecision(1) << fixed;

cout << endl;
system("pause");

// Enter the Number Grade.
cout << "Enter Number Grade: ";
cin >> grade;
grade = toupper(grade);

if (grade >= 89.5)
cout << "Student's Letter Grade is: A";
else if (grade >= 84.5)
cout << "Student's Letter Grade is: B+";
else if (grade >= 79.5)
cout << "Student's Letter Grade is: B";
else if (grade >= 74.5)
cout << "Student's Letter Grade is: C+";
else if (grade >= 69.5)
cout << "Student's Letter Grade is: C";
else if (grade >= 64.5)
cout << "Student's Letter Grade is: D+";
else if (grade >= 59.5)
cout << "Student's Letter Grade is: D";
else if (grade <= 59.4)
cout << "Student's Letter Grade is: F";
else //default
cout << "Invalid Grade";
//end if

cout << endl;

for( int i = 0; i < numStuds; ++i )

{

double sumGrds = 0.0;

cout << "For student " << i+1 << " ...\n";

for( int j = 0; j < numTests; ++j )

{

cout << "Test " << j+1 << ": " << flush;

int grd;

cin >> grd;

sumGrds += grd;

} // end of inner loop

cout << "Student " << i+1 << " average "

<< sumGrds/numTests << endl;



courseSumScores += sumGrds;

} // end of outer loop



cout << "\nCourse average---> "

<< courseSumScores /(numTests*numStuds)



<< "\n\nPress 'Enter' to continue ... "

<< flush; // flush/print all cout stream now ...

system("pause");
return 0;

}

I know I did something worng. Please Help!!!
think about your logic in this one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

// we have ten students
int Index = 0
for (Index =0; Index != 10; ++Index)
{
       // read in student info...
       std::cin >> (int)StudentID >> (float)studentGrade;
       // are we in a file or user input for all the info...
       // we should have an Id and Grade for that Id that is read in from user or file.
       // process information.
       // if grade is between xx and yy it has grade Some grade.  it looks like you have this if ladder as it should be
       std::cout << "Student Id#" << studentID << " has a grade of " << (float)StudentGrade << "//" << (string)LetterGrade << std::endl;
       // The average accumlator
       TotalGrade+= CurrentGrade;
} // end of for loop

// dump the average...
float Average = TotalGrade / 10;
std::cout << "Average Grade :" << Average << "//";
// process the letter grade for the average

std::cout << (string)AverageLetterGrade << std::endl;


this is basically what you are trying to do without all the code only small hints. We don't do homework on this site. Most of who can program could do these in our sleep and it your learning process.

Keep that in mind when posting homework.
Last edited on
Various other things you should seriously re-think.

1
2
3
4
5
6
7
short studentID = 10;  //Defined as short & why assign un-used 10?
...
studentID = toupper(studentID); 
switch (studentID)
{

}


Don't know if you've noticed, but numbers don't have an uppercase.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "Student's Letter Grade is: ";
if (grade >= 89.5)
 cout << "A";
 else if (grade >= 84.5)
 cout << "B+";
 else if (grade >= 79.5)
 cout << "B";
 else if (grade >= 74.5)
 cout << "C+";
 else if (grade >= 69.5)
 cout << "C";
 else if (grade >= 64.5)
 cout << "D+";
 else if (grade >= 59.5)
 cout << "D";
 else if (grade <= 59.4)
 cout << "F";
 else //default
 cout << "Invalid Grade";


The bottom line will never happen, actually none of them would work accurately, grade is an integer, meaning it cannot hold a fractional value.

There's no point in writing a bunch of code and hoping It'll work. Even if your program appears to run, it is bug ridden by flawed logic and structure.
Last edited on
Topic archived. No new replies allowed.