M-Amr Moussa M-Amr Moussa - 1 month ago 7
C++ Question

How to pass an input from c++ function to the main function

i want to take an input from c++ function and return it to the main function
, i've already tried to do it but the function returns zero , any idea ?

#include<iostream>
using namespace std;
int input( int x);
int main()
{
int number;
input(number);
cout<<number;

}
int input (int x)
{
cin>>x;
return x;
}

H.G H.G
Answer

you need to pass by reference

 void input (int & x)
 {
   cin>>x;  
 }

or use the return value

int main()
{
  int number;
  number = input();
  cout<<number;
}

int input ()
{
  cin>>x;  
  return  x;
}