Vcoderz Community

Vcoderz Community (http://forum.vcoderz.com/index.php)
-   Computers & Information Technologies (http://forum.vcoderz.com/forumdisplay.php?f=6)
-   -   Build a GPA Calculator Using C++ (http://forum.vcoderz.com/showthread.php?t=16936)

J()e 10-21-2009 06:00 PM

Build a GPA Calculator Using C++
 
Hello every1 well I'm taking C++ and I have an assignment for tomorow where I should build a GPA calculator using C++.

Tawa was helping me do it but it seems that we are stuck and we don't know how to continue(At least I don't:p).

“GPA Calculator”
You are asked to develop a GPA calculator with the following features:
  • The program prompts the user to enter the umber of courses taken in the current semester.
  • The user enters the number of courses
Steps 3 and 4 are repeated for each course
  • The program prompts the user to enter the predicted grade for the course
  • The user enters the predicted grade (number not letter)
  • The program calculates and displays the GPA based on the following system:

System of Grades
The University uses the following system of grades. This system consists of letter grades with their corresponding numerical ranges (i.e. percentage equivalent, and the 4.0 point maximum).
http://img36.imageshack.us/img36/3059/15178804.jpg


Grade-Point Average
The grade-point-average (GPA) or index is the ratio of the total quality point values divided by the number of the credit hours attempted by the student, as shown below.
The GPA of the five courses would then be 30.6 / 14 = 2.19. Courses with a grade of W, U, PR or I are not counted in computing the cumulative GPA.


The code that I wrote(with the help of tawa).

Code:

#include <iostream>
using namespace std;

int main(){
    int nmbr, grade, credits, GPA = 0, totalCredits = 0;

    cout << "Enter The Number of Courses: ";
    cin >> nmbr;

    for(int i = 0; i < nmbr; i++){
        cout << "Enter the credit number for course No. " << i+1 << " : ";
        cin >> credits;
        totalCredits += credits;

        cout << "Enter the grade for course No. " << i+1 << " : ";
        cin >> grade;
        GPA += grade*credits;
    }

    GPA /= totalCredits;

    cout << "The GPA Is: " << GPA << " / ";

    if(GPA < 60)
        cout << "F    / 0.0 Quality Point Value";
    else if(GPA >= 60 && GPA <= 62)
        cout << "D    / 1.0 Quality Point Value";
    else if(GPA >= 63 && GPA <= 65)
        cout << "D+ / 1.3 Quality Point Value";
    else if(GPA >= 66 && GPA <= 69)
        cout << "C- / 1.7 Quality Point Value";
    else if(GPA >= 70 && GPA <= 72)
        cout << "C  / 2.0 Quality Point Value";
    else if(GPA >= 73 && GPA <= 76)
        cout << "C+ / 2.3 Quality Point Value";
    else if(GPA >= 77 && GPA <= 79)
        cout << "B- / 2.7 Quality Point Value";
    else if(GPA >= 80 && GPA <= 84)
        cout << "B  / 3.0 Quality Point Value";
    else if(GPA >= 85 && GPA <= 88)
        cout << "B+ / 3.3 Quality Point Value";
    else if(GPA >= 89 && GPA <= 92)
        cout << "A- / 3.7 Quality Point Value";
    else if(GPA >= 93 && GPA <= 96)
        cout << "A  / 4.0 Quality Point Value";
    else if(GPA >= 97)
        cout << "A+ / 4.0 Quality Point Value";
    cout << endl;

    return 0;
}

Everything is perfect but in the if and else there is something wrong and I don't know how to fix it.

ie when I enter for example that I take 4 courses, and I have the following grades: 80,94,87 and 89. and each course has 3 credits so GPA will be 3.5 but using the program above it will give me 3.3 and I know that the problem is in the if and else statement but I don't know how to fix it again.


NOTE:I am asked to build the program using only the for loop and if and else statements.

Note 2: I have to submit the assignment before today's midnight:D so Edgard chammas chidd el hemme:p

Neoxter 10-21-2009 06:47 PM

i'm a C++ n00b but in if statement eza 7attet two equations i think u have to put like this :

else if(GPA >= 66 && GPA <= 69) becomes else if( (GPA >= 66) && (GPA <= 69) )

every operation should be in a bracket.

Tawa 10-21-2009 07:04 PM

Quote:

Originally Posted by J()e (Post 200786)
The code that I wrote(with the help of tawa).

Excuse Me? :p you wrote the code with "my help" lool I don't get it :p

Anyways, you're wrong, if you calculate the 4 numbers you gave {
80,94,87 and 89} You get a GPA of 87.5%, and if you check the table you gave:
http://img36.imageshack.us/img36/3059/15178804.jpg
85-88 ~> 3.3 Quality Point Value.

Enta mdayya3, just send him this code and you'll do fine.

Google 10-21-2009 07:20 PM

The error comes from here:
(80x3) + (94x3) + (87x3) + (89x3) = 1050
1050 / 12 = 87.5
GPA => 3.3
You are using this method to evaluate the GPA. You are getting the average grade of the courses and then passing it to the IF conditions and testing it against the "Quality point value" rule. This is a logical mistake and not a programming one.

What you should do is use IF conditions after each grade input, to convert that grade to its equivalent Quality point value.

You have another mistake. GPA variable must be declared as double.

You will have to do it like this:


Code:

#include <iostream>
using namespace std;

void main()
{
int nbrOfCourses, grade, credits, totalCredits = 0;
double GPA = 0; //There will be a division!

cout << "Enter The Number of Courses: ";
cin >> nbrOfCourses;
for(int i=0; i<nbrOfCourses; i++)
{
  cout << "Enter the grade for course "<< i+1 <<" : ";
  cin >> grade;
  cout << "Enter the number of credits for course "<< i+1 <<" : ";
  cin >> credits;
  totalCredits += credits;
  if(grade>=60 && grade<=62)
      GPA += 1*credits;
  else if(grade>=63 && grade<=65)
      GPA += 1.3*credits;
  else if ...
      ...
  //You end those stupid conditions
}
 
GPA /= totalCredits;

cout << "The GPA Is: " << GPA << " / ";

    if(GPA < 60)
        cout << "F    / 0.0 Quality Point Value";
    else if(GPA >= 60 && GPA <= 62)
        cout << "D    / 1.0 Quality Point Value";
    else if(GPA >= 63 && GPA <= 65)
        cout << "D+ / 1.3 Quality Point Value";
    else if(GPA >= 66 && GPA <= 69)
        cout << "C- / 1.7 Quality Point Value";
    else if(GPA >= 70 && GPA <= 72)
        cout << "C  / 2.0 Quality Point Value";
    else if(GPA >= 73 && GPA <= 76)
        cout << "C+ / 2.3 Quality Point Value";
    else if(GPA >= 77 && GPA <= 79)
        cout << "B- / 2.7 Quality Point Value";
    else if(GPA >= 80 && GPA <= 84)
        cout << "B  / 3.0 Quality Point Value";
    else if(GPA >= 85 && GPA <= 88)
        cout << "B+ / 3.3 Quality Point Value";
    else if(GPA >= 89 && GPA <= 92)
        cout << "A- / 3.7 Quality Point Value";
    else if(GPA >= 93 && GPA <= 96)
        cout << "A  / 4.0 Quality Point Value";
    else if(GPA >= 97)
        cout << "A+ / 4.0 Quality Point Value";
    cout << endl;
}


MORPHEUSP 10-21-2009 08:20 PM

Quote:

Originally Posted by Google (Post 200790)
The error comes from here:
(80x3) + (94x3) + (87x3) + (89x3) = 1050
1050 / 12 = 87.5
GPA => 3.3
You are using this method to evaluate the GPA. You are getting the average grade of the courses and then passing it to the IF conditions and testing it against the "Quality point value" rule. This is a logical mistake and not a programming one.

What you should do is use IF conditions after each grade input, to convert that grade to its equivalent Quality point value.

You have another mistake. GPA variable must be declared as double.

You will have to do it like this:


Code:

#include <iostream>
using namespace std;

void main()
{
int nbrOfCourses, grade, credits, totalCredits = 0;
double GPA = 0; //There will be a division!

cout << "Enter The Number of Courses: ";
cin >> nbrOfCourses;
for(int i=0; i<nbrOfCourses; i++)
{
  cout << "Enter the grade for course "<< i+1 <<" : ";
  cin >> grade;
  cout << "Enter the number of credits for course "<< i+1 <<" : ";
  cin >> credits;
  totalCredits += credits;
  if(grade>=60 && grade<=62)
      GPA += 1*credits;
  else if(grade>=63 && grade<=65)
      GPA += 1.3*credits;
  else if ...
      ...
  //You end those stupid conditions
}
 
GPA /= totalCredits;

cout << "The GPA Is: " << GPA << " / ";

    if(GPA < 60)
        cout << "F    / 0.0 Quality Point Value";
    else if(GPA >= 60 && GPA <= 62)
        cout << "D    / 1.0 Quality Point Value";
    else if(GPA >= 63 && GPA <= 65)
        cout << "D+ / 1.3 Quality Point Value";
    else if(GPA >= 66 && GPA <= 69)
        cout << "C- / 1.7 Quality Point Value";
    else if(GPA >= 70 && GPA <= 72)
        cout << "C  / 2.0 Quality Point Value";
    else if(GPA >= 73 && GPA <= 76)
        cout << "C+ / 2.3 Quality Point Value";
    else if(GPA >= 77 && GPA <= 79)
        cout << "B- / 2.7 Quality Point Value";
    else if(GPA >= 80 && GPA <= 84)
        cout << "B  / 3.0 Quality Point Value";
    else if(GPA >= 85 && GPA <= 88)
        cout << "B+ / 3.3 Quality Point Value";
    else if(GPA >= 89 && GPA <= 92)
        cout << "A- / 3.7 Quality Point Value";
    else if(GPA >= 93 && GPA <= 96)
        cout << "A  / 4.0 Quality Point Value";
    else if(GPA >= 97)
        cout << "A+ / 4.0 Quality Point Value";
    cout << endl;
}



was goind to post the saaame thang!, btw why people dont use void main :p w yertei7o men el return 0 :D ? if ya need some help later on 2ebba7bechlak bi my C++ notebook we did the same program in class but not GPA (bala ma te2sema ye3neh).

J()e 10-21-2009 08:51 PM

Thanks guyz,w tawa la twekhizna int kent ketib el program kello:D

anyway the code sar hek

Code:

#include <iostream>
using namespace std;

void main()
{
int nbrOfCourses, grade, credits, totalCredits = 0;
double GPA = 0; //There will be a division!

cout << "Enter The Number of Courses: ";
cin >> nbrOfCourses;
for(int i=0; i<nbrOfCourses; i++)
{
  cout << "Enter the grade for course "<< i+1 <<" : ";
  cin >> grade;
  cout << "Enter the number of credits for course "<< i+1 <<" : ";
  cin >> credits;
  totalCredits += credits;
  if(grade>=60 && grade<=62)
      GPA += 1*credits;
  else if(grade>=63 && grade<=65)
      GPA += 1.3*credits;
  else if(grade>=66 && grade<=69)
      GPA += 1.7*credits;
  else if(grade>=70 && grade<=70)
      GPA += 2.0*credits;
  else if(grade>=73 && grade<=76)
      GPA += 2.3*credits;
  else if(grade>=77 && grade<=79)
      GPA += 2.7*credits;
  else if(grade>=80 && grade<=84)
      GPA += 3.0*credits;
  else if(grade>=85 && grade<=88)
      GPA += 3.3*credits;
  else if(grade>=89 && grade<=92)
      GPA += 3.7*credits;
  else if(grade>=93 && grade<=96)
      GPA += 4.0*credits;
  else if(grade>=97)
      GPA += 4.0*credits;

 
}
 
GPA /= totalCredits;

cout << "The GPA Is: " << GPA << " / ";

    if(GPA < 60)
        cout << "F    / 0.0 Quality Point Value";
    else if(GPA >= 60 && GPA <= 62)
        cout << "D    / 1.0 Quality Point Value";
    else if(GPA >= 63 && GPA <= 65)
        cout << "D+ / 1.3 Quality Point Value";
    else if(GPA >= 66 && GPA <= 69)
        cout << "C- / 1.7 Quality Point Value";
    else if(GPA >= 70 && GPA <= 72)
        cout << "C  / 2.0 Quality Point Value";
    else if(GPA >= 73 && GPA <= 76)
        cout << "C+ / 2.3 Quality Point Value";
    else if(GPA >= 77 && GPA <= 79)
        cout << "B- / 2.7 Quality Point Value";
    else if(GPA >= 80 && GPA <= 84)
        cout << "B  / 3.0 Quality Point Value";
    else if(GPA >= 85 && GPA <= 88)
        cout << "B+ / 3.3 Quality Point Value";
    else if(GPA >= 89 && GPA <= 92)
        cout << "A- / 3.7 Quality Point Value";
    else if(GPA >= 93 && GPA <= 96)
        cout << "A  / 4.0 Quality Point Value";
    else if(GPA >= 97)
        cout << "A+ / 4.0 Quality Point Value";
    cout << endl;
}

bass there is something wrong bel output.

the output is like this:
http://img291.imageshack.us/img291/3342/outputc.jpg


u c bi ekhir line kello mni7 min 3ada / F / 0.0 bla bla

kif fina nzabbeton w tnx salaf:)

