Right so I thought hey lets make a calculator program to practice my function noobie skills. This is my code so far, and whenever I go to subtracting(), the answer is always 0?!?!
#include <iostream>
usingnamespace std;
int subtractionOne;
int subtractionTwo;
int subtractionTotal;
int subtraction() {
int subtractionTotal = subtractionOne + subtractionTwo;
cin >> subtractionOne >> subtractionTwo;
cout << subtractionTotal;
return subtractionTotal;
}
int main () {
int choose;
system ("clear");
cout << "Press one for adding, two for subtracting.";
cin >> choose;
if(choose == 1) {
subtraction();
}
return 0;
}
#include <iostream>
usingnamespace std;
int subtractionOne;
int subtractionTwo;
int subtractionTotal;
int subtraction() {
cin >> subtractionOne >> subtractionTwo;
int subtractionTotal = subtractionOne + subtractionTwo;
cout << subtractionTotal;
return subtractionTotal;
}
int main () {
int choose;
system ("clear");
cout << "Press one for adding, two for subtracting.";
cin >> choose;
if(choose == 1) {
subtraction();
}
return 0;
}
needs to be rearranged. The value of subtractionTotal will always end up being zero because at the time you are assigning the value of subtractionOne + subtractionTwo, the user has not input any values, you are then attempting to output that 0 value. In your main function you prompt for the user to enter 1 for adding, two for subtracting, but your subtraction function only executes if you select to add 1, and the function doesn't actually subtract... it adds....