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;
}
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()
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;
}