Lesson 7: Hands On Project!

So, we’ve talked a lot about variables and a few other things so far.  Let’s go ahead and put them together in a quick project.

We’re going to make a small application that accepts an integer value from the user and increases it by a static amount.

Let’s start off by creating a new project.  I am going to be creating a C++ Console Application.

My new, empty application looks like the following:

newApp

Now we need to start adding some code.  Since our application is going to accept information from a user and display it, we should include iostream.

Add the following like below the “stdafx.h” include line.  If your file does not have this like, don’t worry, you shouldn’t need to add it.  If it does not have this line already, just add the iostream line where this would be.  This will allow us to utilize things like “cout” and “cin” to output and input information.

#include <iostream>

Next, we’re going to need to figure out what variables we’re going to need to work with.

Determining and writing variables

We will be accepting a number from a user and we’ll need to store that somewhere so that’s one variable we’ll need to think about.

The purpose of the app is to increase the users number by a pre-determined amount.  This amount will need to be stored in a variable, so that’s the second one.

The final variable in out super awesome application is going to be the number after it’s been increased.

The following lines of code will go in the body of the main() function (between {}) but before the return statement.

  • An integer variable that we will add the users input to, since this will be changing based on what the user enters, we’ll initialize it to 0.

 

int userInput = 0;
  • A constant integer variable  that we will increase the users input by.  This will always be the same and we don’t want it to change so we make it a const and initialize it to the number we want to increase by.
const int numIncrease = 5;
  • Another integer variable which will contain the new value.
int combinedNum = userInput + numIncrease;

 

Writing the rest

Now that we have our variables, we need to write the lines that will accept the information from the user and then display it back to them.

As I have mentioned in previous lessons, it’s best to NOT use the “using namespace std;” line of code. Not using it requires a little extra code to be written whenever you want to use something from the std library, but it’s best practice and we always want to build good habits.

First, we want to display text to the user asking them to enter a number.

std::cout << "Enter a number you would like to increase by " << numIncrease << ": ";

You may notice that I added an extra item to the end of the line that displays the “numIncrease” variable.   Also, pay attention to the spaces I have included in the quoted sections.  I added an extra space after the last character before the ending quote.   This is just to help with the formatting when it displays on screen.

This line will display: “Enter a number you would like to increase by 5: ”

So far, we have only utilized the “cout” statement.  Now we are going to use the “cin” statement.  As you have probably guessed, “cin” takes input.

Below the line that asks the user for input, we need to create some code that actually takes that input and saves it in a variable.

std::cin >> userInput;

Looking at that line of code, you can see that the arrows are pointing the opposite direction of “cout”.   This is because with “cout” we are passing information into the “cout” stream which displays it on the screen.  With “cin,” we are actually doing the opposite, passing information out of the “cin” stream and into the variable to be stored for later use.

Now our last line of code is going to display the increased number.

std::cout << userInput << " increased by " << numIncrease << " equals: " << combinedNum << std::endl;

This line of code utilizies aspects that we have already discussed.  Basically just displaying variables and some text to the user.

Now save your application, build it, and run it and test it out.

firstCompletedApp

WAIT! WHAT IS THIS NONSENSE!  5 increased by 5 isn’t 5!

firstWhatWeSee

TROUBLESHOOTING!

Troubleshooting is going to be a huge aspect of any application you’ve ever written.  I don’t know any programmers who just write perfect code the first time through.  Some might be better than others and might be able to do fairly complex things without issues, but eventually there will be something that needs troubleshooting.

With our application as it currently is, we will always display 5 as the final answer no matter what number you enter.

Before moving on to determine why this is happening, take a look at your code to see if you can figure it out on your own.  After you’ve figured it out or you’ve hit a wall, come back and finish the lesson. 

This is due to the order of the lines of code in our application.  Yes, order does matter.

Take a look at our list of variables at the top of our application.  We have 3 of them; userInput, numIncrease, and combinedNum.

The variable that combines our two integers does its work when those two integers equal 0 and 5 respectively.  We haven’t modified the user input variable yet and the machine can’t tell the future!

If this is the case, the math in the combinedNum variable needs to occur after the userInput variable has been modified.

To fix this, we need to take line 12 from the code above and put it after the “cin” statement that accepts the userInput.

You can’t just put it at the bottom though.  It needs to go above the “cout” line of code that displays the combinedNum variable.  It needs to be set before we can display it.

The code should now look like this:

SecondCompletedApp

And after some testing, BOOM! Our application now increases our number appropriately.

SecondWhatWeSee

Congratulations on completing your second C++ application!  It’s like we’re professional developers now.  Our portfolio is growing out of control!   Ok, maybe not quite, but we are getting there.

Now play around a bit.  A small number like 5 works pretty well as input.  Does a larger number work?

Remember that we are initializing our variables as “int” and that “int” has a limited amount of numbers.

What happens if you user a number outside the range of “int”?

What happens if you use a negative number?

Do numbers with decimals in them work?

Try playing around with how you initialize the variables and see what you can get to work or what will break the application.

 

See you on the flip side!

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s