Conversation started Nov 6, 2012 at 18:23.
Nov 6, 2012 18:23
hi
can you talk?
do you see the chat bar at the bottom?
0
Q: In my college, we have to register with our laptop's MAC addresss to the administrator for accessing the wi-fi network. How to get rid of it?

AGNIIn our college , the students have to register with their laptop's MAC address with the administrator to use the wi-fi access to the system. otherwise, Even though is signal is available, we cant use the wi-fi services. Can you computer experts, provide me a way to use the wi-fi without registe...

dummy answer that question, just be professional and tell him there's no easy way to do that (correct answer) and tell him that he shouldn't try to bypass network policy
me and @r.tanner.f from root access will upvote you and you'll have enough rep
quick before it gets VTC'ed :P
Nov 6, 2012 19:02
argh, you need to get your stackexchange account and your superuser account under the same roof, you need to add your google account to your stackexchange account OR vice versa
try talking now, they look merged
i see your superuser account showing up under jtbadn
i tried giving you write access but it thinks your parent account is meta.stackoverflow
1
Q: Why can I not post in chat rooms on Stack Overflow? My reputation is 26, and it says I need 20

Visyoual Possible Duplicate: How long does it take for chat profiles to update? I have 26 reputation points. When I go to a chat room, it says I have to have a reputation of at least 20 to talk. Why can't I post in the chat room, then? I asked a question about a JavaScript problem, and I thin...

Nov 6, 2012 19:25
hey!
I MADE IT! :)
ok...
now that I'm here... give me a minute...
you will likely hate me for doing this to you
i'm not going to write your code, i will only help with conceptual issues
this is like really basic programming, but I am a dummy and haven't got a clue where to start.
okay
go ahead
Nov 6, 2012 19:29
I need you to help me write the code... so that I understand what the hell I am doing.
Program #1: Write a simple test program to validate passing by value and passing by reference.
Instructions

