User-Defined Functions and Boolean Variables

Hey everyone,

I am learning C++ and have been working on a program for a few days now that requires A LOT of techniques be used. So far, I have gotten my program to output what I want it to output with the exception of 2 cases.

1- a User-Defined function that calculates the per pizza charge. This function will take two input parameters (pizza size and number of toppings) and will return the cost per pizza. ** I have this built into the main function, rather than a user-defined function (or method i heard it was called), and have yet to figure out how to break them apart but call the function where I need the output to be.

2-Whether the customer wants the pizza delivered - Read from the user a single character ('d'/'D') for delivery, ('p'/'P') for pickup. After reading the inpu character the program will store a value into a Boolean variable called deliver (true for customers who want delivery, false for customers who will pick up their own pizza) an use only that Boolean in later processing, when testing the value to decide whether to charge for delivery. The user may enter the character in uppercase or lowercase, the program must use a logical operator to test for upper and lower when decided where to store true or false in the boolean value. - I have tried multiple ways to make this work, and obviously not gotten the concept of boolean values..any help would be appreciated.

**AND...is the declaration of "string studentCouncilMember;" correct if I want input to be either "yes" or "no"??

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
119
120
121
122
123
124
125

#include <iostream>
#include <iomanip>
#include <cctype>

using namespace std;

