Hello this is Robert and in this video I would like to demonstrate a program that does a simple calculation. The calculation that I’ve chosen is a tip calculator. So it’s just going to calculate the tip amount for a bill at a restaurant, for example. So i have two doubles set up, one for the total, the total bill, and then one for the tip rate. And then i have a prompt here – “what is the total bill?” So i can use a cin statement right here to ask the user to tell me how much the total bill was. So cin total. All right. Then I need to know the tip rate, so I need another prompt to let the user know what I want them to do, cout “What rate do you want to tip?” Okay and then I need another cin statement after that, so cin on tipRate. So now the user can tell me how much the total bill is and how much they would like to tip. Well, in order to do the calculation, we have two choices. We could multiply the total times the tip rate to get the the tip amount, and then we could put that expression in a cout statement. I’ll just do a simple example of what that might look like. I could do cout total times (which is the asterisk) tipRate like that. Okay so it’s just a simple example. We could put the expression right there. But let’s say that we would like to have the tip amount for a later calculation. So what we can do is, whenever we need to save some information, the light bulb should go off in your head that you need a brand new variable, right? So if we would like to save this value somewhere, the first thing we should think is I need a variable for that, and then the second thing that I should think is what kind of variable? Well the tip amount, it’s going to be dollars and cents, right? Which implies a decimal point, so we should be thinking another double. Okay so let’s go ahead and create tipAmount right here, and now we have a list of three doubles: a total, a tipRate, and a tipAmount. So instead of cout right here, instead of sending that value to the screen, all I’m going to do is say tip amount equals, or is assigned, the value of the total times the tipRate. Okay then all we need to do is send that information back to the screen to let the user know how much the tip will cost. Okay so let’s just go ahead and output the tipAmount, and then the end line. And let’s give them just a little bit more information. Right, so let’s put a literal string right here, which means i need to start it with some double quotes, and then i’ll say “the tip mount is “, and then i would like this to appear as dollars and cents, right? So… well maybe not cents so much, but as a… dollar value, so what I’m going to do is I’m going to put a dollar sign right there inside of the double quotes, and so now we will get a literal dollar sign right there. Okay, I’ve written some code, and that means that I should at least test my code before I write anymore. Because the more i write, the more chance there is that I’ve created a mistake. I don’t see any red squiggly underlines, but that doesn’t mean there are no errors in my code. There might be a logic error in my code. So it might run, but it might not run correctly. So let’s test the code by running it. So I’m just going to click the local windows debugger, and remember that that will build our program and launch it. Okay, so I do, yes I do want to build it. And then the console window will pop up, or command line, console window – command line. So it’s asking me what is the total bill. Well I want to do a simple calculation here because I want to test my code. If I was doing a more difficult calculation, i would have to get out the calculator to find out if my program is running correctly, so I’ll just give it something simple at first. So let’s say the bill is twenty dollars. And I could say twenty just by itself. I’m doing input on a double. Notice that cin>>total right there is a double, so I can either type 20 or i can type 20.00. It doesn’t matter. So that’s twenty dollars.And then let’s say, just to make it easy, I want to tip ten percent. So i’m going to say point, or i could say 0.1, or I could just say zero point one, or i could say .10 for ten percent. Alright, so now i know that ten percent of 20 is two dollars, right? So when i type return, I get the tip amount is two dollars. So that’s a good test of my program. Well let’s add a little bit more to this program. We usually want to know the total amount of money that we should pay right? So there’s one more step. We – you know – if we just use this tip calculator as it is, then we have to add the two dollars to the twenty-dollar total bill, and then we would have to – in our head – add that up as $22. Well we can have our program do the whole thing for us. What we can do is we can add the tip amount to the total bill. Okay so if we want to store the grand total I guess you could say, which would be the tip amount plus the total, we could create a brand new variable. Or we could use the the total to store the grand total. Okay so let’s go ahead and and do it with a separate variable first, and then we’ll redo it using just the total. Okay so let’s create one more double. So I’ve got three doubles right here and so I might be thinking to myself, you know that’s a lot of variables declared with one statement. So I could say well maybe four is my maximum. So i could add it right here and i could say grandTotal. That’s perfectly fine right, it’s not off the edge over there, but you know you might say to yourself, maybe I only want to have three on one statement. So I can create a brand new line, and I can say double grandTotal, right there.Ok so now notice that I have this red underline right there, the squiggly underline. And Visual Studio is doing its best to try to let me know where the error is. The error is not actually with double right there. It’s actually right here at the end. We need a brand-new semicolon right there, and now that error will go away. So whenever you see an error in your code that Visual Studio or Xcode or whatever IDE you are using, if it detects a problem, it could be on that statement, it could have been right there, or it could be on the line above. So whenever you see an error, don’t forget to check on the statements that are above where your IDE has detected a problem. Okay so now we have four doubles: a total, a tipRate, a tipAmount and a grandTotal. So what we can do is we can say, anywhere after tipAmount, right – so we’ve calculated tip amount right there. If I try to calculate the grand total right here: grandTotal equals tipAmount plus the total, well that’s too soon right? Because I haven’t calculated the tip amount, I’m sorry, right here – I haven’t calculated the tip amount yet. The calculation for the tip amount is on this statement. Which means that this will be, what we call garbage. It will contain a garbage value. I’ve declared it, but I haven’t set it to any particular value. So i am using it before it has been initialized or set to a value. Visual Studio is actually one of the better IDEs for this, because if i try to run it, I don’t see any errors in my code, but if I try to build it and run it, it will refuse to run, because it will reach this statement right there, and it will say that tipAmount does not contain a valid value yet. So I now have an error in my code. So the answer is “no,” I don’t want to run the last successful build, because I want to fix my errors. So I click “no” and then if I look on the error list down here, it says that uninitialized local variable tipAmount used. So it was uninitialized. It’s not set to a value yet. The great thing about these errors is it tells you which line the error was on, you can see it’s on line 15, and then the other thing I can do is double click on it. And the cursor will jump right to the statement. Alright the fix for this is really easy. All I have to do is cut that line and move it down to the statement, to the line after i calculate the tip amount right there. Ok so now I’m going to get rid of this empty line, and then I can output the tip amount right there. And then what i can do is i can output the grand total right after that: cout

Tip Calculator
Tagged on:         

One thought on “Tip Calculator

  • February 24, 2018 at 7:09 pm
    Permalink

    hello how do you do this in a basic c# console app?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *