![Very Happy :D](./images/smilies/icon_biggrin.gif)
This isn't my first C++ program (more like third), but it's my first one that can do something, and the first one that doesn't have a bug. And my first one with functions.
Please tell me where and how I can improve. It does multiplication, division, addition, and subtraction.
I originally started in Visual Studios, but I switched to Dev-Bond. I prefer it a lot more.
Next stop, Classes! And objects.
![Cool 8)](./images/smilies/icon_cool.gif)
Code: Select all
#include <cstdlib>
#include <iostream>
using namespace std;
float GetNumber();
int GetRepeat();
void DispAns(float);
float fAdd(float, float);
float fSubtract(float, float);
float fMultiply(float, float);
float fDivide(float, float);
int main(int argc, char *argv[])
{
int function; //Number that gets the function
float a; //First input number
float b; //Second input number
float ans; //answer variable
int repeat; //checks to repeat
while (repeat)
{
cout<<What>";
cin>>function;
if (function == 1)
{
a = GetNumber();
b = GetNumber();
DispAns(fAdd(a, b));
}
if (function == 2)
{
a = GetNumber();
b = GetNumber();
DispAns(fSubtract(a, b));
}
if (function == 3)
{
a = GetNumber();
b = GetNumber();
DispAns(fMultiply(a, b));
}
if (function == 4)
{
a = GetNumber();
b = GetNumber();
DispAns(fDivide(a, b));
}
repeat = GetRepeat();
}
return EXIT_SUCCESS;
}
float GetNumber() //Gets a number
{
int number;
cout<<"What is the number? ";
cin>>number;
return number;
}
int GetRepeat() //Checks user input to see if to run again
{
int again;
cout<<"\n\nDo you what to try again?";
cout<<"\n0) No";
cout<<"\n1) Yes\n";
cin>>again;
if (again)
{
cout<<"------------------------------\n";
}
return again;
}
void DispAns(float ans) //Displays the answer
{
cout<<"\nThe answer is: "<<ans<<".\n";
cout<<"------------------------------\n";
}
float fAdd(float a, float b) //adds two numbers
{
return a+b;
}
float fSubtract(float a, float b) //subtracts two numbers
{
return a-b;
}
float fMultiply(float a, float b) //multiplies two numbers
{
return a*b;
}
float fDivide(float a, float b) //divides two numbers
{
return a/b;
}