Initialize a variable and display its value.
Pass the variable by value to a function change and display the value inside the function.
Display the value once the function has returned.
Call the same variable passing by reference to another function change and display the value inside the function.
Display the value once the second function has returned.
uh, in what language?
c++
ok... if the way to do this isn't immediately obvious to you, then you probably don't understand passing by value or passing by reference, or both
I don't understand any of it.
Nov 6, 2012 19:32
I passed Programming I with a B+ and I have no idea how...
cause I learned NOTHING.
then you probably need to go back and review everything because there has got to be a reason why you aren't retaining the knowledge or really capturing it
Ok, then I should just read the book again from cover to cover I guess.
:)
passing by value / reference may be a new concept to you, but the instructions to the program are pretty clear
do you know how to initialize a variable in C++?
a local variable
int
........
a bit more than that, right?
Nov 6, 2012 19:34
yes
first of all, int is just the type... it doesn't HAVE to be int, it can be anything else
int stands for integer, which is just one type of variable, but we can use int here
initializing a variable in C++ (or C, or Java, or...) really comes down to declaring the type of the variable, and the name of it
the name is how you refer to the variable in other parts of your code, so that the program knows which variable you're referring to
int main()
{
int a;
char b;
return 0;
}
the type is how the compiler knows what kind of data is contained in the variable
ok... right... so that little program you just provided initializes two variables
well, it declares them, but it doesn't initialize them
int a; declares a variable of type int, and name a
like saying "Dog chip;" would declare a variable of type Dog (the variable represents a dog), and name chip (the way that you refer to the variable from here on out is by typing "chip")
do you understand this much?
ok if the value is "Barry" then should I use int Barry; ?
no -- assigning a value to a variable is done with the = operator. So far we've only declared them
you can declare and initialize a variable in the same statement, or you can do it in two statements. the result is the same.
you could write:
int barry = 0;
OR you could write:
int barry;
barry = 0;
those two statements do the same thing
0 is the value you've assigned to the integer, so you use the equals sign just like in math when you say "x = 0" in algebra
Nov 6, 2012 19:39
ok
so...
so the first part of the assignment told you to initialize a variable
Pass the variable by value to a function change and display the value inside the function.
one line of code can do that
if the second value is Thomas...
int main()
{
int barry;
barry = 0;
{
i HAVE THIS SO FAR..
damn caps
ok, now you need to learn what functions are
your int main() function has opening and closing braces to indicate the body of the function
in C++, you can't have functions defined within functions, but you can define more than one function in a file
Nov 6, 2012 19:41
char *sp
sp = "Hello"l
so what you need to do is define a new function that is not named main(), outside of the opening and closing braces of the main() function
just hang on, you're declaring a string and I'm trying to teach you about functions.
int main()
{
int barry = 0;
}
You have that program. In that program, you have one function. Its name? main.
Whenever you hear someone say "pass by" (e.g., pass by value, pass by reference), we're talking about passing a value TO a function.
To pass anything, you need to have at least one more function in your program, to pass the variable to.
So instead of a program with just one function -- main() -- you need a second function also.
What does this second function need to do? Well, it needs to accept a variable as a parameter, because you're passing a value (or a reference, in the second case) to that function.
In the end, your program is going to have THREE functions in the completed assignment.
One function is given already -- the main() function.
uhh
The second function will need to work by passing by value.
The third function will need to work by passing by reference.
That's basically the entire assignment.
What you're going to do is, call the other two functions you declared, from the main() function.
Here's another function, in case you still don't get what a function is:
int gain()
{
return 0;
}
What's different about gain() than main()? Well, the name is different, and it has a different implementation.
Nov 6, 2012 19:45
yes, I do understand that.
The implementation of the function is the code that goes between the braces.
So, now you can start to lay out the structure of your completed program:
int main()
{
// declare your variable
// call function2 by value
// call function3 by reference
}

int function2()
{
//change the passed value
}

int function3()
{
//change the passed reference
}
now, all you have to do is replace the comments (starting with //) with your code
and you need to add appropriate parameters to function2() and function3()
So, in C++, what's a value variable versus a reference variable?
Here's a hint: a value variable is one like that int barry = 0; we talked about earlier
you should be able to complete the "pass by value" part of the assignment now, without worrying about "references" at all.
you already declared barry, so you need to pass that value into function2, by calling function2 from main().
right..
like this
include<stdio.h>

int main()
{
int barry;
barry = 0;
}
what you have so far is right, but you're missing a # before the include :)
Nov 6, 2012 19:51
right, i didn't have it highlighted.
you should probably write function2() now. What is the purpose of function2()? It's supposed to accept an integer value as a parameter, then change that value, and print it out.
Variable name has the value "Barry"

calling pass by value
Parameter now has the value "Thomas"

pass by value returned
Variable name has the value "Barry"

calling pass by references
Parameter now has the value "James"

call by reference returned
Variable name has the value "James"
This is the example that's given... so I think it best to follow it.
so function 2 would be Thomas?
oh ok, they're using strings
it doesn't matter what name you give to the function
you aren't printing out the name of the function in the output
function2 is fine.
so, instead of int we're going to use char*, which is a string type variable
change your int to a char* in the main() function
Nov 6, 2012 19:53
#include<stdio.h>

int main()
{
char* barry;
barry = 0;
char* barry = "Barry";
you have to assign it a value -- and 0 isn't a valid value for a string
actually it is, because it's a NULL pointer, but I don't want to confuse you
for now just think that when you use a char* type variable (a string), you need to assign it a string, which is in quotes "like this"
#include<stdio.h>

int main()
{
char* barry = "Barry";

// call function2 by value
// call function3 by reference
}
right, you can stop working on main() now, and we'll come back to it later
Nov 6, 2012 19:55
now you wanted me to write function 2
focus on function2 for now
function2 needs to accept a parameter in order to use that variable, barry, from main(), in function2.
when you declare function2, you need to tell the program what type of parameter it accepts.
you know how we have the open and closed parentheses after the name of the function?
do I need to initialize Thomas like this then,...
int function2()
{
char* thomas = "Thomas";

//change the passed value
}
within those parentheses is where you put your formal parameter declaration.
Nov 6, 2012 19:56
Yeah
you have a good idea, and that's pretty close, but instead of having char* thomas in the function body, it needs to be within the parentheses after function2, to indicate that it is a parameter of function2().
THEN, what you can do is assign the string "Thomas" to the parameter itself, within the function body.
int function2(thomas)
{
?
int function2(char* thomas).
oic
without the period...
the thing is, within the body of function2, you can directly refer to thomas (in lowercase, without quotation marks) to read from or write to that passed-in parameter.
Nov 6, 2012 19:59
i understand.
so now, within the body of `function2`, you can do:
thomas = "Thomas";
So here's the thing. Since we are passing by value, when you modify the variable thomas within the body of function2, you are not modifying the value of variable barry within the body of main!
"Pass by value" means exactly this behavior. Whenever you pass the value into a function, that function receives a copy of the value you passed, and can modify it independently.
Let me explain pass by value in terms of real life experience.
On a piece of paper, I write "john". I want to pass this to you by value. To do that, I get another piece of paper and write "john" on it, and hand you the second piece of paper.
If you then erase "john" from your paper with an eraser and write "smith" instead, it won't change the fact that "john" is written on my piece of paper.
and then I can write "athan" at the end of john and change it.
If you pass by reference, it works differently.
Nov 6, 2012 20:03
i see.
If we were passing by reference, then I take a piece of paper, and write "john", and hand you the same paper that I wrote "john" on.
You can then erase "john" and write "smith", and now the paper is changed for both of us -- if I look at the paper you wrote on, it doesn't say "john" anymore. it says "smith"!
understood.
So passing by value makes a copy of the data that you pass to the function. Passing by reference does not copy the data, it's referring to the same data in both functions.
So in function2(), since we passed by value, changing the variable to "Thomas" doesn't make char* barry in main() also say "Thomas".
But in function3() we're going to pass by reference, and when we do that, it will change the value of that char* barry to something else.
so passing reference will change the function that it's been pointed at..
passing function makes a copy
Passing by reference will change the value of the variable in the function that passed it in.
Nov 6, 2012 20:06
passing by value rather.
Passing by value makes a copy.
passing by reference modifies the reference completely.
It modifies the value contained in the variable, not the reference itself.
the reference itself can be changed, but that's not what we're talking about.
so anyway, the assignment asks you to print out some stuff
have you seen cout << something << endl; before?
Nov 6, 2012 20:08
yes
the "something" is where you need to put your strings that you print out in the program, so that you can show proof by running the program that you did the assignment...
cin requires user input, cout prints out something to the screen.
from what I understand, they want you to print out the value of the parameter within the function2 function, and then print out the value of the barry variable back in main().
so in function2...
it should read ..
int function2(char* thomas)
{
thomas = "Thomas";
cout << thomas << endl;
Within main(), before you call function2(), you should cout << "Variable name has the value \"" << barry << "\"" << endl;
What you just wrote is correct, but I think they want you to include some other verbiage, too, I'll help you with that.
Instead of the cout << thomas line, change it to: cout << "Parameter now has the value \"" << thomas << "\"" << endl;
that includes the output that your sample shows
actually, update function2() to have a return type of void, which means it doesn't return anything. Just change int to void.
Nov 6, 2012 20:12
#include<stdio.h>

int main()
{
char* barry = "Barry";
cout << "Variable name has the value \ "" << barry << "\"" << endl;

// call function2 by value
// call function3 by reference
}

int function2(char* thomas)
{
thomas = "Thomas";
cout << "Variable name has the value \ "" << thomas << "\"" << endl;

//change the passed value
}
Great! Almost there. Change the return type of function2(), and then you just have to write function3() and add some more cout statements, and call the functions from main().
The program's starting to take shape though.
change the return type of function2() ?
Yeah. Read up before your paste.
1 min ago, by allquixotic
actually, update function2() to have a return type of void, which means it doesn't return anything. Just change int to void.
void function2(char* thomas)
Nov 6, 2012 20:14
ok
Do you know how to call a function and pass a parameter to it?
i don't recall
Instead of that // call function2 by value you need to call the function. It's one line of code. In fact, it looks almost like the function signature of function2 which you already wrote, except for omitting the return type and changing the parameter to an actual parameter.
Give it a try. It's real simple. You just type the name of the function, then an opening parenthesis, then the name of the variable you're passing in, then the closing parenthesis, then a semicolon.
(don't include the type of the variable you're passing in, only its name.)
you want to pass in barry, by the way.. just in case you were confused
function2(barry); ???
YES!!!!
Perfect :)
Nov 6, 2012 20:17
yay
What that line of code does is, it causes the body of function2() to execute, and when you read or write thomas within the body, it's going to initially contain a copy of the value from barry.
But, since we passed by value (remember that?) you aren't changing barry to "Thomas"!
barry remains Barry
thomas is Thomas
Yes
They're two separate variables because we passed by value.
In function3() the exact opposite will be true.
So now, below the line of code you just wrote -- that function2(barry); -- you need to use cout for the next line of program output.
cout << Variable name has the value \"" << barry << "\"" << endl;
And indeed, barry will have the value of "Barry".
Because we passed by value, not by reference.
"Thomas", in fact, will be destroyed when function2() exits.
So I think at this point you're done all of your program up through the point where you have to pass by reference.
Passing by value and printing out the stuff is done
#include<stdio.h>

int main()
{
char* barry = "Barry";

function2(barry); //calling function2 by value

cout << "Variable name has the value \ "" << barry << "\"" << endl;

// call function3 by reference
}
Post your code so far
Nov 6, 2012 20:22
this what you mean?
Yes, you've got main() right so far
here is everything
#include<stdio.h>

int main()
{
char* barry = "Barry";

function2(barry); //calling function2 by value

cout << "Variable name has the value \ "" << barry << "\"" << endl;

// call function3 by reference
}

void function2(char* thomas)
{
thomas = "Thomas";
cout << "Variable name has the value \ "" << thomas << "\"" << endl;

//change the passed value
}

int function3()
{
//change the passed reference
}
Actually, if you want your code to output the same as the instructor's example, you need to add a cout before you call function2, in main().
So it'll be something like:
cout << ...
function2(barry);
cout <<... (you already have this line written)
#include<stdio.h>

int main()
{
char* barry = "Barry";

cout << "Variable name has the value \ "" << barry << "\"" << endl;

function2(barry); //calling function2 by value

// call function3 by reference
}

void function2(char* thomas)
{
thomas = "Thomas";
cout << "Variable name has the value \ "" << thomas << "\"" << endl;

//change the passed value
}

int function3()
{
//change the passed reference
}
that is how I originally had it.
I thought you had asked me to change that.
Well you need to display it both BEFORE and AFTER calling function2, to match the instructor's output, right?
The point is to show that the value of barry does not change from "Barry" even though the variable was purportedly "modified" in the function2. Because it's by value, it doesn't change.
Nov 6, 2012 20:24
ok
so you were right..
so this is main..
#include<stdio.h>

int main()
{
char* barry = "Barry";

cout << "Variable name has the value \ "" << barry << "\"" << endl;

function2(barry); //calling function2 by value

cout << "Variable name has the value \ "" << barry << "\"" << endl;

// call function3 by reference

}
that's 100% correct, and the // call function3 by reference is going to be almost exactly the same as the three lines where you called function2 and the couts.
you may as well copy and paste from the first cout through the second cout, and change function2 to function3 in the copied code.
huh
?
#include<stdio.h>

int main()
{
char* barry = "Barry";

cout << "Variable name has the value \ "" << barry << "\"" << endl;
cout << "Calling by value" << endl;
function2(barry);
cout << "Variable name has the value \ "" << barry << "\"" << endl;

cout << "Variable name has the value \ "" << barry << "\"" << endl;
cout << "Calling by reference" << endl;
function3(barry);
cout << "Variable name has the value \ "" << barry << "\"" << endl;

return 0;
}
see what I did?
yeah, made "calling by value" and calling by reference" show up on screen
that should be your entire main function -- I think it's totally done, for both functions... I added in a return 0; at the end... now it's done... you just have to write function3
just copy my code from there, and focus on function3
Maybe this will help you, since it's dealing with exactly this problem:
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
I gotta AFK for a minute so read that webpage, it says it exactly as I would say it... pay special attention when you see & because that's what gives away the fact that the function's parameter is being passed by reference
Nov 6, 2012 20:30
ok will do
i think though, you have one too many cout << "Variable...... " in there though..
you're right :D go ahead and remove the extra one
one right after the other one.
it'd just say the same thing, anyway
void function3(int &barry) // barry is now a reference
{
using namespace std;

cout << "barry =" << barry << endl;
char james = "James";

cout << "Variable name has the value \ "" << James << "\"" << endl;

cout << "Call by reference returned." << endl;

}
Tell me if that's right when you get back...
yay!!!!!!! well, except for using namespace std; you should put that at the top of your program :)
Nov 6, 2012 20:39
oh right
also, change << James << to << james <<
and the type of char james should be char* james
and...............
oh right
you need to modify int &barry to reflect the correct type
void function3(char* &barry) // barry is now a reference
{

cout << "barry =" << barry << endl;
char* james = "James";

cout << "Variable name has the value \ "" << james << "\"" << endl;

cout << "Call by reference returned." << endl;

}
char* &barry
yayyyyyy!
uh...
you don't need to declare variable char *james; you can directly modify the value of barry to "James"
barry = "James";
and guess what? lol? the variable barry in the main() method will change to "James"
because you passed by reference!
Nov 6, 2012 20:41
void function3(char* &barry) // barry is now a reference
{

cout << "barry =" << barry << endl;

barry = "James";

cout << "Variable name has the value \ "" << james << "\"" << endl;

cout << "Call by reference returned." << endl;

}
cout << "Variable name has the value \"" << barry << "\"" << endl;
you got rid of james, remember?
hi @KronoS, feel free to stick around if you want to learn basic C++ ;P
just as an FYI we do have this room:

 Fake Programmers

The room for elite Super Users who realize they know nothing a...
@jtbadn is a real life friend so I'm helping him personally, and think that more people would just confuse him
#include<stdio.h>

using namespace std;

int main()
{
char* barry = "Barry";

cout << "Variable name has the value \ "" << barry << "\"" << endl;
cout << "Calling by value" << endl;

function2(barry);
cout << "Variable name has the value \ "" << barry << "\"" << endl;

cout << "Calling by reference" << endl;

function3(barry);
cout << "Variable name has the value \ "" << barry << "\"" << endl;

return 0;
}

void function2(char* thomas)
{
thomas = "Thomas";

cout << "Variable name has the value \ "" << thomas << "\"" << endl;
that's the whole thing.
changed it back to barry
you have an extra function2 body at the end of the program
seems copy and pasted
oh wait no
Nov 6, 2012 20:44
@allquixotic thanks... I know a little, but can always learn more
NEVERMIND
i didn't click "(see full text)"
ok, so this should work then?
well, it looks pretty darn good so far, i'm not sure if it'll output precisely the same text as the example, but the concept is right
it actually does make a lot more sense now that you've taught me how to do this... the second project should be easier.
I can't see your prof giving you less than a B on this because it really looks like you have the two concepts down
to summarize, you declare a variable in main(), you assign it the value "Barry", you pass it by value to function2(), function2 changes its local copy of the value to "Thomas" (but not the main() version of the variable), then you pass the main() variable by reference to function3(), and function3 changes the value of the variable which is shared between main() and function3(), resulting in the value of "Barry" changing to "James" in main().
Nov 6, 2012 20:46
What's the original problem?
@KronoS basically he has to print out some strings and write a program to distinguish between passing by value and passing by reference
Got it.
problem with this line...
cout << "Variable name has the value \ "" << barry << "\"" << endl;
there's a space between the first backslash and the quotation mark
oh yes
Nov 6, 2012 20:49
the backslash means "treat the next character as part of the string literal", but space doesn't have any special meaning, so you can't escape it.
you have to put the backslash there to tell the compiler "this quotation mark is part of the string, I don't want to actually end the string literal here"
still getting error
what error?
cout << "Variable name has the value \"" << barry << "\"" << endl; is that right?
It's going to keep showing up cause I copy/pasted
illegal character \
should be, unless there's an error elsewhere
is it erroring on that line?
What IDE are you using?
Nov 6, 2012 20:53
still giving error
Right now I'm using Dev-CPP
cause it's a lot easier and quicker than visual studio
and it has its own compiler.
@allquixotic WHAT?
@jtbadn The prob with Visual-Studio is that you have to use the Visual C++ if you don't want to configure anything
however if you use CMAKE you can have multiplatform support.
oy no
don't teach him cmake now lol
Nov 6, 2012 20:55
I know... that's why I'm agreeing with him :)
what is wrong with this line?
cout << "Variable name has the value \"" << barry << "\"" << endl;
too many "
nothing? unless there's a space before a \
It'd be nice if we got a more specific error message
" or ' ' ???
always "
it's one character. a double quote character
Nov 6, 2012 20:59
9 C:\Users\Joshua\Desktop\main.cpp `cout' undeclared (first use this function)
yes
AH
#include <iostream>
at the top
 
Conversation ended Nov 6, 2012 at 20:59.