Kingroudy 10-21-2009 08:57 PM

LOL man, GPA is 3.5 which is wayyyyyyyyyyyyyyyy less than 60, it will keep displaying F :P
zabbeta anyway you want. i made it clear.

Google 10-21-2009 09:08 PM

Oh, ma ntabahet. After computing the GPA and outputting the value, why would you need this part?

Quote:

if(GPA < 60)
cout << "F / 0.0 Quality Point Value";
else if(GPA >= 60 && GPA <= 62)
cout << "D / 1.0 Quality Point Value";
else if(GPA >= 63 && GPA <= 65)
cout << "D+ / 1.3 Quality Point Value";
else if(GPA >= 66 && GPA <= 69)
cout << "C- / 1.7 Quality Point Value";
else if(GPA >= 70 && GPA <= 72)
cout << "C / 2.0 Quality Point Value";
else if(GPA >= 73 && GPA <= 76)
cout << "C+ / 2.3 Quality Point Value";
else if(GPA >= 77 && GPA <= 79)
cout << "B- / 2.7 Quality Point Value";
else if(GPA >= 80 && GPA <= 84)
cout << "B / 3.0 Quality Point Value";
else if(GPA >= 85 && GPA <= 88)
cout << "B+ / 3.3 Quality Point Value";
else if(GPA >= 89 && GPA <= 92)
cout << "A- / 3.7 Quality Point Value";
else if(GPA >= 93 && GPA <= 96)
cout << "A / 4.0 Quality Point Value";
else if(GPA >= 97)
cout << "A+ / 4.0 Quality Point Value";
cout << endl;
You don't need it.

J()e 10-21-2009 09:13 PM

Thanks, meche el 7al,choukran jazilan ya edgard:W

Sony 10-22-2009 05:21 PM

what you need to do is getting some quality in your code...too much if statements is NOT a good style after all...
you can use switch case way...
or you can hide the if else sh*t in a library and use a function to call it. (this function takes the result and return the equivalent value... )

i can't believe that tawa failed in this simple task ( simple compared to the snake game )


All times are GMT +1. The time now is 10:11 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger