Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 02-15-2006, 06:04 PM   #1
Member (8 bit)
 
Join Date: Apr 2004
Location: Phoenix
Posts: 223
C++ value return trouble.

I think this message belongs in this category, but I'm guessing. Here goes...

I'm currently working on a C++ program (just started, still within first 60 lines) and it's doing something really weird. I don't know what the heck's going on here, but at some point an integer variable is being passed to another function that does nothing more than take one piece of input from the user and then returns it, but when it gets returned, the value in the variable is off.... way off. The input is supposed to be between 1 and 13, but right after the variable is set, the value returned is something like -1204396852. To me it's as if the computer is saying "you don't mean that, let's try this one."

In a nutshell, the functions look like this:

#include

using namespace std;

int select(int myOption)
{
cout << "Enter option: ";
cin >> myOption;

return myOption;
}

int main()
{
int opt;

option(opt);

cout << opt;

return 0;
}

Anyone have an idea?

P.S.: I'm on a Linux system working with the built in compiler.
__________________
"Advancement is answering the questions, discovery is questioning the answers."
dataDude is offline   Reply With Quote
Old 02-16-2006, 03:22 AM   #2
Member (7 bit)
 
Join Date: Jun 2001
Location: Australia
Posts: 79
The reason the output is strange is because the variable isn't being assigned to the value of the function. It basically equals null (depending on the compiler).

It should be:

opt = select()

I'm not quite sure why you are passing the opt variable into the function. It isn't going to do anything. So rather than having the function like select(int myOpt), just have select(). But if you do need to do that, it's not the cause of the error, so no need to worry.

Remember the returned value of the function is retrieved by the actual function itself, hence why you need opt = select() in your main function. That means "The variable opt equals the return value of the function select()".

Something like this should work:

int select()
{
int myOption;
cout << "Enter option: ";
cin >> myOption;
return myOption;
}

int main()
{
int opt;
opt = select();
cout << opt;
return 0;
}

Hope that helps
adsta is offline   Reply With Quote
Old 02-16-2006, 10:25 AM   #3
Member (9 bit)
 
Join Date: Feb 2005
Posts: 392
it's well and good to return a value from a function name, but when
more than one value is needed to return from a function then passing by reference is used.

you can use something like:

void select(int &myOption1, int &myOption2)
{
cout << "Enter option 1: ";
cin >> myOption1;
cout << "Enter option 2: ";
cin >> myOption2;

}

int main()
{
int opt1,opt2;

option(opt1,opt2);

cout << opt1 << opt2;

return 0;
}
__________________
words to live by:
others don't know, I know.
others know, I know more.
others know more, I excel.
one shouldnt read this far; above, is meant as an encouragement, translated from a Chinese Proverb.
"He who angers you conquers you." : Elizabeth Kenny

Last edited by alfie2; 02-16-2006 at 10:30 AM.
alfie2 is offline   Reply With Quote
Old 02-16-2006, 02:47 PM   #4
Member (8 bit)
 
Join Date: Apr 2004
Location: Phoenix
Posts: 223
Right after I asked, I figured out the opt = option(var) solution (though I didn't know I could leave the "var" part out). Overall, didn't know it could be done the way you guys told it. I just figured you kept the variable with you throughout the program (meaning I thought you had to pass the variable and return it). Shoot, that works. Thanks. I have another question concerning the program as well, but I'll post separately.
dataDude is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 05:05 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2