Your basic layout for C++ programs can be different that what you are normally use to.
The first line of your program will identify the header file you will be using. The
header file I will be using is
The beginning of each of the code samples and programs I use will look like this:
*Note, at the end of the main function you see "return 0;" - Once the function has completed everything, this will return to the OS the number 0 to indicate the successful ending.
Creating a Console Application
Again, this tutorial is for Visual C++ and for this part, it would be quite helpful if you install it on your machine so that you can follow along. But If you already have a copy, then you can start off by opening Visual C++.
From here, you have now created a project and a workspace. The workspace will hold several projects, and your projects hold the source code files, ect. You have not added a source file to your project, so to do that, follow the steps below:
Be careful not to open several different source code files under the same project and attempt to compile them. Make sure that each project has only the files it needs, and start new workspace for different work. I say this because if you're working on several source files, and try to compile and you're getting an error, but cant find it -- the compiler is compiling all the source code files in the workspace, so this can create conflicting problems that are un-necessary.
cin and cout
cin and cout are two streams you use for input and outputting data. You will combine these streams with an insertion operator (<<) and extraction operator (>>). The insertion operator goes with cout stream, and the extraction operator goes with cin. "<<" inserts information into the cout stream to display on the screen, and ">>" takes data and stores into cin from the keyboard. Take a look below how they work:
This would display to the screen: "I drink Mountain Dew".
This will print on the screen, "How old are you?" and when you enter a number, "cin << age" stores the number entered into the age variable.
cin gets information from the user. But what if you wish to get a string from the user? To do this, you must use the getline(); function. The getline function has two arguments between its parenthesis. The first argument is for the source of the input, and the second is the variable to store the input received. heres the syntax: getline(cin, variable); just as you see. Glance below to see an example of cin using getline:
The code sample will display, "What is your name? " And wait for you to enter your name. getline(); stores the string you enter into the variable "name". So, after this, if you were to add the code, cout << "Your name is:" << name << endl; It would show, "Your name is: <whatever you typed>.
Notice the endl; at the end of the code samples above. This is very simple to remember, it
just takes the cursor and moves it to the next line, what they call a hard break return. If
you used two endl's, it would take the cursor down two lines after the statement. It is the
same as using "
Arithmetic Operators and Math Functions
Visual C++ uses alot of the normal arithmetic operators you might currently be use to.
*Note: These functions are defined in <cmath> ; so if you plan to use these in your source code, be sure to include it just as you included iostream.
Here is a complete code sample that uses a bit of what we have learned thus far:
No Comments for this page.