My first (working) C++ program!

Discuss whatever you want here--both QB and non-QB related. Anything from the DEF INT command to the meaning of life!

Moderators: Pete, Mods

Post Reply
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

My first (working) C++ program!

Post by Mentat »

:D

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. 8)

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;
}
For any grievances posted above, I blame whoever is in charge . . .
neuro
Newbie
Posts: 1
Joined: Sun Nov 11, 2007 7:28 pm

"no bugs..."

Post by neuro »

#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

// bug 1: using an uninitialized variable. repeat could be _ANYTHING_ at this point, even 0 (which would cause the program to just exit). Advice: set int repeat = 1;

while (repeat)
{
// bug 2: missing quotation mark
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
{
// generally C++ cin/cout style uses newl instead of "\n", like cout<<ans<<newl;
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
{
// bug (if you want to call it that) 3: unhandled division by 0 exception. What if b == 0?
return a/b;
}
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Thanks

I noticed the repeat=0 thing too, but when I ran it, it was never a problem (I might have pasted wrong).

Thanks for the division=a/0. I forgot about error handling. A lot more probably needs handling too.
For any grievances posted above, I blame whoever is in charge . . .
Post Reply