CPP program displays shadows the perimeter error [duplicate] - c++

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.

Related

How to print double with an initially declared int value in C++ [duplicate]

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.

c++ reference to a class member but not changing value

Please help me to review the following code.
I am wondering why the variable "b" is not the modified value.
I can not change the value using reference ?
Thanks!
#include <iostream>
using namespace std;
class Foo{
public:
int a = 1;
int& check(){
return a;
};
};
int main()
{
int b;
Foo foo;
b = foo.check();
cout << b << endl;
foo.check() = 2;
cout << foo.a << endl;
cout << b << endl;
return 0;
}
The output is
1
2
1
As #Igor Tandetnik indicated, foo.check returns a reference, but b is an int, not a reference to int, so it keeps the original value.
What you want can be achieved by ...
#include <iostream>
using namespace std;
class Foo
{
public:
int a = 1;
int &check()
{
return a;
};
};
int main()
{
Foo foo;
int &b { foo.check() };
cout << b << endl;
foo.check() = 2;
cout << foo.a << endl;
cout << b << endl;
return 0;
}

no match for 'operator<<' in c++

When I run this code, I get the following error
Screenshot erore
50:7: error: no match for 'operator<<' (operand types are 'std::__ndk1::ostream' {aka 'std::__ndk1::basic_ostream'} and 'void')
cout << generateRandomNumber();
compilation terminated due to -Wfatal-errors.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
// class game
class mathGames
{
public:
mathGames();
void generateRandomNumber();
void calculate();
void show();
void answer();
private:
int x;
int y;
int z;
char ans;
};
// constructor
mathGames::mathGames()
{
x = 0;
y = 0;
}
// generate random numbers
void mathGames::generateRandomNumber()
{
srand (time(NULL));
x = rand()%9+1;
y = rand()%9+1;
}
// calculate numbers
void mathGames::calculate()
{
z = x + y;
}
// show generate number
void mathGames::show()
{
cout << " " << x << " + "
<< y << " = " << z << endl;
}
// user answer
void mathGames::answer()
{
cout << " true or false (t/f) ? ";
cin >> ans;
if (ans == 't')
cout <<
generateRandomNumber();
}
// main
int main ()
{
mathGames number;
number.generateRandomNumber();
number.calculate();
number.show();
number.answer();
}
That error it is showed becouse the function don't return anything (it is a void) and so it is impossible to print something.
Also if you want to store somewhere this random values you can pass to the function by address two argouments, in this way you can store the results here and don't lose them after you exit that scope.
Your function is returning void. Therefore you cannot print anything.

without declaration the function first, I can swap the value of the variables?

#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

Pointers, dynamic variables, C++

Errors
I keep receiving errors when running this program. What do these errors mean? A screenshot is attached.
This is the problem:
A class Area that has two private variable members; units of type string and area_value of type float.
1) Input from the keyboard the area_value and its units. Compute one-half and one-quarter of the area and display the results with unit.
2) Destroy the dynamic variable at the end.
#include <iostream
#include <string>
using namespace std;
class Area
{
public:
Area();
void setu(string a);
void seta(float b);
string getu();
float geta();
private:
string unit;
float area_value;
};
int main()
{
Area *p = new Area();
string a;
float f;
cout << "Enter a unit with no space: ";
getline(cin, a);
p->setu(a);
cout << "Enter a value of area: ";
cin >> f;
p->seta(f);
cout << "A half of your input = " << f / 2 << endl;
cout << "A quarter of your input = " << f / 4 << endl;
delete p;
return 0;
}
Looks like
You have not implemented the member functions, or
You have implemented the member functions in a separate file and forgot to include it in building the executable.
It seems you forgot to implement the constructor Area() and the methods of your Area class.
The first include is missing greater than sign (>). The public Area class only defines the functions, not implementing them. Variable p is defined as a pointer.
You can also add half and quarter as functions. I modified it slightly so it compiles with clang++ -Wall -std=c++11 -o area area.cpp.
#include <iostream>
#include <string>
using namespace std;
class Area {
public:
Area() {};
~Area() {};
void setu(string u) { unit = u; }
void seta(float a) { area_value = a; }
string getu() { return unit; }
float geta() { return area_value; }
float getHalf() { return area_value / 2; }
float getQuarter() { return area_value / 4; }
private:
string unit;
float area_value;
};
int main() {
Area p {};
string a;
float f;
cout << "Enter a unit with no space: ";
getline(cin, a);
p.setu(a);
cout << "Enter a value of area: ";
cin >> f;
p.seta(f);
cout << "A half of your input = " << p.getHalf() << endl;
cout << "A quarter of your input = " << p.getQuarter() << endl;
return 0;
}