code help!

Started by
10 comments, last by DM7 21 years, 9 months ago
I just started relearning c++ and I''m making some pointless progs to make sure I remember some stuff and I can''t figure out why this program doens''t work it''s supposed to swap the two variables but they stay the same #include <iostream.h> int swap(int,int); int main() { int x; int y; cout<< "Enter a value for X "; cin>> x; cout<< "\nEnter a value for Y "; cin>> y; cout<< "X before Swap() : " << x << "\n"; cout<< "Y before Swap() : " << y << "\n"; swap(x,y); cout<< "X After Swap() :" << x << "\n"; cout<< "Y after Swap() :" << y << "\n"; return 0; } int swap(int x,int y) { int temp = x; x = y; y = temp; return x,y; }
:-P
Advertisement
emm... err... i would make swap() smt like this:
void swap(int*, int*);
don''t you think?

May The Gzoo Be With You!
~Lord Gzoo
--Amir
your code:

int swap(int x,int y)
{
int temp = x;
x = y;
y = temp;

return x,y;
}

Now, you are making two mistakes. First you cannot return two variables at once in a return statement. When you pass x and y to a function, a COPY of them is made and is used INSIDE the function only (function local scope). As you might have (or have not) learned already, whatever is inside { } has its own "scope". Scope simply means that the variables that are declared inside that scope are local to that scope only, you cannot use them anywhere else outside the scope. What you should do in a case like this is to pass parameters as REFERENCES. Passing a reference to a function allows you to modify the original variable from inside the function as if it still belongs to the program''s scope. That way, the results are modified right inside the function "on-the-fly", and therefore you don''t even need to return anything. Here''s your new swap function:

void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}

Note that to pass a reference of a variable, you prefix it with the & operator. However, when calling the function you dont have to specify the & operator:

swap (x,y);

Greg Damon
DM7,

Hi, wow, does this code even compile?

Sorry, I don't mean to be harsh. Your problem is this: you are going about parameter passing the wrong way. You want to pass two integers into swap() and have their values altered within swap(), right?

What you need to do is pass parameters x and y by reference, not by value. This means that you must pass them in a way that gives your function "permission" to alter them. It's a bit more complicated than that, but anyway...

You have two options for passing by reference:

(1) Pass pointers to ints, not ints themselves.
Example:

void swap(int* x, int* y) {
... stuff here...
*x = new value;
*y = new value;
}

and call it like this:
int x, y;
...
swap(&x, &y);

(2) Pass references to ints, not ints themselves.
Example:

void swap(int& x, int& y) {
... stuff here...
x = new value;
y = new value;
}

and call it like this:
int x, y;
swap(x, y);

Check out a good book on C/C++ and read about pointers and references. My recommendation is Ivor Horton's Beginning Visual C++ 6.

Hope this helps.
--Hoozit

(Edit: grammar)

[edited by - HoozitWhatzit on July 15, 2002 2:18:54 AM]

[edited by - HoozitWhatzit on July 15, 2002 2:19:46 AM]
----------------------Check out my game demo and resume at www.fivestory.com/projects/game.
thanks.. I''ve not learned much of this language. function parameters is as far as I''ve gotten and I was side tracked.
:-P
Hi, I''m DM7''s friend, I would like to know how i could find a few header files, particularly unistd.h, termios.h, vga.h, and vgamouse.h. If you could help I would really appreciate it.
I will just add some information so you don''t misunderstand the concept.
Think about this: both x and y are stored somewhere in the memory. When you pass them to a function that accepts two ints, in your case swap(int, int), the compiler actually creates a copy of these values, and use the copy in the function, just like gregd has told ya.

Let me make a simple picture of what''s going on in the memory:
| x | y | ... | ... | copy x | copy y | ... | ... | ...

When you call the swap function, ''copy x'' and ''copy y'' are used inside the function, therefore you''re swapping these two variables, not the original x and y. That''s why you don''t see any changes in those two x and y variables.

To solve this, you can either use reference or pointers as people have said. By using reference or pointers, you are passing the address of x and y, thus you''re dealing with the actual value, not the copy.

My compiler generates one error message: "Doesn''t compile."
-Albert Tedja-
My compiler generates one error message: "does not compile."
I understand it .. but for some reason when I try to do a reference I get a reall weird error
:-P
post the error
---GUI Programming Division Manager at Wildfire Gamesworking on the 0 A.D. project
--------------------Configuration: Swap - Win32 Debug--------------------
Compiling...
Swap.cpp
Linking...
Swap.obj : error LNK2001: unresolved external symbol "int __cdecl swap(int,int)" (?swap@@YAHHH@Z)
Debug/Swap.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Swap.exe - 2 error(s), 0 warning(s)
:-P

This topic is closed to new replies.

Advertisement