Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Follow the instructions for starting C++ and viewing the SwatTheBugs25.cpp file,

ID: 3808514 • Letter: F

Question

Follow the instructions for starting C++ and viewing the SwatTheBugs25.cpp file, which is contained in either the Cpp8Chap10SwatTheBugs25 Project folder or the Cpp8Chap10 folder. (Depending on your C++ development tool, you may need to open this exercise’s project/solution file first.) The program should calculate and display the sum of two numbers, but it is not working correctly. Debug the program Ok so I worked the problem and this is what I came up with but when I run the code it will still not add the numbers, what am I missing. I need to use & and not *, so with that in mind what is my error that does not allow the function to calculate. //SwatTheBugs26.cpp - displays the sum of two numbers //Created/revised by on #include using namespace std; //function prototypes void getNumber(int num); void calcSum(int n1, int n2, int &n3); int main() { int num1 = 0; int num2 = 0; int num3 = 0; //enter input items getNumber(num1); getNumber(num2); //calculate and display sum calcSum(num1, num2, num3); cout << "Sum: " << num3 << endl; system("pause"); return 0; } //end of main function //*****function definitions***** void getNumber(int num) { cout << "Enter a number: "; cin >> num; } //end of getNumber function void calcSum(int n1, int n2, int &n3) { n3 = n1 + n2; } //end of calcSum function

Explanation / Answer

Hi

I have fixed the issue and highlighted the code changes below,

#include <iostream>

using namespace std;
//function prototypes
void getNumber(int &num);
void calcSum(int n1, int n2, int &n3);
int main() {
int num1 = 0;
int num2 = 0;
int num3 = 0;
//enter input items
getNumber(num1);
getNumber(num2);
//calculate and display sum
calcSum(num1, num2, num3);
cout << "Sum: " << num3 << endl;
//system("pause");
return 0;
} //end of main function
//*****function definitions*****
void getNumber(int &num) {
cout << "Enter a number: ";
cin >> num;
  
} //end of getNumber function
void calcSum(int n1, int n2, int &n3) {
n3 = n1 + n2;
  
} //end of calcSum function

Output:

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter a number: 2                                                                                                                                                                                                                                                      

Enter a number: 3                                                                                                                                                                                                                                                      

Sum: 5