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"??
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 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.
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)
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;
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?
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.