C++ help. I have found 2 errors but need help finding 3 more. // Overloading.cpp
ID: 3849456 • Letter: C
Question
C++ help. I have found 2 errors but need help finding 3 more.
// Overloading.cpp : This code contains five errors before it will work as desired. Find those errors,
// document a description of the errors and their fix, and fix them. Try using the debugger to
// step through the program to find the bugs. As you step through, take notice of the information
// you can see.
//
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int add(int, int);
double add(double, double);
int main()
{
int a, b, x;
float c, d, y;
cout << "Enter two integers ";
cin >> a >> b;
x = add(a, c);//error
cout << "Sum of integers: " << x << endl;
cout << "Enter two doubles ";
cin >> c >> d;
y = add(a, b);
cout << "Sum of doubles: " << y << endl;
return 0;
}
int add(int a, int b)
{
int sum;
sum = a + b;
}
double add(double a, double b)
{
double sum;
sum = a + b;
return sum
}//error
//debugger has shown me 2 of them. The error code is:
Severity Code Description Project File Line Suppression State
Error (active) E0308 more than one instance of overloaded function "add" matches the argument list: IT312_Overloading c: #### 23
Error (active) E0065 expected a ';' IT312_Overloading c:#### 45
Explanation / Answer
The following C++ code with comments list out the errors present in the given code:
//#include "stdafx.h" //error 1
#include <cstdlib>
#include <iostream>
using namespace std;
int add(int, int);
double add(double, double);
int main()
{
int a, b, x;
float c, d, y;
cout << "Enter two integers ";
cin >> a >> b;
x = add(a, c);//error: c can't be send before initialization and c is initialized as float as well
cout << "Sum of integers: " << x << endl;
cout << "Enter two doubles ";
cin >> c >> d;
y = add(a, b); //error: it should be c and d rather than a and b
cout << "Sum of doubles: " << y << endl;
return 0;
}
int add(int a, int b)
{
int sum;
sum = a + b; //error: this function should return sum
}
double add(double a, double b)
{
double sum;
sum = a + b;
return sum //error: termination ';' is required
}//error
The following code is error free, and it is performing the required operation:
//#include "stdafx.h" //error 1
#include <cstdlib>
#include <iostream>
using namespace std;
int add(int, int);
double add(double, double);
int main()
{
int a, b, x;
float c, d, y;
cout << "Enter two integers ";
cin >> a >> b;
x = add(a, b);//error: c can't be send before initialization and c is initialized as float as well
cout << "Sum of integers: " << x << endl;
cout << "Enter two doubles ";
cin >> c >> d;
y = add(c, d); //error: it should be c and d rather than a and b
cout << "Sum of doubles: " << y << endl;
return 0;
}
int add(int a, int b)
{
int sum;
sum = a + b; //error: this function should return sum
return sum;
}
double add(double a, double b)
{
double sum;
sum = a + b;
return sum; //error: termination ';' is required
}//error
Input:
1 2
1.2 2.3
Output:
Enter two integers
Sum of integers: 3
Enter two doubles
Sum of doubles: 3.5
Code's link: http://ideone.com/WGPDY6
Hope it helps, feels free to comment in case of any query.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.