Sign in to follow this  
xx6heartless6xx

[C++] How to make variable recognized in different functions

Recommended Posts

For example: void Mom:: Bob() { if (choice == "one") ..... } int main() { Mom Joe; cin >> choice; Joe.Bob(); } I want to cin something in the main and then i want the same variable choice to be checked in the function Bob. I was wondering how can I use choice in both functions where they both would recognize it and i can use them in both

Share this post


Link to post
Share on other sites
px    593
You could either make the variable global, ie:


std::string choice;

void Mom:: Bob()
{
if (choice == "one")
.....
}

int main()
{
Mom Joe;
cin >> choice;
Joe.Bob();
return 0;
}



Or better yet just pass the variable to the function:

void Mom:: Bob(std::string const& choice)
{
if (choice == "one")
.....
}

int main()
{
Mom Joe;
std::String choice;
cin >> choice;
Joe.Bob(choice);
return 0;
}



Also, if the valid values are just 1, 2, 3, ... then you might consider using an int rather than a string.

Share this post


Link to post
Share on other sites
I tried to make the variable global by putting it before the function and main(),
and it has no errors when i compile and build but when i input something while
it's running, it crashes!

Here is an example of the code:

string choice;

void Mom:: Bob()
{
if (choice == "one")
.....
}

int main()
{
Mom Joe;
cin >> choice;
Joe.Bob();
}

I do think this might work but why is it all of a sudden crashing?

Share this post


Link to post
Share on other sites
jyk    2094
Your variable, class, and function names are perplexing :-S

Can you post a complete, working program that demonstrates the problem? Be sure to use [ source ] tags (no spaces).

Share this post


Link to post
Share on other sites
apstring choice;

void Maze:: Move()
{

if ( choice == "m1")
....
}
int main()
{
Maze Peter;
cout << "What do you want to do? ";
cin >> choice;
Peter.Move();
}


The whole program is a tad long, but this is the part where I am having the issue
with. After I added the global variable 'choice' then while im running it and i
input something for choice the program crashes, a window opens and asks me to
abort, retry, or ignore and says 'abnormal program termination.' Help!

Share this post


Link to post
Share on other sites
jyk    2094
Source tags, like this:

[source]...code goes here...[/source]

I'm not familiar with 'apstring', but a little Googling indicates that it's some sort of custom string class - is that right? (Ok, Googled a little more - it looks like we're talking about this.)

Maybe to make this example easier for us to work with, you could just use std::string instead.

Also, I think we'll really need a complete, working program if we're going to have any hope of being able to help you with this. If the program is long, start taking things out until you have a minimal example that demonstrates the behavior. It's likely that you'll find the source of the problem (whatever it might be) during this process, but if you don't, you'll then have something that you can post here for us to take a look at.

Share this post


Link to post
Share on other sites
lpcstr    127
When the question as to whether or not to make a variable global arises, it is usually the easiest solution. However, it is also usually the wrong solution. Global variables are bad. You should try to avoid using them whenever possible.

In this case, your program is pretty trivial, but just the same you should practice good habits.

Share this post


Link to post
Share on other sites
dm_3x    100

#include <iostream>
#include <string>
using namespace std;

class Maze
{
public:
void Move( string const& choice );
};

void Maze:: Move( string const& choice )
{
if ( choice.compare( "left" ) == 0 )
cout << "you moved left" << endl ;
}

int main( int argc, char **argv )
{
Maze Peter ;
string sChoice ;

cout << "what direction you want to move?" << endl ;
cin >> sChoice ;

Peter.Move( sChoice );

return 0 ;
}





Inherited from C you can NOT use something like > if ( a_string == "something" ). It is not valid method. <-- looks like it's valid in Cpp only, not C anyway.

[Edited by - dm_3x on May 6, 2010 3:57:03 PM]

Share this post


Link to post
Share on other sites
TimmyC    102
Quote:
Original post by dm_3x
*** Source Snippet Removed ***

Inherited from C you can NOT use something like > if ( a_string == "something" ). It is not valid method.

Pretty sure he couldn't be using cout/cin <</>> if he was coding in C, so a_string == "something" is perfectly valid, assuming apstring implements the operator.

Share this post


Link to post
Share on other sites
alvaro    21266
You should really make an effort to strip your program of anything that is not related to the problem, and then post a small complete program that still shows the problem. The code you posted in your first post is fine. Proof:
#include <iostream>
#include <string>

using namespace std;

string choice;

struct Mom {
void Bob();
};

void Mom::Bob() {
if (choice == "one")
std::cout << "You chose one!\n";
}

int main() {
Mom Joe;
cin >> choice;
Joe.Bob();
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this