Calculations Using Bloodshed Dev C++ 1
Calculations Using Bloodshed Dev C++ 1
Calculations Using Bloodshed Dev C++ 1
By Janine Bouyssounouse
Bloodshed Dev C++ is a free program to make writing and compiling C++
programs easy to do. It makes executable files quickly so programs can be
shared with others as soon as they are written and debugged. Bloodshed Dev
C++ can be downloaded from the website:
http://www.bloodshed.net/devcpp.html.
There is more than one way to start writing programs in Dev C++. This
document discusses using a project for each program. This encourages using
an organized file structure to keep each program in a separate location for
future use. Remember that reusing code is a good idea and it is nice to be
able to find the folder with everything for a program in one place.
Click on the File menu and select New – Project. Choose Console
Application for this program. Type in a name for the project, such as Display
Input, in the name field in the bottom left corner of the window. Click on the
OK button. Choose a place on the computer to save the project. It's a good
idea to make a folder for each project. Once the project is saved, some basic
items show up on the screen. These items give a shell of a C++ program and
include things that are unique to Bloodshed Dec C++ as well.
using namespace std; - This tells the compiler that you will be using key
words that are included in namespace std.
int main(int argc, char *argv[]) - This starts the main function.
{ - Main is contained between { and }.
Type in the first lines of the program by clicking at the beginning of the first
line and pressing enter a couple of times, then moving to the first blank line.
Type in these first two lines of code:
// This program gets input from the user, does calculations and displays
// the results using functions
The next line of code can be a comment stating your name as the
programmer and the date the program was written.
Type the following line of code after the using namespace statement:
int askNumber();
Int means there is an integer value returned to the main function from the
askNumber function. AskNumber is the name of the function. The empty
parentheses show that nothing is being passed to the askNumber function
from the main program. The semicolon shows the end of the line of code.
} // end main
Next we will start typing the askNumber function at the end of the program,
outside of the curly braces for the main function.
Notice the comments are listed on the lines before the start of the function.
The first line of the function looks exactly like the function prototype, except
for the semicolon at the end.
{
int response; // declares int variable response
cout << "\nPlease type an integer: "; // displays prompt to get input
cin >> response; // places the user input into response variable
return response; // sends the contents of response back to main
} // end of askNumber function
This function uses a variable to hold the input from the user. The variable
name in the function is only for that function and a separate variable needs
to be declared in the main function in order to handle the data returned from
the askNumber function. I am using different variable names to illustrate the
fact that they are actually different, even though they share the same
information.
To do this, type the following code after the first curly brace in the main
function and before the system("PAUSE") line of code:
Note the declaration of two variables on the same line of code. This is
optional and is up to the user. Grouping the variables for what they will be
used for can make it easier to understand the code at a later date.
There is a need for two different variables with two different names because
we want to do a calculation with two different numbers from the user. This
shows the use of the same function to get more than one piece of
information by calling it more than one time and using a different variable
for each time it is called.
The program is only partially completed. We now have input from the user,
but we still need to do something with that input. Since the next function
needs to have the information from the first function, we are going to pass
that information to the next function. Now the parentheses at the end of the
function name will be filled, instead of empty.
Type the following code after the askNumber function prototype at the
beginning of the program:
Int states there will be and integer returned from the function. Int num1, int
num2 in the parentheses states there will be two integer variables passed to
the function from the main function. I used the same variable titles (num1,
num2) as used in the main function, but they can have different titles.
The variables passed do not need to be declared inside of the function as the
last one did. They are passed and used with the title given to them in the first
line of the function.
A new variable is created to store the answer to the calculation. The two
passed variables are added together and the result is passed back to the main
function.
Before calling the add function, another variable needs to be declared in the
main function to hold the result of the add function. Declare the new
variable on the line following the num1 and num2 variable declaration in the
main function.
Now the new function needs to be called from the main function with the
information gathered in the askNumber function. Type the following code
into the main function after the askNumber function has been called the
second time:
The num1 and num2 variables are in the parentheses after the name of the
function as a way to get the contents of the variables to the function, so the
function can do the calculations.
Now that the input has been gathered and the calculation has been done, the
results need to be displayed to the user. It is a nice idea to show the
completed information that shows the user input as well as the result of the
Another function prototype needs to be added after the two listed at the top
of the program.
The same variable names are used as in the main function to help understand
what order the variables should be entered when calling the function. This is
a void function because nothing will be returned to the main function.
The new function needs to be called from main or else it will not be used in
the program. Type in the following line after the add function is called in the
main function.
// This program gets input from the user, does calculations and displays
// the results using functions
// Written by Janine Bouyssounouse on 08/24/08
#include <iostream>
#include <stdlib.h>
system("PAUSE");
return 0;
} // end main
// add function gets two integers from the main function, adds
// them together and returns the result of the calculation
// back to the main function
int add(int num1, int num2)
{
int answer; // declares integer variable answer
answer = num1 + num2; // does calculation and puts result into answer
return answer; // returns the result to main
} // end add function
Save, compile and run the program to see if it works. Choose Compile and
Run from the Execute menu.
6 + 7 = 13
This program waited for input on the first line. Once the user typed the input
and pressed the enter key, the second line of code printed.
Exercise 1: Write a program with an input function asking the user for two
numbers, then pass this information to two calculation functions and pass
that information to two print functions to display the results of the
calculations. Choose names for the functions that tell what they do.
Exercise 2: Write a program with and input, four calculation functions and
four print functions and call them each from the main function. Use the %
modulus operator to give the remainder for the division problem. This can be
done in a fifth calculation function.
// This program gets input from the user, does calculations and displays
// the results using functions
// Written by Janine Bouyssounouse on 08/24/08
#include <iostream>
#include <stdlib.h>
int askNumber();
int add(int num1, int num2);
int multiply(int num1, int num2);
void displayAdd(int num1, int num2, int answerAdd);
void displayMult(int num1, int num2, int answerMult);
system("PAUSE");
return 0;
} // end main
// add function gets two integers from the main function, adds
// them together and returns the result of the calculation
// back to the main function
int add(int num1, int num2)
{
int answer; // declares integer variable answer
answer = num1 + num2; // does calculation and puts result into answer
return answer; // returns the result to main
} // end add function
// multiply function gets two integers from the main function, multiplies
// them together and returns the result of the calculation
// back to the main function
int multiply(int num1, int num2)
{
int answer; // declares integer variable answer
answer = num1 * num2; // does calculation and puts result into answer
return answer; // returns the result to main
} // end multiply function
3 + 7 = 10
3 x 7 = 21
// This program gets input from the user, does calculations and displays
// the results using functions
// Written by Janine Bouyssounouse on 08/24/08
#include <iostream>
#include <stdlib.h>
int askNumber();
int add(int num1, int num2);
int multiply(int num1, int num2);
int subtract(int num1, int num2);
int divide(int num1, int num2);
system("PAUSE");
return 0;
} // end main
// add function gets two integers from the main function, adds
// them together and returns the result of the calculation
// back to the main function
int add(int num1, int num2)
{
int answer; // declares integer variable answer
answer = num1 + num2; // does calculation and puts result into answer
return answer; // returns the result to main
} // end add function
// multiply function gets two integers from the main function, multiplies
// them together and returns the result of the calculation
// back to the main function
int multiply(int num1, int num2)
{
int answer; // declares integer variable answer
answer = num1 * num2; // does calculation and puts result into answer
return answer; // returns the result to main
} // end multiply function
// subtract function gets two integers from the main function, multiplies
// them together and returns the result of the calculation
// back to the main function
int subtract(int num1, int num2)
{
int answer; // declares integer variable answer
answer = num1 - num2; // does calculation and puts result into answer
return answer; // returns the result to main
} // end subtract function
// divide function gets two integers from the main function, multiplies
// them together and returns the result of the calculation
// back to the main function
int divide(int num1, int num2)
{
// mod function gets two integers from the main function, multiplies
// them together and returns the result of the calculation
// back to the main function
int mod(int num1, int num2)
{
int answer; // declares integer variable answer
answer = num1 % num2; // does calculation and puts result into answer
return answer; // returns the result to main
} // end mod function
20 + 3 = 23
20 x 3 = 60
20 - 3 = 17
// This program gets input from the user, does calculations and displays
// the results using functions
// Written by Janine Bouyssounouse on 01/06/09
#include <iostream>
#include <stdlib.h>
#include <cmath>
void welcome();
double askBase();
double askPower();
double calculate(double base, double power);
void displayAnswer(double base, double power, double answer);
system("PAUSE");
return 0;
} // end main
This program will ask for two numbers. One will be the
base and the other will be the power the base will be
raised to in the calculation. The answer will then be
displayed on the screen.
// This program gets input from the user, does calculations and displays
// the results using functions
// Written by Janine Bouyssounouse on 01/21/09
#include <iostream>
#include <stdlib.h>
#include <cmath>
system("PAUSE");
return 0;
} // end main