Reputation: 67
I'm a student and started learning C++ recently.I was making a GPA calculator for my juniors in C++ but I'm facing an error. When I multiply a decimal with a variable, I get the same answer every time. This happens with the decimals only. Exact integers work fine. I get the right answer if I place the value of variable "cal" in the range of and integer.Like if I place 85, I get 12 as output but when I type the value below 85, I get 3.1 every time.
Can you point out my mistake?
Here is my starting source code :
#include <iostream>
#include <string>
using namespace std;
int main()
{double cal,calh,c1;
cout<< "How many marks you obtained in calculus? \n";
cin >>cal;
cout<<"Enter the credit hours of Calculus? \n";
cin>>calh;
if (cal>85 || cal==85){c1=4*calh;}
else if (cal>80 || cal==80 || cal<85){c1=3.7*calh;}
else if (cal>75 || cal==75 || cal<80){c1=3.3*calh;}
else if (cal>70 || cal==70 || cal<75){c1=3*calh;}
else if (cal>65 || cal==65 || cal<70){c1=2.7*calh;}
else if (cal>61 || cal==61 || cal<65){c1=2.3*calh;}
else if (cal>58 || cal==58 || cal<61){c1=2*calh;}
else if (cal>55 || cal==55 || cal<58){c1=1.7*calh;}
else if (cal>50 || cal==50 || cal<55){c1=1*calh;}
else if (cal<50 || cal==49){cout<<"Sorry but you are failed in calculus.";}
cout<<"your total gpa in calculus is "<< c1<<endl;
system("pause");
return 0;
}
Upvotes: 0
Views: 235
Reputation: 2612
Let's look at your first two conditional lines:
if (cal>85 || cal==85){c1=4*calh;}
else if (cal>80 || cal==80 || cal<85){c1=3.7*calh;}
In the second one, you are entering in the condition for every possible value, as you are asking "any value that are greater than 80, but also any value that are lower than 85". So you will always have c1 = 3.7*calh, except if cal is superior to 85. That's why you are getting the same value everytime.
I think the problem is that you misunderstood the logic of the || operator. When you have several conditions in a if statement separated with a ||, it means that only one of the conditions needs to be true to enter the condition. If you want to keep this logic, remove the "==" and ">" tests and replace them with a ">=" one, and replace your || operator with &&.
Now let's look at your code again. There is some conditions you are repeating there. You already that you are entering the first else if cal is lesser than 85, so you don't need to verify it again. Then, in the end, you can replace
else if (cal>80 || cal==80 || cal<85){c1=3.7*calh;}
With:
else if(cal >=80) {c1=3.7*calh;}
Apply this trick everywhere, and you are done.
Upvotes: 1
Reputation: 85316
Think about what you wrote
else if (cal>80 || cal==80 || cal<85){c1=3.7*calh;}
This will match everything (any number is either larger than 80 or less than 85)
You want something like this instead
#include <iostream>
#include <string>
using namespace std;
int main()
{
double cal, calh, c1;
cout << "How many marks you obtained in calculus? \n";
cin >> cal;
cout << "Enter the credit hours of Calculus? \n";
cin >> calh;
if (cal >= 85) { c1 = 4 * calh; }
else if (cal >= 80) { c1 = 3.7*calh; }
else if (cal >= 75) { c1 = 3.3*calh; }
else if (cal >= 70) { c1 = 3 * calh; }
else if (cal >= 65) { c1 = 2.7*calh; }
else if (cal >= 61) { c1 = 2.3*calh; }
else if (cal >= 58) { c1 = 2 * calh; }
else if (cal >= 55) { c1 = 1.7*calh; }
else if (cal >= 50) { c1 = 1 * calh; }
else { cout << "Sorry but you are failed in calculus."; c1 = 0; }
cout << "your total gpa in calculus is " << c1 << endl;
system("pause");
return 0;
}
Upvotes: 1