Display no value

Hi guys: Can any one please tell me what i make mistake as the program runs but it dont display the age. Thanks.


#include <iostream>
using namespace test;

void getAge(int years);

int main()
{
      int age = 0;
      getAge(age);
      cout << "You are " << age << " years old." << endl;

      system("pause");
      return 0;
}
void getAge(int years)
{  
      cout << "Enter an age: ";
      cin >> years;
}
mustish1Asked:
Who is Participating?
 
Paul JacksonSoftware EngineerCommented:
forgot to take parameter out of the function header :

#include <iostream>
using namespace test;

int getAge();

int main()
{
      int age = 0;
      age = getAge();
      cout << "You are " << age << " years old." << endl;

      system("pause");
      return 0;
}
int getAge()
{  
      int inputage = 0;
      cout << "Enter an age: ";
      cin >> inputage;
      return(inputage);
}
0
 
systanCommented:
void getAge(ref int years) ?
0
 
systanCommented:
or
void getAge(out int years)
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
just change the scope of the age variable as follows
Question: Hi guys: Can any one please tell me what i make mistake as the program runs but it dont display the age. Thanks.


#include <iostream>
using namespace test;

void getAge();
private int age =0;
int main()
{
     
      getAge();
      cout << "You are " << age << " years old." << endl;

      system("pause");
      return 0;
}
void getAge(i)
{  
      cout << "Enter an age: ";
      cin >> age;
}
0
 
Paul JacksonSoftware EngineerCommented:
#include <iostream>
using namespace test;

int getAge(int years);

int main()
{
      int age = 0;
      age = getAge();
      cout << "You are " << age << " years old." << endl;

      system("pause");
      return 0;
}
int getAge()
{  
      int inputage = 0;
      cout << "Enter an age: ";
      cin >> inputage;
      return(inputage);
}
0
 
mustish1Author Commented:
C:\Documents and Settings\Kathy\My Documents\test.cpp: In function `int main()':
C:\Documents and Settings\Kathy\My Documents\test.cpp:4: error: too few arguments to function `int getAge(int)'
C:\Documents and Settings\Kathy\My Documents\test.cpp:9: error: at this point in file

Execution terminated


#include <iostream>
using namespace test;

int getAge(int years);

int main()
{
      int age = 0;
      age = getAge();
      cout << "You are " << age << " years old." << endl;

      system("pause");
      return 0;
}
int getAge()
{  
      int inputage = 0;
      cout << "Enter an age: ";
      cin >> inputage;
      return(inputage);
}
0
 
mustish1Author Commented:
C:\Documents and Settings\Kathy\My Documents\test.cpp:4: error: conflicts with previous declaration `void getAge()'
C:\Documents and Settings\Kathy\My Documents\test.cpp:15: error: `i' was not declared in this scope
C:\Documents and Settings\Kathy\My Documents\test.cpp:16: error: expected `,' or `;' before '{' token

Execution terminated

#include <iostream>
using namespace test;

void getAge();
private int age =0;
int main()
{
     
      getAge();
      cout << "You are " << age << " years old." << endl;

      system("pause");
      return 0;
}
void getAge(i)
{  
      cout << "Enter an age: ";
      cin >> age;
}
0
 
mustish1Author Commented:
#include <iostream>
using namespace test;

void getAge(int years);

int main()
{
      int age = 0;
      getAge(age);
      cout << "You are " << age << " years old." << endl;

      system("pause");
      return 0;
}
void getAge(ref int years)
{  
      cout << "Enter an age: ";
      cin >> years;
}



Compiler: Default compiler
Executing  g++.exe...
g++.exe "C:\Documents and Settings\Kathy\My Documents\test.cpp" -o "C:\Documents and Settings\Kathy\My Documents\test.exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib"
C:\Documents and Settings\Kathy\My Documents\test.cpp:15: error: variable or field `getAge' declared void
C:\Documents and Settings\Kathy\My Documents\test.cpp:15: error: `int getAge' redeclared as different kind of symbol
C:\Documents and Settings\Kathy\My Documents\test.cpp:4: error: previous declaration of `void getAge(int)'
C:\Documents and Settings\Kathy\My Documents\test.cpp:15: error: declaration of `int getAge'
C:\Documents and Settings\Kathy\My Documents\test.cpp:4: error: conflicts with previous declaration `void getAge(int)'
C:\Documents and Settings\Kathy\My Documents\test.cpp:15: error: `ref' was not declared in this scope
C:\Documents and Settings\Kathy\My Documents\test.cpp:16: error: expected `,' or `;' before '{' token

Execution terminated
0
 
mustish1Author Commented:
Thanks a lot.
0
 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
sorry, left the letter 'i' in the getAge() definition. remove it and it should work.

#include <iostream>
using namespace test;

void getAge();
private int age =0;
int main()
{
     
      getAge();
      cout << "You are " << age << " years old." << endl;

      system("pause");
      return 0;
}
void getAge()
{  
      cout << "Enter an age: ";
      cin >> age;
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.