![]() |
|
|
{2} Declaring All Your Variables
The first thing you should do in any C++ program is to include the header files (libraries) which your program will use, set up the main() function and declare all your variables. This is what you should enter in the beginning:
#include <iostream.h> Since we will only be dealing with numbers, we don't need to include the string library. The iostream header file is included at the very top, and after it we have a blank main() function where the code will be placed in. We will only be using four variables in this project: first number, second number, arithmetic operator and answer. Declare these variables as follows: char operation; double firstnumber=0; double secondnumber=0; double answer=0; We use the double data type for our variables because the user might enter numbers with decimal points, and therefore, the answer could also contain decimal points. In other words, we are not only dealing with whole numbers. Setting the variables to zero is considered good programming. The variable operation is declared as char because it will hold one of four characters: +, -, *, /, which will determine the type of operation that will be used for the two numbers. That's it. We now have a fully working program that will compile without errors. In the next step, we'll add all the code that will be used to operate the application. |