This question already has answers here:
possible assignment in condition (C)
(2 answers)
Closed 8 years ago.
I'm new to StackExchange, but I have a simple class that seems to not return to the correct result when I run it.
Here is the code:
#include <iostream>
using namespace std;
int thisIsHowYouIfLikeABoss2(int, int);
int main()
{
cout << "One." << endl;
thisIsHowYouIfLikeABoss2(9, 9);
cout << "Two." << endl;
thisIsHowYouIfLikeABoss2(4, 9);
return 0;
}
int thisIsHowYouIfLikeABoss2 (int x, int y)
{
cout << "Welcome to the thisIsHowYouIfLikeABoss(), where I calculate if x = y easily." << endl;
if (x = y)
{
cout << "X is Y." << endl;
}
if (x != y)
{
cout << "X is not Y" << endl;
}
}
My compiler is GNU C++ Compiler for Ubuntu,if anyone was wondering.
= is the assignment operator, not the relational equality operator, which is ==.
Change your code to this:
if (x == y)
{
cout << "X is Y." << endl;
}
Protip: if you annotate your function's parameters with const then the compiler would have given you an error with that expression:
int thisIsHowYouIfLikeABoss2( const int x, const int y )
(Unlike in C# and Java, const in C++ does not mean the value is a compile-time fixed-value or literal, so you can use const with variables).
Related
This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 2 years ago.
I was actually thinking that this program should throw a Compilation Error(coz, I am passing values to swap method and not &a, &b) but I was shocked to see that it got executed Successfully.
So, am posting this to know how/why it got executed without any error.
#include <iostream>
using namespace std;
void swap(int* x, int* y)
{
int z = *x;
*x = *y;
*y = z;
}
int main()
{
int a = 45, b = 35;
cout << "Before Swap\n";
cout << "a = " << a << " b = " << b << "\n";
swap(a, b);
cout << "After Swap with pass by pointer\n";
cout << "a = " << a << " b = " << b << "\n";
}
As is said so often on this site, using namespace std; is a bad idea.
You called std::swap<int>
This question already has an answer here:
Why does passing object reference arguments to thread function fails to compile?
(1 answer)
Closed 7 years ago.
My code goes like this :-
#include <iostream>
#include <thread>
using namespace std;
void swapno (int &a, int &b)
{
int temp=a;
a=b;
b=temp;
}
int main()
{
int x=5, y=7;
cout << "x = " << x << "\ty = " << y << "\n";
thread t (swapno, x, y);
t.join();
cout << "x = " << x << "\ty = " << y << "\n";
return 0;
}
This code fails to compile. Can anyone help me out why ?
Not only this code but the code in this also failed to send std::unique_ptr by reference. What's wrong with std::thread ?
The problem is that std::thread copies its arguments and stores them internally. If you want to pass an argument by reference you need to use the std::ref or std::cref functions to create reference wrappers.
Like
thread t (swapno, std::ref(x), std::ref(y));
You can do this, instead:
#include <iostream>
#include <thread>
void swapno (int *a, int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int x = 5, y = 7;
std::cout << "x = " << x << "\ty = " << y << "\n";
std::thread t (swapno, &x, &y);
t.join();
std::cout << "x = " << x << "\ty = " << y << "\n";
return 0;
}
You should achieve the same result ;)
EDIT:
So it seems like everyone is getting the correct output, so my question is now this: Why am I getting the wrong output? Why is the second argument, y, getting changed to zero, regardless if I use a variable or a literal? And no, I didn't get confused and put the wrong variable, I double checked. I am using Visual Studio 2013.
I'm doing an exercise from the fifth edition of the C++ primer by Lippman, Lajoie, and Moo on page 250, and my code for the third exercise, 6.56, is returning incorrect values.
I made a vector of pointers to function of type int (int, int), made four functions of that type (addition, subtraction, multiplication, and division), and added pointers to them to my vector. I attempted to move through those pointers with an iterator, dereferencing them to call them, but for add2 and subtract, it returned the value of the first argument, 0 for mult, and the else clause for divide, even though y was not equal to 0.
The code is as follows:
int test(int x, int y);
int add2(int x, int y);
int subtract(int x, int y);
int mult(int x, int y);
int divide(int x, int y);
typedef decltype(test) *FuncP; //type declaration of a ptr to a function that takes two ints and returns int
int main(){
//6.7
vector<FuncP> ptrsToFuncs;
ptrsToFuncs.push_back(*add2);
ptrsToFuncs.push_back(*subtract);
ptrsToFuncs.push_back(*mult);
ptrsToFuncs.push_back(*divide);
vector<FuncP>::iterator fIter;
int test1 = 6, test2 = 8;
int test3 = 0;
cout << "Running four arithmetic functions with " << test1 << " and " << test2 << "\n\n";
for (fIter = ptrsToFuncs.begin(); fIter != ptrsToFuncs.end(); ++fIter)
{
int result = (*fIter)(test1, test2);
cout << result << endl;
}
system("PAUSE");
}
int test(int x, int y)
{
if (y != 0)
{
cout << "Modulo of one and two is: " << x % y << "\n\n";
return x % y;
}
else
{
cout << "Cannot divide by zero.\n\n";
return -1;
}
}
int add2(int x, int y)
{
cout << "Adding " << x << " and " << y << ": ";
return (x + y);
}
int subtract(int x, int y)
{
cout << "Subtracting " << x << " and " << y << ": ";
return (x - y);
}
int mult(int x, int y)
{
cout << "Multiplying " << x << " and " << y << ": ";
return (x * y);
}
int divide(int x, int y)
{
if (y != 0)
{
cout << "Dividing " << x << " and " << y << ": ";
return (x / y);
}
else
{
cout << "Cannot divide by zero.\n";
return -1;
}
}
For example, with test1 = 6 and test2 = 8, the return values would be: 6, 6, 0, "Cannot divide by zero." -1.
I also tried this: (**fIter)(test1, test2). I thought that maybe I didn't dereference enough and needed to dereference the pointer to the function as well as the iterator, but it resulted in the same output.
Thank you.
Your code works fine for me, I think possibly you mixed up your variables inside your iteration (replaced test2 to with test3)
for (fIter = ptrsToFuncs.begin(); fIter != ptrsToFuncs.end(); ++fIter)
{
int result = (*fIter)(test1, test3);
cout << result << endl;
}
would give you
6, 6, 0, "Cannot divide by zero." -1.
If you need to see what your code is doing, try replacing your variables with literals and also adding some couts to your function calls.
for (fIter = ptrsToFuncs.begin(); fIter != ptrsToFuncs.end(); ++fIter)
{
int result = (*fIter)(12, 24);
cout << result << endl;
}
int add2(int x, int y)
{
cout<<"add function called with first variable"<<x<<" and second variable"<<y<<endl;
return (x + y);
}
I feel like an idiot. When I pass a variable to the function it results in a strange output like 6.2+e003 instead of the value that the variable holds. What am I doing wrong?
The x in main and the x in the function are different?
main:
int x, y;
while(system.WindowOpen())
{
x++;
y++;
bg.Draw2D();
bob.Think(x, y);
riley.Think(x, y);
system.Render(0);
}
class method:
void Organism::Think(double x, double y)
{
std::cout << "X: " << x << "\n";
std::vector<double> food;
food.push_back(x);
food.push_back(y);
std::cout << "VECTOR: " << food[0] << " " << food[1] << "\n";
std::vector<double> path;
if(refresh.IsTime()) {
std::cout << "\nFOOD VECTOR: \n" << food[0]
<< "\n" << food[1] << "\n";
path = brian.GetOutput(food);
organism.Translate2D(path[0], path[1]);
if(organism.IsOffScreen2D(resX, resY) == 'l' )
organism.SetPos2D(resX, organism.GetY());
if(organism.IsOffScreen2D(resX, resY) == 'r')
organism.SetPos2D(0, organism.GetY());
if(organism.IsOffScreen2D(resX, resY) == 't')
organism.SetPos2D(organism.GetX(), resY);
if(organism.IsOffScreen2D(resX, resY) == 'b')
organism.SetPos2D(organism.GetX(), 0);
};
font.DrawNumber2D(x, 50, 50);
font.DrawNumber2D(y, 50, 100);
organism.Draw2D();
}
Both x and y are uninitialized here:
int x, y;
so they can hold any value, and reading from them is undefined behaviour. You should initialize them:
int x = 0
int y = 0;
I was writing outside the bounds of a vector. I switched from using the [] operators to using the .at() and found my bug right away. Just a bit of memory corruption. I feel quite silly. Thanks all!
I'm learning C++ and I have trouble with getting recursion working when a function is called by itself.
#include <iostream>
using namespace std;
int countdown(int y) {
if (y==1) {
return 1 && cout << y << endl;
}
else {
return countdown(y-1);
}
}
int main () {
cout << "Countdown from ten: " << endl;
cout << countdown(10) << endl;
}
Of course there are other ways to achieve this, but really I created this example to verify my own understanding of how functions are called recursively.
In the example I added && cout << y to verify if y is being passed to the function as 1, which always appears to be the case irrespective that I call the function as countdown(10).
Could someone tell me if I'm missing something obvious here please?
Your ' cout << y ' only executes if y has been tested to be one.
This version does what I think you want:
#include <iostream>
using namespace std;
int countdown(int y)
{
cout << y << endl;
if (y==1)
{
return 1;
}
else
{
return countdown(y-1);
}
}
int main()
{
cout << "Countdown from ten: " << endl;
cout << countdown(10) << endl;
}
Your call stack looks like this:
main
countdown(10)
countdown(9)
countdown(8)
countdown(7)
countdown(6)
countdown(5)
countdown(4)
countdown(3)
countdown(2)
countdown(1)
std::cout << 1 << std::endl;
If you want to see the whole countdown, move the output command in front of the if condition.
Also, your style of writing the output is very unidiomatic. Note that it only works because 1 %&& cout converts the cout to bool and bool can be converted to int. Please don't write code like that.