Thank you all for helping me. In return i made this article http://www.cplusplus.com/articles/oE18T05o/ for all the beginners who would like to study a calculator program. I'm surprised it took me more than 3 hours for just like 70 lines of code because i had to keep on restarting because it was pretty hard for me. Experienced people - comment on this if i should change anything so i don't have people looking at wrong information. Once again thanks.
#include <iostream> // declare all of the libraries
#include <cstdlib>
#include <cmath>
usingnamespace std; // this is for cout and cin
// declare the variables
double num;
int operation;
int howmany = 0;
int howmanynum;
int restart;
int menuchoice;
class setup // a class to organize things
{
public:
void menu()
{
cout << endl << "Enter 1 to use the calculater." << endl << "Enter 2 for important rules you need to know: ";
cin >> menuchoice;
}
void rules()
{
cout << endl << "Rule 1 - The calculator doesnt know order of operations so you have to enter the numbers in the correct order." << endl;
cout << "Rule 2 - The calcualtor doesnt understand parenthesis so dont use them." << endl;
}
void operations()
{
cout << endl << endl << "Here are your options for operations" << endl << endl; // give the user the operation choices
cout << "Now enter the operation" << endl;
cout << "Enter 1 for addition" << endl;
cout << "Enter 2 for subtraction" << endl;
cout << "Enter 3 for multiplication" << endl;
cout << "Enter 4 for division " << endl;
cout << endl << endl;
}
};
int main()
{
do{ // begining of do while loop to restart the program
int total = 0;
setup callfunctions; // sets up a object of class setup to call functions
callfunctions.menu(); // calls the function menu
switch(menuchoice)
{
case 1:
cout << endl << endl;
callfunctions.operations();
cout << "How many numbers are in the math problem: "; // asks the user how many numbers are in their math problem
cin >> howmanynum;
howmany = howmany + howmanynum;
cout << endl << "Enter the first number: ";
cin >> num;
total = total + num; // adds the first number to the total, otherwise the answer would be incorect
for(int i = 1; i < howmanynum; i++){ //loop
cout << endl << "Enter the number for the operation: "; // gets the operation from the user
cin >> operation;
cout << endl;
cout << "Enter the next number: "; // gets the number from the user
cin >> num;
cout << endl;
switch(operation){ // does all the math stuff
case 1:
total = total + num;
break;
case 2:
total = total - num;
break;
case 3:
total = total * num;
break;
case 4:
total = total / num;
break;
default:
cout << "Invalid input" << endl; // tells the user if he/she entered an invalid operation number
}
}
cout << endl << "The answer is " << total << endl; // outputs the answer
break;
case 2:
callfunctions.rules();
break;
}
cout << "Enter 1 to restart or 2 to quit: "; // asks the user to restart or exit
cin >> restart;
}while(restart == 1); // ending of do while loop to restart program
cin.get(); // pauses the program.Dont use system("pause")!!
return 0; // termanates the program
}