Thread: variables in sperate functions

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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