I am getting an error when trying to run this code
In function 'int main()':
error: 'area' was not declared in this scope
I cannot find a clear solution to the problem.
#include <iostream>
using namespace std;
int main() {
area(13.3, 67.4);
return 0;
}
void area(int a, int b){
cout << "The area is " << a * b << endl;
}
void area(float a, float b){
cout << "The area is " << a * b << endl;
}
void area(double a, double b){
cout << "The area is " << a * b << endl;
}
You need to declare the functions before you can use them.
Either forward declare them:
#include <iostream>
using namespace std;
// forward declarations
void area(int a, int b);
void area(float a, float b);
void area(double a, double b);
int main() {
area(13.3, 67.4);
return 0;
}
void area(int a, int b){
cout << "The area is " << a * b << endl;
}
void area(float a, float b){
cout << "The area is " << a * b << endl;
}
void area(double a, double b){
cout << "The area is " << a * b << endl;
}
Otherwise, move the implementations above main():
#include <iostream>
using namespace std;
void area(int a, int b){
cout << "The area is " << a * b << endl;
}
void area(float a, float b){
cout << "The area is " << a * b << endl;
}
void area(double a, double b){
cout << "The area is " << a * b << endl;
}
int main() {
area(13.3, 67.4);
return 0;
}
That being said, since the implementations are exactly the same, just with different data types, consider using a single templated function instead:
#include <iostream>
using namespace std;
template<typename T>
void area(T a, T b){
cout << "The area is " << a * b << endl;
}
int main() {
area<double>(13.3, 67.4);
return 0;
}
Put functions prototypes above your main function and you should be fine.
Related
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 2 months ago.
For some reason the value of divide() still prints as an int even though I declared it as a double.
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
class Calculator{
private:
int a = 0;
int b = 0;
public:
Calculator(int n1, int n2) : a(n1), b(n2) {}
Calculator() = default;
int getN1() const{
return a;
}
int getN2() const{
return b;
}
int add() const{
return a + b;
}
int subtract() const{
return a - b;
}
int multiply() const{
return a * b;
}
double divide() const{
return a / b;
}
void showDetails() const{
cout << " " << endl;
cout << "Computed:" << endl;
cout << "Sum = " << add() << endl;
cout << "Difference = " << subtract() << endl;
cout << "Product = " << multiply() << endl;
cout << "Quotient = " << setprecision(2) << fixed << divide() << endl;
}
};
int main() {
int n1;
int n2;
cout << "Enter Num1: ";
cin >> n1;
cout << "Enter Num2: ";
cin >> n2;
Calculator x(n1, n2);
x.showDetails();
return 0;
}
It worked when I declared the initial values as double but I was tasked to assign the initial values as int.
a and b are declare as integers. Make it double. int upon int gives int value hence change any one or both of them to double.
I am trying to make a function that returns double the integer number that I will pass to it. I am getting the following error message with my code:
declaration of 'int x' shadows a parameter int x; "
Here is my code:
#include <iostream>
int doublenumber();
using namespace std;
int doublenumber(int x)// <-- this is the function which returns double the value .
{
int x;
return 2 * x;
cout << endl;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin >> a;
doublenumber(a);
return 0;
}
You have x as a parameter and then try to declare it also as a local variable, which is what the complaint about "shadowing" refers to.
I did it because your advice was so helpful, and this is the final result :
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int n= doublenumber(a);
cout << "the double value is : " << n << endl;
return 0;
}
#include <iostream>
using namespace std;
int doublenumber(int x)
{
return 2*x;
}
int main()
{
int a;
cout << "Enter the number that you want to double it : " << endl;
cin>>a;
int d = doublenumber(a);
cout << "Double : " << d << endl;
return 0;
}
There are some problem with your code. Your declaration and definition of function dies not match. So remove declaration as no necessity of it.
You are declaring local x variable inside function which will shadow your function arguments.
In my program I have created a constructor called Point with two values. I also have a set, get, scale and translate function. I'm trying to create a function that allows me to get the distance between the object and another point. I'm have trouble with it though any help would be brilliant.
#ifndef POINTMODEL
#define POINTMODEL
#define POINTDEB UG
#include <iostream>
#include <string.h>
using namespace std;
class Point {
public:
Point(void);
Point(double anX, double aY);
~Point();
void setPoint(double anX, double aY);
double getX();
double getY();
double scaleX(double theX);
double scaleY(double theY);
void translate(double theX, double theY);
void distance(const Point& aPoint);
protected:
private:
double theX;
double theY;
};
inline Point::Point(void)
{
theX = 1;
theY = 1;
cout << "\n The default constructor was called" << endl;
}
inline Point::Point(double anX, double aY)
{
cout << "\n regular constructor called";
}
inline Point::~Point()
{
cout << "\n the destructor was called" << endl;
}
inline void Point::setPoint(double anX, double aY)
{
theX = anX;
theY = aY;
}
inline double Point::getX()
{
return theX;
}
inline double Point::getY()
{
return theY;
}
inline double Point::scaleX(double theX)
{
return theX;
}
inline double Point::scaleY(double theY)
{
return theY;
}
inline void Point::translate(double offSetX, double offSetY)
{
cout << "X is translated by : " << offSetX << endl;
cout << "Y is translated by : " << offSetY << endl;
}
inline void Point::distance(const Point& aPoint)
{
}
#endif
Cpp file:
#include "Point.h"
using namespace std;
int main(void)
{
cout << "\n main has started" << endl;
//Point myPoint;
Point myPoint(1, 1);
myPoint.setPoint(1, 1);
cout << "\n The value for X is : " << myPoint.getX() << endl;
cout << "\n The value for Y is : " << myPoint.getY() << endl;
cout << "\n X scaled by 2 is : " << myPoint.scaleX(2) << endl;
cout << "\n Y scaled by 2 is : " << myPoint.scaleY(2) << endl;
myPoint.translate(2, 3);
cout << "\n main has finished" << endl;
return 0;
}
You need to make your Point::getX() and Point::getY() functions const like so:
inline double Point::getX() const
{
return theX;
}
If they are not const you cannot call them when the parameter is a const reference.
Then the distance is (changed return from void to double):
double distance(const Point & aPoint) const
{
const double x_diff = getX() - aPoint.getX();
const double y_diff = getY() - aPoint.getY();
return std::sqrt(x_diff * x_diff + y_diff * y_diff);
}
I have deliberately not used std::pow since the exponent is 2.
You also need to include <cmath> for std::sqrt.
#include <iostream>
using namespace std;
void swap(int, int);
int main()
{
int a=10;
int b=20;
swap (a, b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
void swap(int x, int y)
{
int t;
t = x;
x = y;
y = t;
}
those code above can't swap the value of a and b.
but my question is , when I forgot to type the third line "void swap(int, int);
" , the values of a and b swaped !! why?
It's because you have
using namespace std;
At the beginning of your source code.
This is a a bad programming practice, whose consequences you just experienced, first hand. You told the compiler that you want to invoke std::swap, without having any clue that you actually did that.
It's ironical, because you version of swap() won't work right, but std::swap does; so you were operating under the mistaken impression that your code was working, when it didn't.
Never use "using namespace std;" with your code. Simply forget that this part of the C++ language ever existed.
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
system("pause");
swap(a, b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
system("pause");
return 0;
}
void swap is unnecessary
If you put the function definition above main then you don't need a prototype otherwise you do need it and the compiler should give you an error if you don't have a prototype
I'm trying to find the output of the following code.
How can i add a return statement to magicTime?
The out put should be
a: 10 b: 30 c:
a: 10 b: 30
#include <iostream>
using namespace std;
int magicTime(int a, int &b, const int &c){
a=c;
b=20;
}
int main(){
int a = 10;
int b = 30;
int c;
cout << "a: " << a << " b " << b << " c " << c << endl;
c=b;
magicTime(c, b, a);
cout << "a: " << a << " b " << b << " c " << c << endl;
return 0 ;
}
OK so you want to return an array of int
Pseudo code
int[] magicTime(int a, int &b, const int &c){
a=c;
b=20;
return new int [] { a, b, c};
}