-
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;
}
-
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( );
}
-
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.
-
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.