First c++, calculator

By awkwardsaw on Mar 07, 2010

this is my first c++ program, its pretty simple :p

its a calculator, which just adds, subtracts, multiplies, divides, and squares

instructions are in the script, :D

i cannot compile this my self for some reason, i keep getting a "all-before does not exist - don't know how to make it" error

P.S: i would like ideas on how to make it better, and ideas on what i should make next :D

#include <iostream>
using namespace std;

int a;

class answer {
      struct num {
             float num1;
             float num2;
      } number;
      char x;
      public:
             float addition () {return(number.num1 + number.num2);}
             float subtraction () {return (number.num1 - number.num2);}
             float multi () {return (number.num1 * number.num2);}
             float divide () {return (number.num1 / number.num2);}
             float square () {return (number.num1 * number.num1);}
             void getinput();
             void getnums();
             void switcharoo();                        
};

void answer::getinput() {
    cout << "instructions - type: " << endl;
    cout << "'+' to add" << endl << "'-' to subtract" << endl << "'*' to multiply" << endl;
    cout << "'/' to divide" << endl << "'2' to square" << endl;
    cin >> x;

}    

void answer::getnums() {
    cout << "First Number: ";
    cin >> number.num1;
    cout <<"Second Number: ";
    cin >> number.num2;
} 

void answer::switcharoo() {
     switch (x) {
      case '+':
           cout << addition()<<endl;
           break;
      case '-': 
           cout << subtraction()<<endl;
           break;
      case '*': 
           cout << multi()<<endl;
           break;
      case '/': 
           cout << divide()<<endl;
           break;
      case '2': 
           cout << square()<<endl;
           break;
      default:
        cout << "wrong input!";
        a=0;      
    } 
}

int main() {
    answer ans;
    while (a==0) {
         a=1;
         ans.getinput();
         ans.getnums();
         ans.switcharoo();
    }     
    system("PAUSE");
    return 0;
}

Comments

Sign in to comment.
awkwardsaw   -  Mar 12, 2010

ok, thanks :)

 Respond  
sunslayer   -  Mar 10, 2010

using any system() command in general is a bad habit, for one it is OS bound, meaning that not all OS' support it and if u try to compile it on one without it u will get an error, its also a security risk because if someone hacked your computer and changed pause.exe to something malicious you could harm your computer.

using cin.get() is not exactly like system("pause"), but it can be used to accomplish all the same tasks

also in your code, u have 'a' being a global variable but its only used in main()

 Respond  
awkwardsaw   -  Mar 10, 2010

yes it is, :)
i just use what ever works for me,

also, why is system("pause") worse than cin.get()?

 Respond  
Gummo   -  Mar 09, 2010

awkwardsaw, it must load it by default then. It is a standard library, after all.

 Respond  
sunslayer   -  Mar 08, 2010

instead of using system("pause") cin.get() is better

 Respond  
awkwardsaw   -  Mar 08, 2010

Gummo: Dev-c++ says otherwise, :p

also, changed some stuff around

 Respond  
Gummo   -  Mar 08, 2010

You can't use system("pause") without the stdlib.h header file..

 Respond  
sunslayer   -  Mar 07, 2010
is a good tutorial site and Microsoft has a large tutorial that u can download from their site i cba to find the link atm tho
 Respond  
awkwardsaw   -  Mar 07, 2010

D: this is so backwards from pascal syntax.. lol

thanks :D

like i said, lots of stuff is just for learning, > functionality, like the functions, structure, ect

 Respond  
sunslayer   -  Mar 07, 2010

this should have been posted in the C++ forums

int argc, char *argv[]

is unneeded as you do not use them at all and i don't think your using the cmd prompt, also

var x string;

var is an invalid data type and x and string should be reversed i.e., string x. however since your only using it to store +-*/ using char would be more efficient.

instead of using a loop you can use do... while, and in your switch statement you need to add a break after each statement or it will continue until the end or it reaches a break

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    char x,a;
    double num1,num2;
    do
    {
        system("cls");
        cout << "instructions - type: " << endl;
        cout << "+ to add" << "- to subtract" << endl << "* to multiply" << endl;
        cout << "/ to divide" << endl << "\n\n";
        cin >> x;
        cout << "First Number: ";
        cin >> num1;
        cout <<"Second Number: ";
        cin >> num2;
        switch (x) {
          case '+':
               cout << num1 + num2 << endl;
               break;
          case '-':
               cout << num1 - num2 << endl ;
               break;
          case '*':
               cout << num1 * num2 << endl ;
               break;
          case '/':
               cout << num1 / num2 << endl;
               break;
          default:
               cout << "wrong input!\n";
        }
        cout << "Go again? (y/n) ";
        cin >> a;
    }   while(a=='y');
   return 0;
}
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.