Hello!
I'm terribly confused about how to pass a simple integer that was read from my main() to another function (in the same file). I know this is a really silly issue, but searching on the internet is just making me more confused. Here's my code so far.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
using namespace std;

int zonePick(int);

int main()
{
string shipName;
int zone;
cout<<"Space Simuator v0.1"<<endl;
Sleep(1000);
cout<<"Enter a name for your ship!"<<endl;
cin>>shipName;
cout<<"You are the captain of "<<shipName<<"!"<<endl;
Sleep(500);
cout<<"Your job is to guide the "<<shipName<<" through the perils of space!"<<endl;
cout<<"Choose where you want to go in the map?"<<endl;
Sleep(1000);
cout<<"1-Zone One"<<endl;
Sleep(500);
cout<<"2-Zone Two"<<endl;
Sleep(500);
cout<<"3-Zone Three"<<endl;
Sleep(500);
cout<<"4-Zone Four"<<endl;
cin>>zone;
zone=zonePick(zone);

}

int zonePick() {


}

Basically, I want to pass the variable "zone" into "int zonePick()", so that I can create if loops there to send to other functions that control the zones. That's besides the point right now. Any help would be appreciated. I know the code I have right now isn't perfect. Thanks much!

Your implementation of function definition is wrong:

int zonePick(int foo); // class definition (prototype)

The implementation should be the same:

int zonePick(int foo)
{

}

This also assumes that you're passing a variable, or a value to it:

int zonePick(int foo);

int main()
{
    int foo = 10;

    int valueReturnedFromFunction = zonePick(foo); // will store 100 in this variable

    cout << valueReturnedFromFunction << endl;
}

int zonePick(int foo)
{
   // here's an example
   return (foo * 10);
}

Hope this helps a bit :)

Hi! That helped a bit, thanks! But, I'm still confused about my issue. In the example you showed, the function returned a variable back to main. However, I'm just trying to send the value that was created in main, to be used in getZone. So basically:

get the zone in main--->send it to zonePick--->based on number, call other functions (zones)

Also, I wasn't really trying to create a class in the first line? I was trying to define the function lol.
Thanks again!

Ok, so I'm confused now!!

You basically want to send a variable to a function and then for that variable to do something inside the function? Why is it returning an int then? -confused-

Something like this?

void zonePick(int theZone);
void zone1();
int main()
{
    int zone;

    cout << "Please enter which zone you'd like to go to: ";
    cin >> zone;

    zonePick(zone);
}


void zonePick(int theZone)
{
    // in this function then, we can do something with zone

    switch(theZone)
    {
        case 1:
            cout << "ZONE 1";
            zone1();
        break;    

        default: cout << "Zone doesn't exist";
    }

}

void zone1()
{
    // DO SOMETHING IN ZONE 1

}

PERFECT! Thanks much!!! This sounds really silly, but I think I forgot that I have to put the function type as 'VOID' if I didn't want it to return anything. Thanks again!!!

Good luck with it :)! Remember to mark this as solved, and give rep ;)!

I can give rep by upvoting your comments right? Also, marked as solved.

Member Avatar for thendrluca

something like this?

...
void zonePick(int&);

int main() {
    int value = 10;
    cout << value; // output 10
    zonePick(value);
    cout << value; // output 0

    return 0;
}
void zonePick(int& value) {
    value = 0
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.