Archived

This topic is now archived and is closed to further replies.

DM7

code help!

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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-

Share this post


Link to post
Share on other sites
--------------------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)

Share this post


Link to post
Share on other sites
While doing this tutorial (EDIT: what tutorial!?? I meant "while reading responses") I think you changed your function but not your prototype...

Check they are up to date and identical

/G

[edited by - Gee on July 15, 2002 9:04:19 AM]

Share this post


Link to post
Share on other sites