Sep82008

C++ Tutorial # 1

C++ Tutorial # 1 Post Image

I’m sure you’re here to learn useful C++ programs, not crap, so let’s start off with something basic, but useful for life. We are going to make a C++ program that will prompt the user for a number, grab that number, ask for another number, grab that number, add the numbers then output the answer to the screen, so the user gets the answer to the problem. Some of you might know how to do this, but bear with me I have a fun thing at the end that you might not know. As I stated before, source codes are included, and can be downloaded at the bottom of the page.

So let’s start off with the adding program. In case you don’t want to just add numbers you want a program to subtract, multiply, or divide the numbers the user inputs, then don’t be down! You can manipulate the code to your wanting. If you want the program to subtract simply change the function (explained later). If you want more than 2 numbers, add more lines of code to prompt user for a third number, fourth etc, and for the program to get those numbers and also add, subtract, etc the to first and second numbers entered. This just requires more lines of code, but it’s simple to do.

Ok, so what we need to do. Start Dev C++ or whatever compiler you have and create a new file (CTRL + N for Dev C++). Now you can start. So first off we need the top of the program, the main stuff of any program.

#include <iostream>
#include <math>

using namespace std;

Now you’re like wtf, unless you know this already. In C++, in order to use various commands, you need to include the necessary libraries where the commands are located. For our math stuff we need to the math library (#include <math>). Now for our program to send lines of text to the screen for the user to know what to do, and for the program to grab the user’s answer, you need the ‘iostream’ library. It’s the most basic, every program has it. It’s very necessary. Now ‘using namespace std;’, what’s that? Well it’s always there when ‘iostream’ is used. It just is, I can’t really explain it; just don’t question it ;). Ok now we need our prompts and lines of code for the functions to occur.

int main()
{

Now int main() is where the opening lines to our program are. The ‘{‘ opens up the program, and what ever is within it and the later ‘}’ (to close) are associated with int main(). The program starts here, and in other programs you can have more codes that extend out of it, then come back, but that’s not for this tutorial. Next we need to define our variables that we will use in this program. There are data types for these variables, which define how many numbers they can be up to. ‘int’ is for smaller numbers, ’short’ for longer numbers that ‘int’, etc. For a list head here.

float FirstNumber;
float SecondNumber;
float TheAnswer;

Now to explain. The float (if you read the link I gave you, you would know that it’s a large number) is just in case the user picks a huge number to add to a huge number, and the answer will be huge. ok so we have our variables; FirstNumber is the first number the user will enter, SecondAnswer is the second number entered and TheAnswer is the answer to the problem. These will help later to put any number the user enters into the function; just read on to understand more.

cout << “nPlease enter the First Numbert”;
cin >> FirstNumber;
cout << “nPlease enter the Second Numbert”;
cin >> SecondNumber;

Now to explain all this jumble. ‘cout’ is the command to send info to the screen always followed by <<. Then the text to be displayed is put in quotes. ‘n’ is new line, and ‘t’ is tab. This is so that the text is placed one line down, but the user’s answer will be displayed on the same line as the text, one tab away. Then the next line will be below the first line and ask for the second number. Hope you get this if not comment. Hell comment on anything you don’t get. Ok so ‘cin’ is what is taken, what is entered, and is followed by >>. Then what is typed by the user is assigned to FirstNumber or SecondNumber respectively based on which question it is entered for. Get it? The ‘;’ is used to end the line and tell the compiler that there is another set of commands after. Now for the function.

TheAnswer = FirstNumber + SecondNumber;

Ok so basically ‘TheAnswer’ is to be set to FirstNumber + SecondNumber (this is why there are variables names for the first and second number the user enters; they will be different each time the program is used so numbers can’t be used). Now for the output to the user, then the close of the program, and then that fun thing I promised all the way above.

cout << “nYour answer is ” << TheAnswer << “!”;
cout << “nThanks for using the program!”;

Explanation. You know the ‘cout’ but what you don’t know is that to have less lines of code you can add a << and put more stuff after the first commands, in this case we want to display the answer, so without quotes we put the variable name ‘TheAnswer’. The other line of code is to say thanks for using the program. Next off we need to close the program.

return 0;
}

Ok ‘return 0;’ ends the program and ‘}’ closes ‘int main()’. That’s all for the program. Let’s see what we have so far, then we’ll get to the fun thing.

#include <iostream>
#include <math>

using namespace std;

int main()
{

float FirstNumber;
float SecondNumber;
float TheAnswer;

cout << “nPlease enter the First Numbert”;
cin >> FirstNumber;
cout << “nPlease enter the Second Numbert”;
cin >> SecondNumber;

TheAnswer = FirstNumber + SecondNumber;

cout << “nYour answer is ” << TheAnswer << “!”;
cout << “nThanks for using the program!”;

return 0;
}

Right…lots of code. Now compile it. Now run it by double clicking the .exe file made (will have same name as your saved .cpp file), enter a number then again. WTF! What happened? The program closed before you saw the answer right? Yeah…To run this you have to open command prompt and type int he address of the .exe file then run it for it to not do this. Now comes the fun thing. So that we can view the answer we need an option for the user to either want to run the program to run again, or to close the program. Lets do the following:

char ExitOr;

cout << “nDo you want to run this program again? Y/N:t”;
cin >> ExitOr;

The user will choose ‘y’ (yes) or ‘n’ (no) to run the program again, and ExitOr will be assigned that character, now for the rest of the code.

if (ExitOr == ‘n’)
{
    exit(1);
}
else
{
    if (ExitOr == ‘y’)
    {
         main();
    }
}

Explain to you? Ok. So basically this is if ‘ExitOr’ is ‘n’ then we exit the program, ‘else’ (however) if ‘ExitOr’ is ‘y’ we go back to ‘int main()’ and start the program over. Compile this, run the program, put two numbers and what do you get? Presto we get asked. Great. Now you should have this (unless you changed some names or added more numbers and lines of code or made the addition to subraction etc).

#include <iostream>
#include <math>

 

using namespace std;

 

int main()
{

    char ExitOr;
    float FirstNumber;
    float SecondNumber;
    float TheAnswer;

 

    cout << “nPlease enter the First Numbert”;
    cin >> FirstNumber;
    cout << “nPlease enter the Second Numbert”;
    cin >> SecondNumber;

 

    TheAnswer = FirstNumber + SecondNumber;

 

    cout << “nYour answer is ” << TheAnswer << “!”;
    cout << “nThanks for using the program!”;
    cout << “nDo you want to run this program again? Y/N:t”;
    cin >> ExitOr;

 

    if (ExitOr == ‘n’)
    {
        exit(1);
    }
    else
    {
        if (ExitOr == ‘y’)
        {
            main();
        }
    }
    return 0;
}

There you go!

Download:

Download has been taken down while I transfer hosting companies. I’ll bring it back very soon

Nobody has responded yet, be the first to leave your thoughts!

Comments closed on 22 September, 2008 @ 10:39.