Newbie C++ programmer here (teaching myself)... trying to learn quick. so i making a simple program that implements (functions, arrays, structures, pointers)
However for some reason the numbers that get put into the array are not displayed correctly. I type in '5' but when i try and retrieve that from my array it displays a random number or maybe the memory location i'm not sure. Maybe a problem with my pointer. My code is as follows: (any help would be appreciated...attaching .cpp file if u want to view that instead)
//--------------------INCLUDE-FILES---------------------------\\
#include <iostream.h>
#include <conio.h>
//--------------------GLOBAL-VALUES---------------------------\\
const int x = 5;
//--------------------FUNCTION-DECLARATIONS-------------------\\
void input();
void output();
//--------------------STRUCTURES------------------------------\\
struct table1
{
int number[x];
int age[x];
};
//--------------------MAIN------------------------------------\\
int main()
{
input(); //RUNS INPUT FUNCTION
getch();
return 0;
}
//--------------------FUNCTIONS-------------------------------\\
/* INPUT FUNCTION COLLECTS 5 EMPLOYEE NUMBERS AND 5 EMPLOYEE
AGES. THEN STORES THEM INTO 2 SEPARATE ARRAYS */
void input()
{
table1 employee1; //DATA DECLARATION
table1 *emp; //POINTER DECLARATION
emp = &employee1; //POINT TO VALUE
for(int a = 0; a < 5; a++)
{
cout << "Enter Number for Employee # " << (a + 1) << ": ";
cin >> emp->number[a];
cout << "Enter Age for Employee # " << (a + 1) << ": ";
cin >> emp->age[a];
cout << endl;
}
output(); //RUNS OUTPUT FUNCTION
}
/* OUTPUT FUNCTION DISPLAYS EMPLOYEE NUMBERS AND EMPLOYEE AGES.
RETRIEVES INFO FROM THE 2 ARRAYS */
void output()
{
table1 employee1; //DATA DECLARATION
table1 *emp; //POINTER DECLARATION
emp = &employee1; //POINT TO VALUE
cout << "Employee Numbers " << "Employee Ages" << endl;
for(int a = 0; a < 5; a++)
{
cout << emp->number[a] << " " << emp->age[a] << endl;
}
}
well thats it.. a pretty simple prog. Also if you noticed i had to declare (EMPLOYEE1, *EMP, etc..) in every function. Which i would prefer not to do.. i just want to declare them in my main function and pass the variables through input() and output() functions.. but i can't get it to work properly... if anyone knows how to do that it would sure help me out alot..