passing variables

This is a discussion on passing variables within the C++ Programming forums, part of the General Programming Boards category; Ok, here is my program. If you look in the menu sub routine, I have a variable called input. I ...

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    passing variables

    Ok, here is my program. If you look in the menu sub routine, I have a variable called input. I want it to be recognized in the main program. Don't tell me any alternative ways, because i already know some. Only tell me how to get the variable input to be recognized in the main program. Thanks

    void menu (void)
    {
    int input;
    cout<<"Enter the number of what you want to use"<<flush<<endl;
    cout<<"1. Averager"<<flush<<endl;
    cout<<"2. Multiplier"<<flush<<endl;
    cout<<"3. Divider"<<flush<<endl;
    cout<<"4. Subtracter"<<flush<<endl;
    cout<<"5. Adder"<<flush<<endl;
    cout<<"6. Exit"<<flush<<endl;
    cin>>input

    }

    void avg (void)
    {
    cout<<"This program will get the average of numbers you enter."<<endl;
    float g, x, y, num, avg;
    cout<<"How many numbers do you want to enter? ";
    cin>>num;
    do
    {
    cout<<"Please enter your numbers: ";
    cin>>x;
    num=num-1;
    avg=avg+x;
    }while(num>0);
    g=avg/num;
    cout<<"The average of your numbers is: "<<g<<flush<<endl;
    _getch();
    }

    void multiply (void)
    {
    int mult(int a, int b);
    cout<<"This program will get the product of two numbers"<<endl;
    int a, b;
    cout<<"Enter the two numbers (with a space in between): "<<endl;
    cin>>a>>b;
    cout<<"The product of your numbers is"<<flush<<mult(a, b)<<endl;
    _getch();
    }

    void quotient (void)
    {
    int divide(int q, int w);
    int q, w; //Line 61
    cout<<"This program divides two numbers"<<endl;
    cout<<"Enter the numbers (with a space in between): ";
    cin>>q>>w;
    cout<<"The quotient of your two numbers is: "<<flush<<divide(q, w)<<endl;
    _getch();
    }

    void difference (void)
    {
    int subtract(int e, int r);
    int e, r;
    cout<<"This program subtracts two numbers"<<endl;
    cout<<"Enter the numbers (with a space in between): ";
    cin>>e>>r;
    cout<<"The difference of the numbers is: "<<subtract(e, r)<<flush<<endl;
    _getch();
    }

    void sum (void)
    {
    int add(int t, int p);
    int t, p;
    cout<<"This program adds two numbers"<<endl;
    cout<<"Enter the two numbers (with a space in between): ";
    cin>>t>>p;
    cout<<"The sum of the numbers is: "<<flush<<add(t, p)<<endl;
    _getch();
    }

    int main()
    {
    for(input;input<6;input=0)
    {
    menu();
    switch (input)
    {
    case 1:
    avg();
    break;
    case 2:
    multiply();
    break;
    case 3:
    quotient();
    break;
    case 4:
    difference();
    break;
    case 5:
    sum();
    break;
    case 6:
    return 0;
    break;
    }
    } //loop end
    }
    int mult(int a, int b)
    {
    return a*b;
    }
    int divide(int q, int w)
    {
    return q/w;
    }
    int subtract(int e, int r)
    {
    return e-r;
    }
    int add(int t,int p)
    {
    return t+p;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Try these changes:
    Code:
    int menu (void) 
    { 
    int input; 
    cout<<"Enter the number of what you want to    use"<<flush<<endl; 
    cout<<"1. Averager"<<flush<<endl; 
    cout<<"2. Multiplier"<<flush<<endl; 
    cout<<"3. Divider"<<flush<<endl; 
    cout<<"4. Subtracter"<<flush<<endl; 
    cout<<"5. Adder"<<flush<<endl; 
    cout<<"6. Exit"<<flush<<endl; 
    cin>>input 
    
    return input;
    } 
    
    int main( )
    {
           int nOperator = menu( );
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    As an alternative use a reference to an int as the argument for menu like this:

    void menu (int & input)
    //etc

    int main()
    {
    int input = 0;
    menu(input);
    //etc.

    and add a default to your switch statement to handle any number not handled by individual case statements.

    I would really use:

    void menu(int & Input)

    and substitute Input for input everywhere in menu() so I won't confuse the reference from the int, but using the name input for both should be legal since Input is essentially an alias for input.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    You can't do it as written. You're using input before it's declared in menu(), or anywhere else. Doesn't matter how you pass it. You'll have to declare it first, or call menu() first. I don't think defining it before main will help. It would be out of scope anyway, and since main is the first function called, input doesn't exist in the first for loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing values from function to main variables
    By ERJuanca in forum C Programming
    Replies: 18
    Last Post: 06-12-2009, 07:13 PM
  2. Passing variables to a subroutine
    By shaig in forum C Programming
    Replies: 5
    Last Post: 03-16-2009, 06:21 PM
  3. C# in ASP.Net - Passing complex variables
    By Llam4 in forum C# Programming
    Replies: 3
    Last Post: 01-19-2008, 01:53 AM
  4. question on passing variables
    By dirgni in forum C++ Programming
    Replies: 5
    Last Post: 12-04-2002, 03:36 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 01:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21