Simple Calculator - printing the result
Jan 21, 2014 at 10:15am UTC
Hello, i'm making a simple calculator and have done it all right where you can input everything, all the functions are there, but when i run the program it will come to displaying the result and it will always equal zero, any help on this would be appreciated, i just need it to say 8+8 = 16 rather than 8+8 = 0, i don't know whether its just displaying the results as 0, or not displaying it at all, the code will follow below, thank you!
Also any tips on how i could possibly make the calculator better would be helpful, although its for my assignment so i have to use functions!
code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include<iostream>
using namespace std;
double num3;
double num2;
double result;
char operation;
char a;
int repeat;
double addition()
{
result = num3 + num2;
return (result);
}
double subtraction()
{
result = num3 - num2;
return (result);
}
double division()
{
result = num3 / num2;
return (result);
}
double multiplication()
{
result = num3 * num2;
return (result);
}
int main()
{
cout << "Please enter a number: " ;
cin >> num3;
cout << "Please enter another number: " ;
cin >> num2;
cout << "\n\n\n" ;
cout << "Select the desired function:\n" ;
cout << "+ Addition\n" ;
cout << "- Subtraction\n" ;
cout << "* Multiplication\n" ;
cout << "/ Division\n" ;
cout << "Selection: " ;
cin >> operation;
cout << "\n\n\n" ;
switch (operation)
{
case '+' :
cout << "The sum of " << num3 << "and " << num2 << "equals" << addition << endl;
break ;
case '-' :
cout << "The difference of " << num3 << " and " << num2 << " is " << subtraction << endl;
break ;
case '*' :
cout << "The product of " << num3 << " and " << num2 << " is " << multiplication << endl;
break ;
case '/' :
cout << "The quotient of " << num3 << " and " << num2 << " is " << division << endl;
break ;
default :
cout << "Invalid selection\n" ;
}
system ("pause" );
return 0;
}
Jan 21, 2014 at 10:40am UTC
You need to go back to your textbook and re-read how to call a function. Nowhere in your code are you actually calling the functions addition, subtraction etc.
I'm very surprised it's showing 0 as the result, though.
Jan 21, 2014 at 10:54am UTC
The reason you result is zero is because you're not calling your functions. I also suggest that you avoid declaring your variables with global scope, but instead keep them local.
I have revised your code, and added
(1) a do ... while loop so that your program will keep running until the user tells it to quit
(2) added a function which will check that the user has entered a valid number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#include<iostream>
#include <conio.h>
using namespace std;
int get_number(char * prompt)
{
cout << "\n" << prompt;
int number(0);
cin >> number;
while (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
cout << "\nYou didn't enter a number. " << prompt;
cin >> number;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
return number;
}
double addition(int num1, int num2)
{
return num1 + num2;
}
double subtraction(int num1, int num2)
{
return num1 - num2;
}
double division(int num1, int num2)
{
return num1 / num2;
}
double multiplication(int num1, int num2)
{
return num1 * num2;
}
int main()
{
bool running(true );
do
{
char operation;
cout << "\n\n\n" ;
cout << "Select the desired function:\n" ;
cout << "+ Addition\n" ;
cout << "- Subtraction\n" ;
cout << "* Multiplication\n" ;
cout << "/ Division\n" ;
cout << "q Quit\n" ;
cout << "Selection: " ;
cin >> operation;
cout << "\n\n\n" ;
running = operation != 'q' && operation != 'Q' ? true : false ;
if (running)
{
int num1 = get_number("Please enter a number: " );
int num2 = get_number("Please enter another number: " );
cout << endl;
switch (operation)
{
case '+' :
cout << "The sum of " << num1 << "and " << num2 << " equals " << addition(num1, num2) << endl;
break ;
case '-' :
cout << "The difference of " << num1 << " and " << num2 << " is " << subtraction(num1, num2) << endl;
break ;
case '*' :
cout << "The product of " << num1 << " and " << num2 << " is " << multiplication(num1, num2) << endl;
break ;
case '/' :
cout << "The quotient of " << num1 << " and " << num2 << " is " << division(num1, num2) << endl;
break ;
default :
cout << "Invalid selection\n" ;
}
}
} while (running);
cout << "pause" ;
getch();
return 0;
}
Jan 21, 2014 at 11:51am UTC
Also, rather than having specific function to deal with your arithmetic operations, you could write a class to encapsulate these operations on your class instance. This is then more natural and is how you were trying to code the solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#include<iostream>
#include <conio.h>
using namespace std;
int get_number(char * prompt)
{
cout << "\n" << prompt;
int number(0);
cin >> number;
while (!cin.good())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
cout << "\nYou didn't enter a number. " << prompt;
cin >> number;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
return number;
}
class Calculator
{
public :
Calculator(int num)
: number(num)
{
}
friend std::ostream& operator << (std::ostream& os, const Calculator& c)
{
os << c.number;
return os;
}
friend Calculator operator + (Calculator lhs, const Calculator& rhs)
{
lhs += rhs;
return lhs;
}
friend Calculator operator - (Calculator lhs, const Calculator& rhs)
{
lhs -= rhs;
return lhs;
}
friend Calculator operator * (Calculator lhs, const Calculator& rhs)
{
lhs *= rhs;
return lhs;
}
friend Calculator operator / (Calculator lhs, const Calculator& rhs)
{
lhs /= rhs;
return lhs;
}
Calculator& operator += (const Calculator& rhs)
{
number += rhs.number;
return *this ;
}
Calculator& operator -= (const Calculator& rhs)
{
number -= rhs.number;
return *this ;
}
Calculator& operator *= (const Calculator& rhs)
{
number *= rhs.number;
return *this ;
}
Calculator& operator /= (const Calculator& rhs)
{
number /= rhs.number;
return *this ;
}
private :
int number;
};
int main()
{
bool running(true );
do
{
char operation;
cout << "\n\n\n" ;
cout << "Select the desired function:\n" ;
cout << "+ Addition\n" ;
cout << "- Subtraction\n" ;
cout << "* Multiplication\n" ;
cout << "/ Division\n" ;
cout << "q Quit\n" ;
cout << "Selection: " ;
cin >> operation;
cout << "\n\n\n" ;
running = operation != 'q' && operation != 'Q' ? true : false ;
if (running)
{
int num1 = get_number("Please enter a number: " );
int num2 = get_number("Please enter another number: " );
Calculator c1(num1), c2(num2);
cout << endl;
switch (operation)
{
case '+' :
cout << "The sum of " << num1 << " and " << num2 << " equals " << c1 + c2 << endl;
break ;
case '-' :
cout << "The difference of " << num1 << " and " << num2 << " is " << c1 - c2 << endl;
break ;
case '*' :
cout << "The product of " << num1 << " and " << num2 << " is " << c1 * c2 << endl;
break ;
case '/' :
cout << "The quotient of " << num1 << " and " << num2 << " is " << c1 / c2 << endl;
break ;
default :
cout << "Invalid selection\n" ;
}
}
} while (running);
cout << "pause" ;
getch();
return 0;
}
Last edited on Jan 21, 2014 at 11:58am UTC
Jan 22, 2014 at 10:43am UTC
thank you guys! didn't expect help so quickly! ajh32: although your code looks complicated i can make sense and it really helps so thanks! although wee haven't learnt classes in our lectures yet so i'm unable to do that.
Topic archived. No new replies allowed.