Search
Left Quote    Treat your password like your toothbrush. Don't let anybody else use it, and get a new one every six months.
- Clifford Stoll    
Right Quote
 
[login] | [Register]
 

Beginning Visual C++ 6.0

by: bs0d
Page: 3 of 10
(View All)

Basic Structure


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 . There are many different ones that can be used. Each header file has its own syntax for performing various actions.


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++.


  1. Click file --> New, and a new box will open.
  2. Click on the Projects tab and select the option that reads, "Win32 Console Application" from the list of project types.
  3. Now click on the Create new workspace option.
  4. You can name your project in Project name textbox.
  5. After you have named your project, click OK to close the dialog box.
  6. A new dialog box will open that reads, "Win32 Console Appication - Step 1 of 1". You want to select the option that reads, "An empty project".
  7. Click finnish and the New Project Information box will appear that shows you the details, click Ok to continue.

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:


  1. Click Project from the top menu bar and navigate to --> Add to Project, and click New.
  2. Now select the Files tab from the new dialog box, and select C++ Source File from the list below.
  3. Now just name it using the text box under, "File name" --Click Ok and you now have a source code file to begin you work!

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".

cin:



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.

getline


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>.


endl


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 "
" in HTML.


Arithmetic Operators and Math Functions


Visual C++ uses alot of the normal arithmetic operators you might currently be use to.

  • ( ) - Overrides normal presedence rules.
  • * - for Multiplication.
  • / - Used for Division.
  • % - Modulus (unlike visual basic, where you would use "Mod").
  • + - Addition, as you might guess.
  • '-' - Negation or subtraction. Difference is that negation is unary, subtraction is binary. Unary requires one operand by the operator, binary requires two.

Notice that there is no "^" to represent exponents, like VB. This is because Visual C++ does not support that operator (like javascript). However, a function is provided to perform exponentiation. This is the pow(); function. In fact, there are many math functions that C++ does support, I will list a few:

*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.

  • Square root - If you wish to find the square root of x, use: sqrt(x);.
  • Sine - To find sine of x, use: sin(x);
  • Cosine - To find cosine of x, use: cos(x);
  • Tangent - To find the tangent of x, use tan(x);

Here is a complete code sample that uses a bit of what we have learned thus far:





1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

99,727 Views
1 Total Comments
4 Rating of 5 (2 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· C Tutorial
Your Article Here



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank