Thread: variables in sperate functions

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    10

    variables in sperate functions

    I'm still a noob in C++ and I was hoping someone could help me with this. My problem is that I would like a function to read a variable made in a previous function.
    my code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int secondary();    //prototypes
    int primary2();
    
    int main()
    {
        int x, y, input;              //i would like "y" to be passed to primary2()
    
        cout<<"input the Primary skill you would like \n";
    
        cout<<"1. flying \n";
        cout<<"2. laser \n";
        cout<<"3. super strength \n";
        cout<<"4. element controller \n";
        cout<<"5. dominator \n\n";
    
        cin>>y;
    
        switch (y){
            case 1:
            cout<<"\nyou chose flying\n\n";
            break;
    
            case 2:
            cout<<"\nyou chose laser\n\n";
            break;
    
            case 3:
            cout<<"\nyou chose super strength\n\n";
            break;
    
            case 4:
            cout<<"\nyou chose element controller\n\n";
            break;
    
            case 5:
            cout<<"\nyou chose dominator\n\n";
            break;
    
            default:
            cout<<"\nerror\n\n";
            main();
            break;
    }
    
        cout<< "choose \n";
        cout<<"1. another primary \n";
        cout<<"2. two secondary\n\n";
    
        cin>>input;
    
    
        switch (input){
            case 1:
            primary2();
            break;
    
            case 2:
            secondary();
            break;
    
            default:
            main();
            break;
    }
    
    }
    
    int primary2(int &y)
    {
        int z;
    
        cout<<"choose another primary\n\n";
    
        cin>>z;
    
        switch (z){              //all the couts are fillers to check for it working
            case 1:
            cout<<"\ni cant believe this worked\n\n";
            break;
    
            case 2:
            cout<<"\nhoothoot\n\n";
            break;
    
            case 3:
            cout<<"\num HI\n\n";
            break;
    
            case 4:
            cout<<"\nyour still here\n\n";
            break;
    
            case 5:
            cout<<"\ni have nothing to say to u\n\n";
            break;
    
            default:
            cout<<"\nerror\n\n";
            void primary2(int &y);
            break;
    }
    
        if (y == z){                   //this is where i need the y to be read
    
            cout<<"\nerror\n\n";
    
            primary2();
    }
    
    }
    
        int secondary()
    {
    
    }
    I need help with this
    Last edited by sounz; 05-15-2010 at 03:34 PM. Reason: post why i'm asking

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sounz
    I'm sorry for how sloppy it is if you would like I can fix it up some more
    Please do, especially by indenting the code more properly and consistently.

    As for your question: do you know how to pass by reference?

    You also might want to use a loop instead of recursion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    i don't know how to pass at all.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    change primary2 signature

    Code:
    void primary2(int &y)
    this means that it accepts a reference to an integer , any changes you make to this integer will change the integer you passed..


  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    I got it to run but the problem is, its not running primary2 when i call it. I'll edit my script uptop so you can see what i changed.
    Last edited by sounz; 05-15-2010 at 02:26 PM. Reason: i got the script to run

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    whenever you use primary 2, make sure you include the new parameter


  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    the problem is greater now that i checked it out the switch (input) doesnt run any of the functions

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "void primary2()" doesn't call a function, it merely declares that such a function exists. If you want to call it, ditch "void".

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    I'm still having problems I tried doing what you said and now its telling me undefined reference to 'primary2()' i changed the code block up top again to show what i've changed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. Global variables and functions.
    By earth_angel in forum C Programming
    Replies: 6
    Last Post: 07-25-2005, 12:38 PM
  3. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 04:19 PM