Alright I'm having some trouble with making a calculator. Its a simple text based calculator that I plan to add more features to when I get it working correctly. Main problem: Whenever I enter the answer to the first cin words start falling like drops in a waterfall. Try it out. (I'm using latest version of Dev-C++)
// Draft 1 - Simple Text Based Caculator
//Include Files
#include <iostream> //basic input output operators
#include <math.h> // additional features for when pausing works
//Shortcuts to avoid having to type std::cout/cin everytime
using std::cout;
using std::cin;
usingnamespace std;
//Beginning of actual calculator
int main ()
{
char Done;
char Subtraction;
char Addition;
char Multiplication;
char Division;
char answer;
labelA:
cout << "What basic mathematical operator shall I perform ?";
cout << "Please answer with: Division, Multiplication, Addition, Subtraction or Done.";
cin >> answer;
cin.get();
if (answer == Division); //Division section of the calculator
{
//Declaring the variables for use in the division function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be divided
cout << "Input value to be divided: ";
cin >> value1;
cin.get();
cout << "Input the dividend: ";
cin >> value2;
cin.get();
//dividing the values and outputting the answer
value1 /= value2;
cout << value1;
cin.get();
goto labelA;
}
if (answer == Multiplication); //Multiplication section of the calculator
{
//Declaring the variables for use in the multiplication function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be multiplied
cout << "Input value to be first number: ";
cin >> value1;
cin.get();
cout << "Input the second number: ";
cin >> value2;
cin.get();
//multiplying the values and outputting the answer
value1 *= value2;
cout << value1;
cin.get();
goto labelA;
}
if (answer == Addition); //Addition section of the calculator
{
//Declaring the variables for use in the addition function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be added
cout << "Input the first value : ";
cin >> value1;
cin.get();
cout << "Input the second value: ";
cin >> value2;
cin.get();
//subtracting the values and outputting the answer
value1 += value2;
cout << value1;
cin.get();
goto labelA;
}
if (answer == Subtraction); //Subtraction section of the calculator
{
//Declaring the variables for use in the subtraction function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be subtracted
cout << "Input value to be subtracted from: ";
cin >> value1;
cin.get();
cout << "Input the number to subtract from the first: ";
cin >> value2;
cin.get();
//subtracted the values and outputting the answer
value1 -= value2;
cout << value1;
cin.get();
goto labelA;
}
if (answer == Done); //Function to end the program
cout << "Good bye";
return 0;
}
Your if statements compare the user's input to Division, Multiplication, etc. all characters which you never define. So you are comparing input to undefined, uninitialized values. It's not going to work.
And I see you are using goto statements. Beware: your code will become illegible and impossible to read if you use goto statements too liberally. I know my professors refuse to even look at homeworks and assignments with goto statements. Use loops instead.
// Draft 1 - Simple Text Based Caculator
//Include Files
#include <iostream> //basic input output operators
//Shortcuts to avoid having to type std::cout/cin everytime
using std::cout;
using std::cin;
usingnamespace std;
//Beginning of actual calculator
int main ()
{
char Done = Done;
char Subtraction = Subtraction;
char Addition = Addition;
char Multiplication = Multiplication;
char Division = Division;
char answer = answer;
labelA:
cout << "What basic mathematical operator shall I perform ?";
cout << "Please answer with: Division, Multiplication, Addition, Subtraction or Done.";
cin >> answer;
cin.get();
if (answer == Division); //Division section of the calculator
{
//Declaring the variables for use in the division function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be divided
cout << "Input value to be divided: ";
cin >> value1;
cin.get();
cout << "Input the dividend: ";
cin >> value2;
cin.get();
//dividing the values and outputting the answer
value1 /= value2;
cout << value1;
cin.get();
goto labelA;
}
if (answer == Multiplication); //Multiplication section of the calculator
{
//Declaring the variables for use in the multiplication function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be multiplied
cout << "Input value to be first number: ";
cin >> value1;
cin.get();
cout << "Input the second number: ";
cin >> value2;
cin.get();
//multiplying the values and outputting the answer
value1 *= value2;
cout << value1;
cin.get();
goto labelA;
}
if (answer == Addition); //Addition section of the calculator
{
//Declaring the variables for use in the addition function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be added
cout << "Input the first value : ";
cin >> value1;
cin.get();
cout << "Input the second value: ";
cin >> value2;
cin.get();
//subtracting the values and outputting the answer
value1 += value2;
cout << value1;
cin.get();
goto labelA;
}
if (answer == Subtraction); //Subtraction section of the calculator
{
//Declaring the variables for use in the subtraction function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be subtracted
cout << "Input value to be subtracted from: ";
cin >> value1;
cin.get();
cout << "Input the number to subtract from the first: ";
cin >> value2;
cin.get();
//subtracted the values and outputting the answer
value1 -= value2;
cout << value1;
cin.get();
goto labelA;
}
if (answer == Done); //Function to end the program
{cout << "Good bye";
return 0;
}
The goto statement simply goes to the label listed after it. Or at least its supposed to. I use it because that way I can simply goto the operator requested by the user, I would recommend using a loop instead as the goto statement can seriously hurt your programs logic but I cant wrap my brain around how I would do that here.
// Draft 1 - Simple Text Based Caculator
// NEEDS SOMETHING TO PAUSE THE PROGRAM AFTER EACH OUTPUT!!!
//Include Files
#include <iostream>
#include <string>
//Shortcuts to avoid having to type std::cout/cin everytime
using std::endl;
using std::cout;
using std::cin;
usingnamespace std;
//Beginning of actual calculator
int main()
{
string Done = Done;
string Subtraction = Subtraction;
string Addition = Addition;
string Multiplication = Multiplication;
string Division = Division;
string answer = answer;
labelA:
cout << "What basic mathematical operator shall I perform ?" << endl;
cout << "Please answer with: Division, Multiplication, Addition, Subtraction or Done." << endl;
cin >> answer;
cin.get();
if (answer == Division); //Division section of the calculator
{
//Declaring the variables for use in the division function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
longdouble answer1;
//asking for and storing the values to be divided
cout << "Input value to be divided: " << endl;
cin >> value1;
cin.get();
cout << "Input the dividend: " << endl;
cin >> value2;
cin.get();
//dividing the values and outputting the answer
value1 /= value2;
cout << value1 << endl;
goto labelA;
}
if (answer == Multiplication); //Multiplication section of the calculator
{
//Declaring the variables for use in the multiplication function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be multiplied
cout << "Input value to be first number: " << endl;
cin >> value1;
cin.get();
cout << "Input the second number: " << endl;
cin >> value2;
cin.get();
//multiplying the values and outputting the answer
value1 *= value2;
cout << value1 << endl;
goto labelA;
}
if (answer == Addition); //Addition section of the calculator
{
//Declaring the variables for use in the addition function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be added
cout << "Input the first value : " << endl;
cin >> value1;
cin.get();
cout << "Input the second value: " << endl;
cin >> value2;
cin.get();
//adding the values and outputting the answer
value1 += value2;
cout << value1 << endl;
goto labelA;
}
if (answer == Subtraction); //Subtraction section of the calculator
{
//Declaring the variables for use in the subtraction function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be subtracted
cout << "Input value to be subtracted from: " << endl;
cin >> value1;
cin.get();
cout << "Input the number to subtract from the first: " << endl;
cin >> value2;
cin.get();
//subtracting the values and outputting the answer
value1 -= value2;
cout << value1 << endl;
goto labelA;
}
if (answer == Done);
cout << "Good bye";
cin.get();
return 0;
}
Update: Program now pauses but follows a linear path regardless of user input.
[code]
// Draft 1 - Simple Text Based Caculator
//Program will go through
//Include Files
#include <iostream>
#include <string>
//Shortcuts to avoid having to type std::cout/cin/endl everytime
using std::endl;
using std::cout;
using std::cin;
using namespace std;
//Beginning of actual calculator
int main()
{
int Done = 5;
int Subtraction = 4;
int Addition = 3;
int Multiplication = 2;
int Division = 1;
int answer = 5;
cout << "What basic mathematical operator shall I perform ?" << endl;
cout << "Please answer with: Division(1), Multiplication(2), Addition(3), Subtraction(4) or Done(5)." << endl;
cin >> answer;
// Program will go through a linear path regardless of user input at the beginning.
if (answer == 1); //Division section of the calculator
{
//Declaring the variables for use in the division function
//They can be used again(in a different sub function)
// because they are declared in a sub function
long double value1;
long double value2;
long double answer1;
//asking for and storing the values to be divided
cout << "Input value to be divided: " << endl;
cin >> value1;
cout << "Input the dividend: " << endl;
cin >> value2;
//dividing the values and outputting the answer
value1 /= value2;
cout << value1 << endl;
}
if (answer == 2); //Multiplication section of the calculator
{
//Declaring the variables for use in the multiplication function
//They can be used again(in a different sub function)
// because they are declared in a sub function
long double value1;
long double value2;
//asking for and storing the values to be multiplied
cout << "Input value to be first number: " << endl;
cin >> value1;
cout << "Input the second number: " << endl;
cin >> value2;
//multiplying the values and outputting the answer
value1 *= value2;
cout << value1 << endl;
}
if (answer == 3); //Addition section of the calculator
{
//Declaring the variables for use in the addition function
//They can be used again(in a different sub function)
// because they are declared in a sub function
long double value1;
long double value2;
//asking for and storing the values to be added
cout << "Input the first value : " << endl;
cin >> value1;
cout << "Input the second value: " << endl;
cin >> value2;
//adding the values and outputting the answer
value1 += value2;
cout << value1 << endl;
}
if (answer == 4); //Subtraction section of the calculator
{
//Declaring the variables for use in the subtraction function
//They can be used again(in a different sub function)
// because they are declared in a sub function
long double value1;
long double value2;
//asking for and storing the values to be subtracted
cout << "Input value to be subtracted from: " << endl;
cin >> value1;
cout << "Input the number to subtract from the first: " << endl;
cin >> value2;
//subtracting the values and outputting the answer
// Draft 1 - Simple Text Based Caculator
//Program ends after getting user input the second time.
//Include Files
#include <iostream>
#include <string>
#include <cmath>
//Shortcuts to avoid having to type std::cout/cin/endl everytime
using std::endl;
using std::cout;
using std::cin;
usingnamespace std;
//Beginning of actual calculator
int main()
{
int Done = 5;
int Subtraction = 4;
int Addition = 3;
int Multiplication = 2;
int Division = 1;
int answer = 5;
cout << "What basic mathematical operator shall I perform ?" << endl;
cout << "Please answer with: Division(1), Multiplication(2), Addition(3), Subtraction(4) or Done(5)." << endl;
cin >> answer;
// Program will go through a linear path regardless of user input at the beginning.
if (answer == 1) //Division section of the calculator
{
//Declaring the variables for use in the division function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be divided
cout << "Input value to be divided: " << endl;
cin >> value1;
cout << "Input the dividend: " << endl;
cin >> value2;
//dividing the values and outputting the answer
value1 /= value2;
cout << value1 << endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 2) //Multiplication section of the calculator
{
//Declaring the variables for use in the multiplication function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be multiplied
cout << "Input value to be first number: " << endl;
cin >> value1;
cout << "Input the second number: " << endl;
cin >> value2;
//multiplying the values and outputting the answer
value1 *= value2;
cout << value1 << endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 3) //Addition section of the calculator
{
//Declaring the variables for use in the addition function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be added
cout << "Input the first value : " << endl;
cin >> value1;
cout << "Input the second value: " << endl;
cin >> value2;
//adding the values and outputting the answer
value1 += value2;
cout << value1 << endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer == 4) //Subtraction section of the calculator
{
//Declaring the variables for use in the subtraction function
//They can be used again(in a different sub function)
// because they are declared in a sub function
longdouble value1;
longdouble value2;
//asking for and storing the values to be subtracted
cout << "Input value to be subtracted from: " << endl;
cin >> value1;
cout << "Input the number to subtract from the first: " << endl;
cin >> value2;
//subtracting the values and outputting the answer
value1 -= value2;
cout << value1 << endl;
cout << "Please input the next operation number you would like to preform." << endl;
cin >> answer;
}
if (answer = 5)
cout << "Good bye";
return 0;
}