Hello everyone.. spent a few hours studying and some time writing this code for a calculator.. I am trying to do the addition portion (sum) of the project first before I continue. I am stuck so far with this. Please help!
#include <iostream>
using namespace std;
int main () {
int iVal1, iVal2;
char chOperator[] = { '+', '-','/', '*' };
int sum = iVal1 + iVal2
cout << "Enter an integer:";
cin >> iVal1;
cout << "Enter another integer:";
cin >> iVal2;
cout << "Enter the operation you want to perform:";
cin >> chOperator;
if ( chOperator == + );
cout << "The result is " << sum << endl;
return 0;
}
Ok this is what I have..its still wrong but I feel closer.. this assignment is unique as i must use char and if / else statements.
Thanks for the help
#include <iostream>
using namespace std;
int sum (int iValue1, int iValue2) {
int answer = iValue1 + iValue2;
return answer;
}
int subtraction (int iValue1, int iValue2) {
int answer = iValue1 - iValue2;
return answer;
}
int multiply (int iValue1, int iValue2) {
int answer = iValue1 * iValue2;
return answer;
}
int division (int iValue1, int iValue2) {
int answer = iValue1 / iValue2;
return answer;
}
int main () {
int iValue1;
int iValue2;
char chOperator
cout << "Enter an integer:";
cin >> iValue1;
cout << "Enter another integer:";
cin >> iValue2;
cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;
if (chOperator == +);
cout << "The result is " << sum << endl;
else if (chOperator == -);
cout << "The result is " << subtraction << endl;
else if (chOperator == *);
cout << "The result is " << multiply << endl;
else if (chOperator == /);
cout << "The result is " << division << endl;
if (chOperator == +); You are forgetting single quotes: if (chOperator == '+'); (for all operators of course).
But most importantly, you call your functions without parameters: cout << "The result is " << sum << endl; instead of: cout << "The result is " << sum(iValue1, iValue2) << endl;
I edited it based on what you all said.. its not working but I feel closer.
Thanks for the help:
#include <iostream>
using namespace std;
int sum () {
int iValue1, iValue2;
return iValue1 + iValue2;
}
int subtraction () {
int iValue1, iValue2;
return iValue1 - iValue2;
}
int multiply () {
int iValue1, iValue2;
return iValue1 * iValue2;
}
int division () {
int iValue1, iValue2;
return iValue1 / iValue2;
}
int main () {
int iValue1;
int iValue2;
char chOperator
cout << "Enter an integer:";
cin >> iValue1;
cout << "Enter another integer:";
cin >> iValue2;
cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;
if (chOperator == '+')
cout << "The result is " << sum << endl;
else if (chOperator == '-')
cout << "The result is " << subtraction << endl;
else if (chOperator == '*')
cout << "The result is " << multiply << endl;
else if (chOperator == '/')
cout << "The result is " << division << endl;
What about that semicolon here: char chOperator? ;P
Also, for practice, you should make the calculator detect mistakes when entering the operation. For example, what if the user accidentally types in the letter "a"?
EDIT: Also take a look at the second part of JockX's post, I think you forgot to add that.
Tweaked it a bit based on JockX but more issues :/
#include <iostream>
using namespace std;
int sum () {
int iValue1, iValue2;
return iValue1 + iValue2;
}
int subtraction () {
int iValue1, iValue2;
return iValue1 - iValue2;
}
int multiply () {
int iValue1, iValue2;
return iValue1 * iValue2;
}
int division () {
int iValue1, iValue2;
return iValue1 / iValue2;
}
int main () {
int iValue1;
int iValue2;
char chOperator;
cout << "Enter an integer:";
cin >> iValue1;
cout << "Enter another integer:";
cin >> iValue2;
cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;
if (chOperator == '+')
cout << "The result is " << sum(iValue1, iValue2) << endl;
else if (chOperator == '-')
cout << "The result is " << subtraction(iValue1, iValue2) << endl;
else if (chOperator == '*')
cout << "The result is " << multiply(iValue1, iValue2) << endl;
else if (chOperator == '/')
cout << "The result is " << division(iValue1, iValue2) << endl;
Lookup again how functions work you are passing parameters in your function calls but your function definitions dont have any parameters.
1 2 3 4
int sum () {
int iValue1, iValue2;
return iValue1 + iValue2;
}
notice how the sum() function is defined with no parameters.
Now in main you are calling it with parameters in main sum(iValue1, iValue2).
So the sum function has no idea what to do with iValue1 and iValue2.
So try changing it to this
1 2 3
int sum (int value1, int value2) {
return value1 + value2;
}
Now when you call sum(iValue1, iValue2) it will add whatever variables you pass to it (iValue1, and iValue2) together and return the sum.
Also please use codetags when you post code in the forums (Hint: Highlight all your code and then press the <> button off to the right when replying) it just makes it a lot easier for us to read.
Ok guys.. I went back and read everything. It all makes sense. Wow what an evolution for me from week 1 to week 3 now.
Last part I am stuck with..
I am trying to make the calculator continue to work, based on the last math done, until it reaches 0. But there is an issue. You'll see the function works fine at first, but when the do { kicks in and it starts working on the new value.. it fails.
#include <iostream>
usingnamespace std;
int sum(int iValue1, int iValue2) {
return iValue1 + iValue2;
}
int subtraction (int iValue1, int iValue2) {
return iValue1 - iValue2;
}
int multiply (int iValue1, int iValue2) {
return iValue1 * iValue2;
}
int division (int iValue1, int iValue2) {
return iValue1 / iValue2;
}
int main () {
int iValue1;
int iValue2;
char chOperator;
cout << "Enter the initial value as an integer:";
cin >> iValue1;
do {
cout << "Enter the next value as an integer:";
cin >> iValue2;
cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;
if (chOperator == '+')
cout << "The result is " << sum(iValue1, iValue2) << endl;
elseif (chOperator == '-')
cout << "The result is " << subtraction(iValue1, iValue2) << endl;
elseif (chOperator == '*')
cout << "The result is " << multiply(iValue1, iValue2) << endl;
elseif (chOperator == '/')
cout << "The result is " << division(iValue1, iValue2) << endl;
iValue1 = iValue1 << chOperator << iValue2;
} while (iValue1 !=0);
return 0;
}
I removed it earlier and the math came out all wrong.
It am trying to get my calculator to do something like this:
Enter the initial value: 5
Enter the next value : 7
Enter the math you want to perform: +
The result is 12
Enter the next value: 3
Enter the math you want to perform: -
The result is 9
Enter the next value : 10
Enter the math you want to perform: +
The result is 19
Enter the next value: 19
Enter the math you want to perform: -
The result is 0
#include <iostream>
usingnamespace std;
int sum(int iValue1, int iValue2) {
return iValue1 + iValue2;
}
int subtraction (int iValue1, int iValue2) {
return iValue1 - iValue2;
}
int multiply (int iValue1, int iValue2) {
return iValue1 * iValue2;
}
int division (int iValue1, int iValue2) {
return iValue1 / iValue2;
}
int main () {
int iValue1;
int iValue2;
char chOperator;
cout << "Enter the initial value as an integer:";
cin >> iValue1;
do {
cout << "Enter the next value as an integer:";
cin >> iValue2;
cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;
if (chOperator == '+')
cout << "The result is " << sum(iValue1, iValue2) << endl;
elseif (chOperator == '-')
cout << "The result is " << subtraction(iValue1, iValue2) << endl;
elseif (chOperator == '*')
cout << "The result is " << multiply(iValue1, iValue2) << endl;
elseif (chOperator == '/')
cout << "The result is " << division(iValue1, iValue2) << endl;
} while (iValue1 !=0);
return 0;
}
cout << "Enter the initial value as an integer:";
cin >> iValue1;
do {
cout << "Enter the next value as an integer:";
cin >> iValue2;
cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;
if (chOperator == '+'){
cout << "The result is " << sum(iValue1, iValue2) << endl;
iValue1 = sum(iValue1, iValue2);
}
elseif (chOperator == '-'){
cout << "The result is " << subtraction(iValue1, iValue2) << endl;
iValue1 = subtraction(iValue1, iValue2);
}
elseif (chOperator == '*'){
cout << "The result is " << multiply(iValue1, iValue2) << endl;
iValue1 = multiply(iValue1, iValue2);
}
elseif (chOperator == '/'){
cout << "The result is " << division(iValue1, iValue2) << endl;
iValue1 = division(iValue1, iValue2);
}
} while (iValue1 !=0);
return 0;
This "updates" the value of iValue1 every time so that what you wanted was possible.
Ok everyone.. trying to wrap this and make it real nice.
I put parameters to not allow for characters.. but formatting goes out of whack. Try putting a letter as an integer. Need help with getting it to look clean.
Also, I also put parameters so it cannot divide by 0 based on earlier feedback. It says enter a new integer then it crashes!
#include <iostream>
usingnamespace std;
int sum(int iValue1, int iValue2) {
return iValue1 + iValue2;
}
int subtraction (int iValue1, int iValue2) {
return iValue1 - iValue2;
}
int multiply (int iValue1, int iValue2) {
return iValue1 * iValue2;
}
int division (int iValue1, int iValue2) {
return iValue1 / iValue2;
}
int main () {
int iValue1;
int iValue2;
char chOperator;
cout << "Enter the initial value as an integer:";
cin >> iValue1;
do {
cout << "Enter the next value as an integer:";
cin >> iValue2;
if (cin.fail())
cout << "No integer found. Please use a number" << endl;
cin.clear();
cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;
if (chOperator == '+') {
cout << "The result is " << sum(iValue1, iValue2) << endl;
iValue1 = sum(iValue1, iValue2);
}
elseif (chOperator == '-') {
cout << "The result is " << subtraction(iValue1, iValue2) << endl;
iValue1 = subtraction(iValue1, iValue2);
}
elseif (chOperator == '*') {
cout << "The result is " << multiply(iValue1, iValue2) << endl;
iValue1 = multiply(iValue1, iValue2);
}
elseif (chOperator == '/') {
if (iValue2 == 0)
cout << "You cannot divide by 0. Enter another integer ";
cout << "The result is " << division(iValue1, iValue2) << endl;
iValue1 = division(iValue1, iValue2);
}
} while (iValue1 !=0);
return 0;
}
In line 55 although you are checking the value of iValue2 you are doing nothing to rectify it if indeed it is 0. So it will just proceed to the next line and continue with the division. Better is the following:
1 2 3 4 5
while (iValue2 == 0)
{
cout << "You cannot divide by 0. Enter another integer ";
cin>>iValue2;
}