Getting Started in Microsoft Visual Studio 2008 Console Application Tutorial

advertisement

Getting Started in Microsoft Visual Studio 2008 Console Application Tutorial

Step 1. In Visual C++, select "File Menu-->New-->Project"

Step 2. In the dialog box that pops up, click on "Win32" in the side pane and select

"Win32 Console Application." Make sure that the "Create Directory for Solution" box is

NOT checked and leave the default path the same (as shown---You can leave the

Create Directory for Solution box checked and change the default path, but for purposes of this tutorial, leave it at the settings

Step 3. The Win32 Application Wizard will pop up . Click "Next."

Step 4. Make sure that the following are checked:

----------1. Under "Application type": Console Application

----------2. Under "Additional Options": Empty Project

----------Then: Press "OK"

Step 5. Go to the "Solution Explorer" on the left side and right-click on "AddCalc" but be sure not to right click on "Solution AddCalc (1 project)." Once you have right clicked on AddCalc select "Add-->New Item"

Step 6. In the Dialog Box that appears, on the left pane select "Visual C++" and on the top section choose "C++ file (.cpp)" and change the name to AddCalc (as shown). Press

"OK".

Step 7. A blank screen will appear showing the contents of "AddCalc.cpp" (as shown in

SS1). Copy and Paste the following code into this blank white area (for a line-by-line breakdown of the code, go to part 3): view source print?

01 // initializing C++

02 #include <iostream>

03 using namespace std;

04

05 // declaring function prototypes

06 float addition (float a, float b);

07

08 //main function

09 int main ()

10 {

11 float x; //

12 float y; //declares variables

13 float n; //

14 int b;

15 b = 1; //sets value of b to 1

16 cout << "Simple Addition Calc- First Program"; //displays info about program

17 while

(b==1) //creates loop so the program runs as long as the person wants to add numbers

18 {

19

//following code prompts the user for 2 numbers to add and calls function addition to display results

20 cout << "\n" << "Type a number to add (can also use negetive, decimals, etc.): ";

21 cin >> x;

22 cout << " Second number: ";

23 cin >> y;

24 n = addition (x,y);

25 cout << "Ans: " << x << " + " << y << " = " << n << "\n";

26

//following code sets b to the value the user imputs to determine if the loop is broken to end the program

27 cout << "Solve another operation? (1=yes, 2=no): ";

28 cin >> b;

29 cout << "\n";

30 if (b==2)

31 cout << "Terminating application.";

32 }

33 //ends the main function of the code

34 return 0;

35 }

36 //following function adds the numbers

37 float addition (float a, float b)

38 {

39 float c;

40 c = a+b;

41 return (c);

42 }

43 //END C++ CODE

Step 8. (con.) The blank area with the code in it is shown in SS2 along with the SAVE

ALL button highlighted (you may wish to save your work here).

Step 9. Go to the Build Menu and select "Build Solution"

Step 10. Your Output Screen (near the bottom of the page) should show the following

(the most important being: *Build: 1 succeeded, 0 failed, 0 skipped, 0 up-to-date*):

Step 11. PRESS F5 on you keyboard, and a small box should pop up that is running your program that will add 2 numbers together (as shown). This is command to debug your application, which for our purposes is synonymous to testing.

Step 12. The Debug window should work. Assuming it does, to save your program to

AddCalc.exe, first go to the drop down menu near the top of the screen which says

"Debug" and change it to "Release"

Step 13. Go to the Build menu and select "Rebuild" (as shown). This should give output that is SIMILAR, not the SAME as the output from Step 11.

Step 14. Go to Windows Explorer and go to:

"C:\Documents and Settings\your_username_here\My Documents\Visual Studio

2008\Projects\AddCalc\Release"

Double click on AddCalc.exe, and if it runs, you can delete the other files in this directory.

Line-by-Line breakdown of the AddCalc.exe code

Some notes: There are some basic things about C++ that you should know beforehand.

First, in C++ all spacings are equivilant (space, tab, enter), and you can have any number of the spacings, so an enter and a space both together means the same thing as a single enter or a single space. Second, a line that has // in it has a comment in it.

The comment is whatever is in that line of code after the // untill there is an enter character (this is the only case in which an enter character is not the same as a tab or a space). I will ignore all comments in this code. Finally, all lines of code should end with

a semicolon.

And without further ado, let's begin. I ignored comments because the comment will be the explination aligned to the right of the line it is in.

LINE-BY-LINE

#include <iostream> // this tells c++ that we want to use the normal imput and output library NO ; REQUIRED using namespace std; // this tells c++ that we are using the standard section of this library and the C++ language float addition (float a, float b); //this says that after the main function we will have a seperate function "addition" int main () //says that the next block of code is the main code to be executed.

{ //the curly brace starts the main function's block of code float x; //declares a variable that can use numbers with decimels and negetives float y; //declares variables that is the same as above float n; //declares a variable, same type as x int b; //declares a variable that can only use whole numbers b = 1; //sets value of b to 1 cout << "Simple Addition Calc- First Program"; //displays info about program while (b==1)

//creates loop so the program runs as long as the person wants to add numbers (when b=1)

{ //curly brace starts the loop block of code cout << "\n" << "Type a number to add (can also use negetive, decimals, etc.): ";

// prints a newline "/n" and outputs for the user to specify a number cin >> x; // provides imput for x after the string in the previous line. cout << " Second number: "; // outputs "Second number: "

cin >> y; // provides imput for y after previous string n = addition (x,y); // sets n to the value returned by addition for x and y cout << "Ans: " << x << " + " << y << " = " << n << "\n"; // Displays answer

//following code sets b to the value the user imputs, and b is the variable for the loop above cout << "Solve another operation? (1=yes, 2=no): "; //output cin >> b; //imput cout << "\n"; //Outputs new line if (b==2) //if the user imput is 2 cout << "Terminating application."; //output

} //curly brace sends ends the loop and then it is reassesed whether or not b fits the loop to go again. return 0; //ends the main function of the code

} //curly brace ends main function float addition (float a, float b) //creates a function returning a decimal value

//this function requires two variables in its call of type float.

{ //starts function float c; //creates a variable called c c = a+b; //stores the value a+b as c return ( c ); //c is the value given to the function call for addition

} //ends function

And thats it! the basics of c++ are very simple. For those of you who prefer a list of

commands and definitions:

#include <library> --------------- says you are using commands from a library of c++ commands using namespace [namespace] ------------- says you are using a subset of commands called namespace cin >> variable ----------- stores user imput to variable cout << variable/"string" << variable/"string" << ... ---------------------- outputs varType function ([paramaters]) -----------------defines a function later in the program, or with {} creates function commands varType = value --------------------defines variable while (variable==value) {}-------creates a loop that repeats as long as the variable is the same as the value if (variable==value) [if more than one command: {}] ----------------------- runs a conditional statement functionName ([paramaters]) --------------"calls" a function, the value of the call= the

"return" of the function return (variable)/value ------------------says what value the function should send back to its "caller"

Download