int main()
{
    // local constants
    const double MEDIUM_PIZZA_PRICE = 4.99;
    const double LARGE_PIZZA_PRICE = 7.99;
    const double TOPPING_PRICE = 0.85;
    const double STUDENT_COUNCIL_DISCOUNT = 0.90;
    const double DELIVERY_FEE = 0.12;
    
    // local variables
    char pizzaSize;
    int numPizzaOrdered;
    int numTopping;
    string studentCouncilMember;
    char deliveryInput;
    bool deliver;
    double priceOfPizza;
    double costPerPizza;
    double totalPizzaCost;
    double discount;
    double subtotal;
    double costDelivery;
    double totalBill;
    
    cout << fixed << showpoint << setprecision (2);
    
    cout << "Pizza Bill Calculation Program" << endl << endl;
    
    cout << "Pizza size medium or large (M/L)? ";
    cin >> pizzaSize;
    if (islower (pizzaSize)) pizzaSize = toupper (pizzaSize);
    
    cout << "Enter the number of pizzas ordered: ";
    cin >> numPizzaOrdered;    
    
    cout << "Enter the number of toppings per pizza: ";
    cin >> numTopping;    
    
    cout << "Student Council member? (yes or no, all lowercase): ";
    cin >> studentCouncilMember; 
       
    cout << "Delivery or pickup (D/P)? ";
    cin >> deliveryInput;
    cout << endl << endl << endl;
    
    cout << "ORDER DETAILS" << endl; 
      
    cout << "Pizza Size: ";
    if (pizzaSize == 'M') {
                  cout <<  setw (16) << "medium";
    } else if (pizzaSize == 'L') {
           cout << setw (16) << "large";
    }
    cout << endl;
    
    cout << "Number of Toppings: ";
    cout << setw (8) << numTopping << endl;
            
    if (pizzaSize == 'M') {
                  priceOfPizza = MEDIUM_PIZZA_PRICE; 
    } else if (pizzaSize == 'm') {
           priceOfPizza = MEDIUM_PIZZA_PRICE;     
    } else if (pizzaSize == 'L') {
           priceOfPizza = LARGE_PIZZA_PRICE;
    } else if (pizzaSize == 'l') {
           priceOfPizza = LARGE_PIZZA_PRICE;
    }
    
    costPerPizza = (priceOfPizza + (numTopping * TOPPING_PRICE));
    
    cout << "Cost per Pizza: ";
    cout << setw (12) << costPerPizza << endl;
    
    cout << "Number of Pizzas: ";
    cout << setw (10) << numPizzaOrdered << endl << endl;
    
    cout << "PIZZA BILL" << endl;
    
    totalPizzaCost = costPerPizza * numPizzaOrdered;
    
    cout << "Pizza Total: ";
    cout << setw (15) << totalPizzaCost << endl;
    
    if (studentCouncilMember == "yes") {
                             discount = STUDENT_COUNCIL_DISCOUNT * numPizzaOrdered;
    } else if (studentCouncilMember == "no") {
           discount = 0.00;
    }
    
    cout << "Discount: ";
    cout << setw (18) << discount << " -" << endl;
    
    cout << setw (28) << "---------" << endl;
    
    subtotal = totalPizzaCost - discount;
    
    cout << "Subtotal: ";
    cout << setw (18) << subtotal << endl;
    
    if (deliver = 'd' || 'D') {
                costDelivery = DELIVERY_FEE * subtotal;
    } else if (deliver = 'p' || 'P') {
           costDelivery = 0.00;
    }
                   
    cout << "Delivery Cost: ";
    cout << setw (13) << costDelivery << " +" << endl;
    
    cout << setw (28) << "---------" << endl;
    
    totalBill = costDelivery + subtotal;
    
    cout << "Bill Total: ";
    cout << setw (16) << totalBill << endl << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
closed account (o3hC5Di1)
Hi there,

I'll give you some info, hopefully it will be enough for you to finish it on your own, if not, don't be afraid to ask further questions.

1. Functions are blocks of code which are created mainly to avoid copying source code. Using a function works like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//function declaration, this would be outside of the main(){} function
//you can see it takes 2 arguments as stated in your assignment
//arguments are used to pass data which the function needs
double calculate_costperpizza(char pizzaSize, int numTopping)
{
    double total;
    //calculate total
    return total;  //you're returning a 'double' value, which is why we declared the function as double calculate_costperpizza
}

//calling the function in your main() function would be as easy as:
int main() {
    double totalPizza = calculate_cost_perpizza(pizzaSize, numTopping);
}

//this example is off course very rudimentary, you'll need to implement this for your particular program 


More info on functions to be found here: http://cplusplus.com/doc/tutorial/functions/

2. Boolean values can contain 2 values only: 1 or 0 (usually used as true (for 1) or false (for 0). So an example for your case would be:

1
2
3
4
5
//this is like saying "if input is d/D, assign true to deliver, if not, assign false
bool deliver = (deliveryInput = 'd' || 'D') ? true : false; 

//then  instead of if (deliver = 'd' || 'D') { costDelivery = DELIVERY_FEE * subtotal; }
if (deliver == true) {costDelivery = DELIVERY_FEE * subtotal; }



scu1casper wrote:
**AND...is the declaration of "string studentCouncilMember;" correct if I want input to be either "yes" or "no"??


I would believe so, yes.

Hope that helps.

All the best,
NwN
Last edited on
In regard to Boolean values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
       
    cout << "Delivery or pickup (D/P)? ";
    cin >> deliveryInput;
    
    deliver = (deliveryInput = 'd' || 'D') ? true : false; 

    if (deliver == true) {
                costDelivery = DELIVERY_FEE * subtotal;
    } else if (deliver == false) {
           costDelivery = 0.00;
    }           
                   
    cout << "Delivery Cost: ";
    cout << setw (13) << costDelivery << " +" << endl;


...I am still not getting it. No matter what I choose...d or p, it still adds a delivery charge. I declared bool deliver as a variable at the beginning of my code.
And in regard to the user-defined function...
I thought I had it correct, but then realized he only wanted me to include the two input parameters, but in order to do the calculations I need more variables and constants that I have declared in the main function, if i remove them from the main function and declare them in the user-defined function, then i can't use those totals in other calculations within the main function, but i declare them globally, its considered "sloppy". This is where my confusion is...i am not sure how to go about it.
closed account (o3hC5Di1)
Hi there,

Sorry, I've overlooked something with the boolean values, I copied your if-expression, which is faulty:

1
2
3
//line 5 of your snippet
deliver = (deliveryInput = 'd' || 'D') ? true : false; 
deliver = (deliveryInput == 'd' || deliveryInput == 'D') ? true : false;


The previous would always evaluate to 'true', because you are using the assignment operator (single "="), rather than the equality operator (==).

As for the function, if you need more data for the calculation in the function, just create extra arguments for them and pass them, that would be the only way I believe. For instance double calculate_costperpizza(char pizzaSize, int numTopping, double TOPPING_PRICE)

Hope that helps.

All the best,
NwN
Delivery works!!!!

Now I am working on the user-defined function. I will let you know asap how that goes :)

Do I need to include "#include<string>" in order to have "string studentCouncilMember;" ?
deliver = (deliveryInput = 'd' || 'D') ? true : false; is wrong.
Anything* that is not 0 would be compared as true

c == a or b would be interpreted as (c==a) or b .

1
2
3
4
5
deliver = //assign to deliver
  (deliveryInput = 'd'  //assign 'd' to deliveryInput. If you want to compare it should be ==
  || 'D') ?   //'D' would be evaluated as true.
    true : //so we just return the condition. This is obfuscation
    false;

* Numbers, characters, pointers.

Also, deliver == true is a conceptual error
Last edited on
closed account (o3hC5Di1)
Thanks ne555 for your further clarification.

scu1casper wrote:
Do I need to include "#include<string>" in order to have "string studentCouncilMember;" ?


Yes indeed, sir :)

NwN
Ok, everything is working! Compiles correctly and output looks right with every test case! Thank you so much for all your help. I BELIEVE I understand now why the user-defined function wasn't working, and I think I have it set up correctly now. If you wouldn't mind taking a look and at least telling me if it looks alright?

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

/**********************************************************
* perPizzaCost - Calculates and Outputs the cost per pizza
*
* parameter: pizza size and number of toppings
* return: cost per pizza
**********************************************************/    
double calculate_perPizzaCost(char pizzaSize, int numTopping, double priceOfPizza, double TOPPING_PRICE)
{
       double perPizzaCost;
       
       perPizzaCost = (priceOfPizza + (numTopping * TOPPING_PRICE));
    
       cout << "Cost per Pizza: ";
       cout << setw (12) << perPizzaCost << endl;
       
       return perPizzaCost;   
}

int main()
{
    // local constants
    const double MEDIUM_PIZZA_PRICE = 4.99;
    const double LARGE_PIZZA_PRICE = 7.99;
    const double TOPPING_PRICE = 0.85;
    const double STUDENT_COUNCIL_DISCOUNT = 0.90;
    const double DELIVERY_FEE = 0.12;
    
    // local variables
    char pizzaSize;
    int numPizzaOrdered;
    int numTopping;
    string studentCouncilMember;
    char deliveryInput;
    bool deliver;
    double priceOfPizza;
    double perPizzaCost;
    double totalPizzaCost;
    double discount;
    double subtotal;
    double costDelivery;
    double totalBill;
    
    cout << fixed << showpoint << setprecision (2);
    
    cout << "Pizza Bill Calculation Program" << endl << endl;
    
    cout << "Pizza size medium or large (M/L)? ";
    cin >> pizzaSize;
    if (islower (pizzaSize)) pizzaSize = toupper (pizzaSize);
    
    cout << "Enter the number of pizzas ordered: ";
    cin >> numPizzaOrdered;    
    
    cout << "Enter the number of toppings per pizza: ";
    cin >> numTopping;    
    
    cout << "Student Council member? (yes or no, all lowercase): ";
    cin >> studentCouncilMember; 
       
    cout << "Delivery or pickup (D/P)? ";
    cin >> deliveryInput;

    deliver = (deliveryInput == 'd' || deliveryInput == 'D') ? true : false; 
    
    cout << endl << endl << endl;
    
    cout << "ORDER DETAILS" << endl; 
      
    cout << "Pizza Size: ";
    if (pizzaSize == 'M') {
                  cout <<  setw (16) << "medium";
    } else if (pizzaSize == 'L') {
           cout << setw (16) << "large";
    }
    cout << endl;
    
    cout << "Number of Toppings: ";
    cout << setw (8) << numTopping << endl;
            
    if (pizzaSize == 'M' || pizzaSize == 'm') {
                  priceOfPizza = MEDIUM_PIZZA_PRICE;     
    } else if (pizzaSize == 'L' || pizzaSize == 'l') {
           priceOfPizza = LARGE_PIZZA_PRICE;
    }
    
    double costPerPizza = calculate_perPizzaCost(pizzaSize, numTopping, priceOfPizza, TOPPING_PRICE);  //function call
    
    cout << "Number of Pizzas: ";
    cout << setw (10) << numPizzaOrdered << endl << endl;
    
    cout << "PIZZA BILL" << endl;
    
    totalPizzaCost = costPerPizza * numPizzaOrdered;
    
    cout << "Pizza Total: ";
    cout << setw (15) << totalPizzaCost << endl;
    
    if (studentCouncilMember == "yes") {
                             discount = STUDENT_COUNCIL_DISCOUNT * numPizzaOrdered;
    } else if (studentCouncilMember == "no") {
           discount = 0.00;
    }
    
    cout << "Discount: ";
    cout << setw (18) << discount << " -" << endl;
    
    cout << setw (28) << "---------" << endl;
    
    subtotal = totalPizzaCost - discount;
    
    cout << "Subtotal: ";
    cout << setw (18) << subtotal << endl;
    
    if (deliver == true) {
                costDelivery = DELIVERY_FEE * subtotal;
    } else if (deliver == false) {
           costDelivery = 0.00;
    }           
                   
    cout << "Delivery Cost: ";
    cout << setw (13) << costDelivery << " +" << endl;
    
    cout << setw (28) << "---------" << endl;
    
    totalBill = costDelivery + subtotal;
    
    cout << "Bill Total: ";
    cout << setw (16) << totalBill << endl << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
closed account (o3hC5Di1)
Hi there,

Looks perfect to me.
Best way to test would be to run through the program, making the calculations yourself to check if everything calculates correctly.

Well done :)

All the best,
NwN
I did that with 4 different test cases, and so far everything worked correctly! Thanks again for all your help!
closed account (o3hC5Di1)
Most welcome, glad to be able to help.

All the best,
NwN
Topic archived. No new replies allowed.