Boolean basics

I cannot get the boolean <hasPremium> to display. I want it to display 9.99 if <H> is input, otherwise it should not output anything.







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
double id;
char serlv;
int nmbxs;
bool hasPremium = true;
char H;
char N;
char B;
char P;
double boxcharge;
string emp;
char Preferred;
char Basic;
char service;
char error;
int numofboxes;
int servicetype;
int premium;
double rate;





1
2
cout << "Premium channels (H/N)?";
    cin >> hasPremium;





1
2
3
4
if (hasPremium = (3 < 4))
        cout << "Premium channels: " << setw(11) << "9.99" << endl;
    else
        cout << endl;
You are using = in your condition, which is assignment. Use == for equality. Alternatively, you can just test the boolean value directly, which I recommend.
1
2
3
4
if(hasPremium)
    /* output */
else
    /* otherwise */
I need to store a value into a Boolean variable <hasPremium>. Which is later used in an if-else statement. <H> for true, <N> for false. When I run the program, I get the same output for both <H> and<N>. I have tried every configuration of if-else I know of. My understanding of using boolean variables is little as I am very newb. I tried your suggestion but it did not work (assuming I understood your advice). Inputting an <H> should put 9.99 into the output statement, inputting <N> should leave no output (blank line) Here is my program in its entirety:


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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cctype>

using namespace std;

double id;
char serlv;
int nmbxs;
bool hasPremium = true;
char H;
char N;
char B;
char P;
double boxcharge;
string emp;
char Preferred;
char Basic;
char service;
char error;
int numofboxes;
int servicetype;
int premium;
double rate;

double boxfee(double numofboxes, double servicetype)
  
{
       
          if (numofboxes > 2 && serlv == 'B')
              boxcharge = numofboxes * 7.50;
          else if (numofboxes > 2 && serlv == 'P')   
                   boxcharge = numofboxes * 11.25;
          else 
              cout << "error" << endl;
              
     cout << "Cable box fees:" << boxcharge << endl;         
    
   }    
int main()   
{
    cout << "Cabletastic bill calculation program" << endl;
    cout << endl;
    cout << "Enter the customer ID:";
    cin >> id;
    cout << endl << endl;
    
    cout << "Service level ([B]asic, [P]referred)";
    cin >> serlv;
    cout << endl << endl;
    
    cout << "Enter the number of cable boxes:";
    cin >> nmbxs;
    cout << endl;
    
    cout << "H = HBO premium channels" << endl;
    cout << "N = No premium channels" << endl;
    cout << "Premium channels (H/N)?";
    cin >> hasPremium;
    hasPremium = toupper(hasPremium);
    cout << endl << endl;
    
    cout << "CableTastic employee? (yes or no, all lowercase):";
    cin >> emp;
    cout << endl << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    
   
    
    cout << "CABLETASTIC MONTLHY CABLE BILL" << endl << endl;
    cout << "Customer Id:" << id << endl << endl; 
    
    
    
    cout << "Service:?";
   {
        if (serlv == 'B')
            cout << "Basic Digital" << endl << endl;
        else if (serlv == 'P')
            cout << "Preferred Digital" << endl << endl;
        else 
            cout << "error" << endl << endl;
   }
            
            
    cout << "Cable Boxes:" << nmbxs << endl << endl;
    cout << "Monthly rate:";
     {
        if (serlv == 'B')
            cout << "43.00" << endl;
        else if (serlv == 'P')
            cout << "58.00" << endl;
        else 
            cout << "error" << endl << endl;
     }     
         
   
    boxfee(nmbxs, serlv);
   
     {
    
       
    
    if (hasPremium)
        cout << "Premium channels: " << setw(11) << "9.99" << endl;
    
     }
   
   system("PAUSE");
   
} 
     



Booleans don't take H or N, or Z, or 6, or -3.14. Booleans take 0 or not 0. As far as booleans are concerned, H = N = Z = 6 = -3.14 = not 0.
Thank you for replying, I understand the concept of boolean. My problem is that I cannot translate the programs' needs into the proper syntax. I have tried everything my rookie brain knows and still, I get the same output no matter what. It is outputting the <if> statement but never the <else>, or vice-versa. I am getting the impression that there is no connection between my input (<hasPremium>), and the if-else statement. My mistakes are usually with data types, or actual/formal parameters, but I can't get past this one.
1
2
3
    cout << "Premium channels (H/N)?";
    cin >> hasPremium;
    hasPremium = toupper(hasPremium);


hasPremium is a bool, what is it that you think this piece of code is doing?

hasPremium should start as false, if they purchase a premium channel, you should then flag them as having premium channels by setting hasPremium to true.

1
2
3
4
5
6
bool hasPremium = false;
string input = "";
cout << "Purchase premium channels? (Y/N)"
cin >> input;
if(input == "Y")
   hasPremium = true;



Now line your line 110 would make sense, and work correctly.
If you plan to have more than one category of premium channels, you could also use a char instead of a bool.

-Albatross
Thank you, it all makes sense after seeing your syntax. My problem was misinterpreting the instructions for the program. The instructions called for hasPremium to be true which I misinterpreted as being true right from the start. It never occured to me to start it as false.
@Albatross
It had to be a bool as per the instructions, which explains why he only wanted a one way selection. I was trying to use 2 and 3 way selections with a bool, obviously the wrong decision, which was probably the whole point of the lesson. Thanks
Last edited on
Everything is clear to me except <string input = "";> , does this assign the string data type to <input>? And if so why?
Topic archived. No new replies allowed.