GPA program

Write a program in which you will ask the user to enter his/her GPA and print the grade according to following table.

Note:
The type of variable used for GPA will be float.

Compile and execute the program, and answer the following questions
Which grade is printed when the user entered 3 as his/her GPA
Which grade is printed when user entered 3.67 as his/her GPA
The grade printed in second question is right or wrong? If wrong grade is printed then what is the possible reason behind this output and suggest a feasible solution

Can someone please help me with this program??
i have to submit it tomorrow.
Last edited on
http://www.cplusplus.com/forum/beginner/1/
print the grade according to following table.

Please post the table.

Try to work out the code for yourself first. When you get stuck, post the code that you have, along with the input and expected output.
#include<iostream>
using namespace std;
int main()
{
float gpa=0;
cout<<"Enter the GPA:";
cin>>gpa;
if (gpa==4)
cout<<"Grade=A";
else if(gpa==3.67)
cout<<"Grade=A-";
else if(gpa==3.33)
cout<<"Grade=B+"<<endl;
else if(gpa==3)
cout<<"Grade=B"<<endl;
else if(gpa==2.67)
cout<<"Grade=B-"<<endl;
else if(gpa==2.33)
cout<<"Grade=C+"<<endl;
else if(gpa==2)
cout<<"Grade=C"<<endl;
else if(gpa==1.67)
cout<<"Grade=C-"<<endl;
else if(gpa==1.33)
cout<<"Grade=D+"<<endl;
else if(gpa==1)
cout<<"Grade=D"<<endl;
else if(gpa==0)
cout<<"Grade=F"<<endl;
else
cout<<"Error"<<endl;
system("pause");
return 0;
}
This is the code i have written but its giving error as an output whenever i enter a decimal number like 3.33 or 3.67 as my gpa
Why is it giving an error i need an answer to this as a reasoning and a correct code
Please use code tags. Highlight your code and click the <> button to the right of the edit window. This adds line numbers and syntax highlighting.

Your code says that an A- is exactly 3.67. What if someone's GPA is 3.68, or 3.8, or 3.7344594024? All of these should be A- also. So you need to change else if (gpa == 3.67) to else if (gpa >= 3.67)

Make similar changes to the rest of the conditions.
Using the comparison operator== or operator1!- to compare floating point values is not advised. Floating point types are approximations and can often have small "errors" that will prevent them from evaluating to exactly any value. You should use one of the other comparison operators such as greater than, less than, greater than or equal to, less than or equal to.

Several of your constants are values that can't be accurately represented by a floating point number.
Topic archived. No new replies allowed.