Hi this is my first post. I am taking a programing class for the first time and I am having trouble with one program we are writing. In this program the user picks between 3 packages then enters hours used. The first bool statement is to check that the users input was between 1 and 3. The second is to check that the hours are between 0.00 and 744.00. My first data check comes out okay but when trying to check that the hours entered are with in the correct range it is not working. Is it because in the first boolean test it has already come up with a true response? anyways here is my code.
for hours when I input a value outside of the parameters it does all the calculations. what I need it to do it if I input anything below 0 or above 744 in the hours it will kick out a message that says "The hours used must be between 0.00 and 744.00" but right now even if I put a number in there it does the calculations.
The first bool statement is to check that the users input was between 1 and 3. The second is to check that the hours are between 0.00 and 744.00.
No, the conditions of these if statement's do not do as you had described in the quote above. The first if statement's condition will evaluate to true if the variable 'package' is greater than three or less than/equal to zero. The second if statement's condition will evaluate to true if the variable 'hours' is less than zero or greater than seven hundred forty four. This is certainly not what you had intended to do.
Does the code above function properly? I updated the code with what I had explained in my previous post and changed the value 'hoursAreOK' is initialized to in the first if-else chain.
thank you both very much this is my first go around at if then else and switch statements I have a print out of my original code so I can compare and actually learn this
I have, yet again, altered the source code. I noticed a logical error with the if statement nested within the second if statement. You have placed a semicolon to the right of the parenthesis. Does it function properly after removing this semicolon?
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
float amount; //note its amount not amout
int package, hours;
cout<<"Select a subscription package:"<< endl;
cout<<"1. Package A"<< endl;
cout<<"2. Package B"<< endl;
cout<<"3. Package C"<< endl;
cout<<"4. Quit"<< endl << endl;
cin>> package;
if(package <= 0 || package > 3)
{
cout<<"How many hours were used ? ";
cin>> hours;
}
else
cout <<"The valid choices are 1 through 4. Run the"<<endl<<"program again and select one of those."<<endl;
if(hours < 0 || hours > 744)
{
switch(package)
{
case 1:
if(hours <= 10)
amount = 9.95;
else
amout = (hours - 10) * 2.00 + 9.95;
break;
case 2:
if(hours <= 20)
amout = 14.95;
else
amout = (hours - 20) * 1.00 + 14.95;
break;
case 3:
amout=19.95;
break;
}
cout<<"The amount is $"<< amount << endl; //if you have it here then you
//then you only need to type it once
//for the same effect
}
else
cout<<"The hours used must be between 0.00 and 744.00"<< endl;
return 0;
}
edit:
i did format it to my style of coding and removed the unneccesary bools
You have made the same logical error that the creator of this thread has made within his source code (assuming you rewrote everything). Again, the condition of the if statements are erroneous and will not function as intended. Your source code also references an undefined variable with the identifier 'amout', which I believe was intentional.