# Why does this not work?

## Recommended Posts

Can someone tell me why this code does not do what I want? I am a noob. I believe it has something to do with getting input from the user in the choose function. It won't go to int choice in main. I have a requirement if someone is kind enough to fix the code: int choice needs to stay/changed value. Full Code: ------------------------------------------------- #include <iostream> using namespace std; void choose(int choice); int main() { int choice = 2; cout<<choice<<endl; choose(choice); cout<<choice<<endl; system("PAUSE"); } void choose(int choice) { int temp; cin>>temp; temp = choice; }

##### Share on other sites
C/C++ is pass-by-value, not reference.

##### Share on other sites
To pass by reference,

void choose( int& choice )

##### Share on other sites
I googled passing by reference and sort of fixed the code:

#include <iostream>
using namespace std;

void choose(int choice);
void one();

int main()
{
int choice = 2;
choose(choice);
system("PAUSE");
}

void one(int& choice)
{
cin>>choice;
}

void choose(int choice)
{
cout<<choice<<endl;
one(choice);
cout<<choice<<endl;
}

This is not what I want however, because I want choice in main to change value. When I use cout<<choice<<endl; in main, it still shows up as 2 instead of what the user entered (unless they entered 2).

What I want to happen is when I do that test, the number that will show up is the number the user entered. Is this even possible?

I am puzzled why the placement of function one and function choose can mess up the compiling. If I put the function definition of one under the function definition of choose, why does it not compile? May someone enlighten me?

BTW, how do you make those nice code boxes in your posts?

[Edited by - elitentity on December 5, 2004 12:31:20 AM]

##### Share on other sites
no no no :-p yer close, but not quite

I dont usually do C++, but try this:

#include <iostream>using namespace std;void choose(int& choice){   cout<<choice<<endl;   cin>>choice;   cout<<choice<<endl;}int main(){   int choice = 2;   choose(choice);   cout<<choice<<endl;   system("PAUSE");}

edit: and the code boxes are made with the code tags as described here about halfway down.

##### Share on other sites
xanin's got the right idea for how you want to set up the choose method. Make sure you understand what's going on there.

Quote:
 I am puzzled why the placement of function one and function choose can mess up the compiling. If I put the function definition of one under the function definition of choose, why does it not compile? May someone enlighten me?

The declaration of method one() in your function prototypes is a different method than the method one(int&) you define but fail to declare. Since the one(int&) method is not declared, if you try to use it before it has been defined -- for instance, by calling choice before the definition of one -- you will generate an error. You can correct this by fixing the function prototype to read "void one(int& choice);".

In C++, you can't use a "symbol" before it's been declared. A symbol is just a name (called an identifier) that's attached to some location in memory; it could be a variable, a class, a function, or pretty much anything with a name.

If you try to use a symbol before it's been declared, you get a compiler error along the lines of "undeclared identifier 'foo' used at line 27".

As you've noticed, C++ lets you declare a symbol before it's defined. This tells the compiler "I'm letting you know about the existence of this symbol, but I'm not going to define it just yet -- expect the definition later on." However, the compiler doesn't actually check to see that you've defined it. This can be the source of subtle mistakes in your program that aren't detected by the compiler.

If you haven't declared a symbol previously and you then give a definition for it, the compiler declares the symbol for you in the process of defining it. If you try to use a declared but undefined symbol, you'll get a compiler error.

Why is all this important? Let's take a look at your code:

void choose(int choice);void one(); // <-- declared, but never defined!// ** snippet removed **void one(int& choice) // hasn't been declared, so automatically defined{  cin>>choice;}void choose(int choice) // was declared above; no problem{  cout<<choice<<endl;  one(choice); // generates an error if definition hasn't been placed before this point  cout<<choice<<endl;}
As written, this code compiles. But if you switch the order of choose(int) and one(int&), you'll get an error. Why? Because if one(int&) is called before it's declared, the compiler encounters a symbol you haven't yet told it about, so it fails. You told it about "one()" in the prototypes at the start of your file, but that's a different symbol than "one(int&)". Kablooey.

Most likely you just forgot to change the prototypes. Change the declaration of one() to read "void one(int& choice);" and it will compile regardless of order.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628401
• Total Posts
2982464

• 9
• 10
• 9
• 19
• 24