boolean test

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>

using namespace std;

int main()
{
   float amout;
   int package, hours;
   bool dataAreOK;
   bool hoursAreOK;

   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)
        dataAreOK= false;
    else
        dataAreOK = true;
   if (dataAreOK)
   {
       cout << "How many hours were used ?";
       cin >> hours;

       if (hours < 0 || hours > 744)
        hoursAreOK = false;
       else
        hoursAreOK = true;

       if (hoursAreOK);
       {
           switch (package)
           {


           case 1 :
               if (hours <= 10)
                amout=9.95;
               else
                amout=(hours-10)*2.00+9.95;
               cout<< "The total amout due is $"<<amout<<endl;
               break;

           case 2 :
                if (hours <= 20)
                    amout=14.95;
                else
                    amout= (hours-20)*1.00+14.95;
                cout<< "The total amout due is $"<<amout<<endl;
                break;
           case 3 :
                amout=19.95;
                cout << "The total amout due is $"<<amout<<endl;
                break;

           }

   }
}
    else
        cout <<"The valid choices are 1 through 4. Run the"<<endl<<"program again and select one of those."<<endl;
return 0;
}


Last edited on
closed account (Dy7SLyTq)
well what are you inputting?
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.
closed account (Dy7SLyTq)
oh i see whats wrong. so right now you have it as
if(dataAreOk)
{
//...
}

else
cout<<" the valid choices are..."

you want it as
if(dataAreOk)
{
//...
if(hoursAreOk)
//...
else
cout<<"The valid..."
}
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>

using namespace std;

int main()
{
   float amout;
   int package, hours;
   bool dataAreOK;
   bool hoursAreOK;

   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)
        dataAreOK= true;
    else
        dataAreOK = false;
   if (dataAreOK)
   {
       cout << "How many hours were used ?";
       cin >> hours;

       if (hours > 0 && hours <= 744)
        hoursAreOK = true;
       else
        hoursAreOK = false;

       if (hoursAreOK)
       {
           switch (package)
           {


           case 1 :
               if (hours <= 10)
                amout=9.95;
               else
                amout=(hours-10)*2.00+9.95;
               cout<< "The total amout due is $"<<amout<<endl;
               break;

           case 2 :
                if (hours <= 20)
                    amout=14.95;
                else
                    amout= (hours-20)*1.00+14.95;
                cout<< "The total amout due is $"<<amout<<endl;
                break;
           case 3 :
                amout=19.95;
                cout << "The total amout due is $"<<amout<<endl;
                break;

           }

   }
}
    else
        cout <<"The valid choices are 1 through 4. Run the"<<endl<<"program again and select one of those."<<endl;
return 0;
}
Last edited on
okay so I am still confused on how to fix this do I need to reverse the statement?
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.
Last edited on
it still does the calculations if i put in a value that is to high like 5000 for hours
closed account (Dy7SLyTq)
no vivre it wont and i dont even have to test it. its because right now its

if(dataAreOk)
{
//...
}

else
//print the error message

he needs to put it in with the nested if. im doing it write now but i want to clean up the code first

edit:
@vivre: im sorry. i thought he was saying the error message for the packages didnt work
Last edited on
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>

using namespace std;

int main()
{
   float amout;
   int package, hours;
   bool dataAreOK;
   bool hoursAreOK;

   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)
        dataAreOK= true;
    else
        dataAreOK = false;
   if (dataAreOK)
   {
       cout << "How many hours were used ?";
       cin >> hours;

       if (hours > 0 && hours <= 744)
        hoursAreOK = true;
       else
        hoursAreOK = false;

       if (hoursAreOK)
       {
           switch (package)
           {


           case 1 :
               if (hours <= 10)
                amout=9.95;
               else
                amout=(hours-10)*2.00+9.95;
               cout<< "The total amout due is $"<<amout<<endl;
               break;

           case 2 :
                if (hours <= 20)
                    amout=14.95;
                else
                    amout= (hours-20)*1.00+14.95;
                cout<< "The total amout due is $"<<amout<<endl;
                break;
           case 3 :
                amout=19.95;
                cout << "The total amout due is $"<<amout<<endl;
                break;

           }

   }
}
    else
        cout <<"The valid choices are 1 through 4. Run the"<<endl<<"program again and select one of those."<<endl;
return 0;
}
Last edited on
closed account (Dy7SLyTq)
ok so now i realize why your hour error message isnt working. you dont have one
closed account (Dy7SLyTq)
try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>

using namespace 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
Last edited on
DTSCode,

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.
I have to use bools for this project it was all apart of the lesson
Thank you both I think this will give me enough info to finish this program I have to go to class but will update later if I need anymore help.
Topic archived. No new replies allowed.