Newbie Help (Arrays, Structures, Functions,Pointers)

This is a discussion on Newbie Help (Arrays, Structures, Functions,Pointers) within the C++ Programming forums, part of the General Programming Boards category; Newbie C++ programmer here (teaching myself)... trying to learn quick. so i making a simple program that implements (functions, arrays, ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Talking Newbie Help (Arrays, Structures, Functions,Pointers)

    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..
    Attached Files Attached Files

  2. #2
    Ryce
    Guest
    well from what i saw you were getting random valuse that where just put there because u redeclared the pointers.

    You made them in input then killed em when the function ended so all saved data in those structers were gone.

    in output u remade them but didnt set the to anything assumeing that they would already be set. I hope this code works dont have complier to test right now...


    //--------------------INCLUDE-FILES---------------------------\\

    #include <iostream.h>
    #include <conio.h>

    //--------------------GLOBAL-VALUES---------------------------\\

    const int x = 5;

    //--------------------FUNCTION-DECLARATIONS-------------------\\

    void input();
    void output(table1 &table); // take in a structure

    //--------------------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(emp); //RUNS OUTPUT FUNCTION
    }

    /* OUTPUT FUNCTION DISPLAYS EMPLOYEE NUMBERS AND EMPLOYEE AGES.
    RETRIEVES INFO FROM THE 2 ARRAYS */

    void output(table1 &table)
    {
    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;
    }
    }

    havnt sent tables through functions in a while my syntex might be a bit off but my logic should be right. even if this dosnt work(the passing structures) hopefully u get ur mistake of killing the struct then remakeing it without setting the values.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    thnx man

    Thnx.. man replied so fast too..

    yeah thats exactly what i needed thanx... my problem was i was trying to pass three values through my functions which screwed everything up and the destruction of my pointer makes sense..... ill try your code... i have a feeling it will work.. makes sense.. thanx again...

  4. #4
    Ryce
    Guest

    Talking

    no problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone knows how to sort arrays of structures?
    By sherwi in forum C Programming
    Replies: 19
    Last Post: 04-30-2006, 05:52 PM
  2. Newbie question on Structures
    By Gatt9 in forum C++ Programming
    Replies: 15
    Last Post: 03-26-2005, 02:21 AM
  3. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  4. returning arrays of structures
    By manolo21 in forum C Programming
    Replies: 2
    Last Post: 03-31-2003, 07:09 AM
  5. errors with arrays and structures
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 10:48 PM

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