I got this and i don\'t know what is wrong with my code. BELOW IS THE CODE I TRI
ID: 3840179 • Letter: I
Question
I got this and i don't know what is wrong with my code.
BELOW IS THE CODE I TRIED. PLEASE FIX IT CORRECTLY.
#include <iostream>
using namespace std;
int add(int a, int b){
return a+b;
}
int subtract(int a, int b){
return a-b;
}
int main() {
int num1 = 0;
int num2 = 0;
int sum = 0; //sum of numbers
int diff = 0; //difference of numbers
cout << "Enter the first integer: ";
cin >> num1;
cout << "Enter the second integer: " << endl;
cin >> num2;
cout << endl;
cout << "First Integer: " << num1 << endl;
cout << "Second Integer: " << num2 << endl;
cout << endl;
// Call add function
sum = add(num1, num2);
// FIXME (6) Output result of num1 + num2
cout << num1 << " + " << num2 << " = " << sum << endl;
return 0;
}
Explanation / Answer
You didn't call the subtract function in main. You can try with the below code.
#include <iostream>
using namespace std;
int add(int a, int b){
return a+b;
}
int subtract(int a, int b){
return a-b;
}
int main() {
int num1 = 0;
int num2 = 0;
int sum = 0; //sum of numbers
int diff = 0; //difference of numbers
cout << "Enter the first integer: ";
cin >> num1;
cout << "Enter the second integer: " << endl;
cin >> num2;
cout << endl;
cout << "First Integer: " << num1 << endl;
cout << "Second Integer: " << num2 << endl;
cout << endl;
// Call add function
sum = add(num1, num2);
// FIXME (6) Output result of num1 + num2
cout << num1 << " + " << num2 << " = " << sum << endl;
// Call subtract function
diff = subtract(num1, num2);
// Output result of num1 - num2
cout << num1 << " - " << num2 << " = " << diff << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.