Have you ever wanted to create a C/C++ program? Well here is your chance to learn how to. In these instructions you will learn how to download, install, and register Microsoft Visual
- Step
Go to your Start menu, and find Microsoft Visual C++ 2008 Express Edition, and open it.
- Step
Once open, and go to File, go to New, the click on Project.
- Step
A window will then appear and ask you to name your project it is recommended that you use a meaningful name. This is helpful when finishing the project at a later time so that you know exactly which file is which.
- Step
Next a window will pop up. It is best to just click Finish because these are settings for more advanced users. At this time most of them will make little to no sense.
- Step
Finally, you have made it to actually coding a program. After clicking Finish in the previous step the window in front of you should now be your project. You will notice that there are three distinct areas in this window, the left window and bottom window you will ignore for now. In the upper right window is the area in which you will be typing in your code. This window should already have some code in it by default from opening the window. Highlight the code with your curser and delete it, it is useless code.
- Step
One of the first things to know about programming in any language is comments. Comments are words in your code that are skipped by the compiler when creating the code. Comments were implemented into the program to help the programmer remember what they were doing at a certain stage in the program. So when creating a program make sure to include lots of comments. This will help you later when debugging the program and errors occur, because you can jump to a certain section of code and see what you were trying to do and what went wrong. So in the upper right hand code box type in: // This is (Insert Name)'s first C++ Program.
After the double forward slash you should notice that the next turns green. Green Text signifies that the line is a comment and not a snippet of code. - Step
If your comment is going to last more than one line you can use another method of creating a comment at the begging of the extended line put /* and then continue to type your message. So following your first line of code put: /*
Then Type: I am
Hit Return: Ready
Hit Return: To Start
Hit Return: Programming!
After the last word close off the comment by typing */
Then hit return once more. - Step
In the C++ language there are many includes that must be listed in order for the program to run. These includes call out to memory to load certain features built into the compiler. In the program that you will be creating you will be using four main includes, due to some limitations of eHow I am unable to type the includes but please see the image to the right in order to view the "includes" you must type in.
Type these includes in under your comments you made in previous steps. The spacing between the words is not necessary but makes the code easier to read. Which will help you later if you have a spelling error. - Step
The next line you will need to include in your code is:
using namespace std;
This line sets aside memory for your program. You will also notice that at the end of this line there is a semicolon the semi colon tells the compiler that this is the end of a statement. - Step
Now for the main body of the program. The main program is what is considered the guts of the program this is where you tell the computer exactly what you want it to do. To start a main body of a program type:
int main ()
Following the completion of this line, hit return, then hold shift and create an open bracket:
{
How hit the return key twice, and type:
return 0;
return 0 tells the computer not to return anything signifying the completion of the program since there is only one main.
Following this hit return twice, and close the brackets by holding shift and the close bracket key.
}
Congratulations, you have just created your first program, everything will compile alright but it won't do anything which isn't any fun. - Step
The next step is to declare the variables in which we will use in our program. In C++ declaring variables acts as a spell check system telling the user when they debug the program if any variables weren't declared. If a variable isn't declared when it comes time to debug the program it is usually the result of a spelling error. This feature quickly allows the user to find their error and fix it quickly. So to declare the variables in our code that will hold integers you should type:
int random,
guess;
The word "int" is what tells the computer that the words following it will be of type integer. So the two variables that we have that will be able to hold integers are guess and random.
Notice that I used a comma between the two and finished it the statement off with a semicolon. In C++ this is allowed because the statement of declaring variables isn't over until the compiler receives a semicolon. - Step
Next we will declare another variable that is capable of holding one character. To do this we type on the next line:
char playAgain;
Once again "char" is what defines the variable "playAgain." As previously stated "playAgain" is only capable of holding one character. - Step
Now we will initialize the variable "playAgain." Since "playAgain" is a character we are only capable of giving it a value of a character. To do this start a new line and type:
playAgain= 'Y';
In C++ if you are assigning a value to a term use a single = sign. Also since we cannot directly say playAgain= Y; we must use single parenthesis around it. The reason we cannot just put playAgain=Y is because Y might be a variable and we would intern be setting two variables equal to each other which isn't what we wish to do here. - Step
Next we will need to make a loop in the event that the user would like to play the game again having successfully completed one number. To do this we insert the following code: Sorry for the inconvience but the code to insert is found in the image, this is due to some technical problems with eHow.
The loop is represented by the term while, which means while the argument is true it will continue. The argument is the code between the parenthesis, which tests whether or not playAgain is equal to either "Y" or "y."
In the C++ language if you would like to test more than one statement for an "or" you inserts to straight brackets. This is achieved by holding the Shift Key and typing the back-slash key found above the Enter key.
Following the code for the while loop, much like the main program we will need to set bounds for our loop with open and closed brackets. - Step
We will now include code that sets an internal clock within the code to 0 to randomly generate a number.
We do this by inserting the following code:
srand(time(0)); - Step
Next we will store our randomly generated number. To do this we use the following code:
random = rand() % 50 + 1;
This line of code sets the value of "rand()" divided by modulus 50, plus 1 to the variable random. Modulus 50 implies whatever the remainder of "rand ()" divided by 50 is kept. We then add one to this number because if it is a perfect divide with no remainder the number is 0, and dividing by 50 implies the largest remainder achievable is 49. In our range of numbers it ranges from 1-50, so a zero remainder would defy our range. - Step
Our next line of code tells the user the range of numbers that they should pick from.
cout << "Guess my Number between 1-50"<< endl;
In the C++ language the "cout<<" tells the computer that it needs to print a message to the screen.
The "endl<<" bit of code tells the computer to start the next bit of code on the next line.
Now we are ready to actually print the message to the display by typing "Guess my Number between 1-50."
Now we want to start the next line of code on the next line so we place "<<> - Step
Now that the user has been given the range they are now permitted to make guesses. So we must store the value that they enter.
cin >> guess;
In C++ "cin>>" tells the computer that whatever the next value entered will be stored in the "guess" variable. - Step
Now our program must continue to execute as long as the user doesn't input a correct answer. To do this we must use another loop, I am once again sorry for the inconvience caused by technical issues with eHow, once again please look at the image to the right for the code.
We will once again use a while loop which tests to see if the number the user entered matches the random number generated. It will continue until the guess matches the number generated. - Step
Since there are two viable possibilities if guess does not equal the randomly generated we need to test whether the guess is to low or to high and tell the use accordingly. So now we must insert the following "if" statement within the new while loop:
if (guess > random)
{
cout << "You were too high try again."<< endl;
}
This statement tests if guess is bigger than the randomly generated number. If the number is larger than the randomly generated number it prompts the use with a "cout" message that tells the user that their number was too large.
Please note that we use an "endl;" at the end of this statement so that the next number the user enters is clearly separated from all other guesses previously entered. - Step
Now we must test against the other possibility, if we guessed to low. Much like the previous step we insert another if statement.
if (guess < random)
{
cout << endl << "You were too low try again."<< endl;
} - Step
Now still contained within the guess not equal to the randomly generated loop we must prompt the user to guess again. If they guess wrong the loop will continue until they get it right. If they do get it right they will be able to exit the loop.
cin >> guess;
This code awaits the use to input another guess, storing the new number entered in the variable guess. - Step
We have now covered all options if the user is incorrect, so we can now assume that the user is correct. So we place the following code:
cout << endl<< "You guessed right! Would you like to play again?";
This code tells the computer to display this message to tell the user that they can stop guessing. - Step
Since the user was prompted whether or not they would like to play again we will now inform them what they must do in order to play again. As you recall, our outermost loop playAgain will continue as long as playAgain is equal to either a "Y" or a "y" so we now tell the user to:
cout<< endl << "Press 'Y' to play again, or any other character key to quit.";
This code tells the user that if any other key other than "Y" is pressed the game will end. - Step
Now we are awaiting a response from the user, so we must wait for their decision. So we wait for the variable playAgain to be reassigned a character.
cin>> playAgain;
If playAgain is either a "Y" or a "y" the game will continue if not the game ends. - Step
The coding stage is now complete; your code should look like the code found in the image.
No comments:
Post a Comment