Please Help me with this program

This is a discussion on Please Help me with this program within the C++ Programming forums, part of the General Programming Boards category; This is a pretty simple program, but I am still having problems, and my CompSci teacher has been out for ...

  1. #1
    IRLeeb
    Join Date
    Feb 2005
    Location
    Amston, CT
    Posts
    7

    Please Help me with this program

    This is a pretty simple program, but I am still having problems, and my CompSci teacher has been out for a couple of weeks.

    Code:
    /*      Lenavich III
    
      Period 6
      
    	This program computes the average, G.P.A., and letter grades of 20
    	different students, as well as outputting that in a table when given
    	the names of the 20 different students and a number of their grades.
    */
    
    #include <iostream.h>
    #include <iomanip.h>
    #include <windows.h>
    #include <math.h>
    
    int main() {
    	char names[4][10]; // array of students names
    	char lgrade[4][3]; // letter grade variable
    	double average[4]; // average of grades variable
    	double gpa[4]; // G.P.A variable
    	int h, i; // counter
    	int grades; // number of grades variable
    	double total;
    	double grade;//
    	int honors;
    	
    	// querying user for number of grades
    	
    	cout << "How many grades would you like to enter per a student?" << endl;
    	cin >> grades;
    	
    	// querying user for names of students
    	
    	for(i = 0; i <= 4; i++) {
    		cout << "Enter a student's name whose grade you wish to compute." << endl;
    		cin >> names[i];
    		cout << endl;
    	}
    	
    	// querying user for grades
    	
    	for( i = 0; i <= 4; i++) {
    		for ( h = 1; h <= grades; h++) {
    			cout << "Please enter one of " << names[i] << "'s grades" << endl;
    			cin >> grade;
    			total = total + grade;
    		}
    	}
    	
    	average[i] = total / grades;
    	
    	for( i = 0; i <= 4; i++) {
    		if(average[i] < 60)     {
    			lgrade[i][0] = 'F';
    			lgrade[i][1] = ' ';
    		}
    		else if(average[i] < 63)        {
    			lgrade[i][0] = 'D';
    			lgrade[i][1] = '-';
    		}
    		else if(average[i] < 67)        {
    			lgrade[i][0] = 'D';
    			lgrade[i][1] = ' ';
    		}
    		else if(average[i] < 70)        {
    			lgrade[i][0] = 'D';
    			lgrade[i][1] = '+';
    		}
    		else if(average[i] < 73)        {
    			lgrade[i][0] = 'C';
    			lgrade[i][1] = '-';
    		}
    		else if(average[i] < 77)        {
    			lgrade[i][0] = 'C';
    			lgrade[i][1] = ' ';
    		}
    		else if(average[i] < 80)        {
    			lgrade[i][0] = 'C';
    			lgrade[i][1] = '+';
    		}
    		else if(average[i] < 83)        {
    			lgrade[i][0] = 'B';
    			lgrade[i][1] = '-';
    		}
    		else if(average[i] < 87)        {
    			lgrade[i][0] = 'B';
    			lgrade[i][1] = ' ';
    		}
    		
    		else if(average[i] < 90)        {
    			lgrade[i][0] = 'B';
    			lgrade[i][1] = '+';
    		}
    		else if(average[i] < 93)        {
    			lgrade[i][0] = 'A';
    			lgrade[i][1] = '-';
    		}
    		else if(average[i] < 97)        {
    			lgrade[i][0] = 'A';
    			lgrade[i][1] = ' ';
    		}
    		else {
    			lgrade[i][0] = 'A';
    			lgrade[i][1] = '+';
    		}
    	}
    	
    	for( i = 0; i <= 4; i++)        {
    		if (average[i] >= 97 )  {
    			gpa[i] = 4.3;
    		}
    		else if(average[i] >= 93)       {
    			gpa[i] = 4.0;
    		}else if(average[i] >= 90)      {
    			gpa[i] = 3.7;
    		}
    		else if(average[i] >= 87)       {
    			gpa[i] = 3.3;
    		}
    		else if(average[i] >= 83)       {
    			gpa[i] = 3.0;
    		}
    		else if(average[i] >= 80)       {
    			gpa[i] = 2.7;
    		}
    		else if(average[i] >= 77)       {
    			gpa[i] = 2.3;
    		}
    		else if(average[i] >= 73)       {
    			gpa[i] = 2.0;
    		}
    		else if(average[i] >= 70)       {
    			gpa[i] = 1.7;
    		}
    		else if(average[i] >= 67)       {
    			gpa[i] = 1.3;
    		}
    		else if(average[i] >= 63)       {
    			gpa[i] = 1.0;
    		}
    		else if(average[i] >= 60)       {
    			gpa[i] = .7;
    		}
    		else{
    			gpa[i] = 0.0;
    		}
    		if(gpa[i] >= 4.0) {
    			honors = 1;
    		}
    	}
    	
    	// table
    	
    	cout << endl << "Name" << setiosflags(ios::fixed||ios::showpoint) <<
    		setprecision(5) << "GPA" << setiosflags(ios::fixed||ios::showpoint) <<
    		setprecision(5) << "Letter Grade" << endl;
    	
    	for(i = 0; i <= 4; i++) {
    		cout << endl;
    		if(honors = 1)  {
    			cout << names[i] << gpa[i]<< lgrade[i] << "High Honors" << endl;
    		}
    		else {
    			cout << names[i] << gpa[i] << lgrade[i] << endl;
    		}
    	}
    	
    	return 0;
    }
    Last edited by SWAT_LtLen; 05-13-2005 at 11:47 AM.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    You aren't calulating your averages correctly. should be something like

    Code:
    	for( i = 0; i < 4; i++) {
    		for ( h = 0; h < grades; h++) {
    			cout << "Please enter one of " << names[i] << "'s grades" << endl;
    			cin >> grade;
    			total = total + grade;
    		}
                    average[i] = total / grades;
                    total = 0;
    	}

    should be comparison.. not assignment
    Code:
    if(honors == 1)
    you should add a null character to the end of each lgrade
    Code:
    lgrade[i][0] = 'A';
    lgrade[i][1] = '+';
    lgrade[i][2] = '\0';

    and the biggest problem is you are going past all of your array boundaries. If your array is size 4, your loop should be
    Code:
    for(i=0; i < 4; i++)  // NOT <=
    Last edited by spydoor; 05-13-2005 at 12:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21