Passing Array To Function & Display Array Contents

This is a discussion on Passing Array To Function & Display Array Contents within the C++ Programming forums, part of the General Programming Boards category; I am a newbie to C++ and am trying to pass an array to a function and then display the ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Passing Array To Function & Display Array Contents

    I am a newbie to C++ and am trying to pass an array to a function and then display the contents of this array through the function. The array is populated through a different function though both functions reside within the same class. I am struggling to figure out how to execute the function object to execute the display function. Listed below are all of the code components:
    //=============================================
    Monop.h
    Code:
    //Purchase Property Class
    class PurchaseProperty
    {
    public:
    void PropInit(void);
    char Display();
    char DisplayProp(char [],int);
    private:
    };
    //=============================================
    MonopFunct.cpp
    Code:
    #include <iostream>
    #include "Monop.h"
    
    using namespace std;
    using std::cout;
    
    void PurchaseProperty::PropInit()
    {
    char *PropNameArray[] = {"Mediterranean Avenue","Baltic Avenue","Oriental Avenue","Vermont Avenue","Connecticut Avenue","St. Charles Place","States Avenue",
    "Virginia Avenue","St. James Place","Tennessee Avenue","New York Avenue","Kentucky Avenue","Indiana Avenue","Illinois Avenue",
    "Atlantic Avenue","Ventnor Avenue","Marvin Gardens","Pacific Avenue","North Carolina Avenue","Pennsylvania Avenue","Park Place",
    "Boardwalk"}; 
    
    float PropCostArray[] = {60,60,100,100,120,140,140,160,180,180,200,220,220,240,260,260,280,300,300,320,350,400};
    
    float PropRentArray[] = {2,4,6,6,8,10,10,12,14,14,16,18,18,20,22,22,22,26,26,28,35,50};
    
    float PropRent1Array[] = {10,20,30,30,40,50,50,60,70,70,80,90,90,100,110,110,120,130,130,150,175,200};
    
    float PropRent2Array[] = {30,60,90,90,100,150,150,180,200,200,220,250,250,300,330,330,360,390,390,450,500,600};
    
    float PropRent3Array[] = {90,180,270,270,300,450,450,500,550,550,600,700,700,750,800,800,850,900,900,1000,1100,1400};
    
    float PropRent4Array[] = {160,320,400,400,450,625,625,700,750,750,800,875,875,925,975,975,1025,1100,1100,1200,1300,1700};
    
    float PropHotelRentArray[] = {250,450,550,550,600,750,750,900,950,950,1000,1050,1050,1100,1150,1150,1200,1275,1275,1400,1500,2000 };
    
    float PropHouseCostArray[] = {50,50,50,50,50,100,100,100,100,100,100,150,150,150,150,150,150,200,200,200,200,200};
    
    float PropHotelCostArray[] = {50,50,50,50,50,100,100,100,100,100,100,150,150,150,150,150,150,200,200,200,200,200};
    };
    
    char PurchaseProperty::Display()
    {
    int cnt;
    for(cnt = 0; cnt < 22; cnt++)
    {
    cout << "Program is Working";
    }
    return 0;
    }
    
    char PurchaseProperty::DisplayProp(char PropNameArray[], int)
    {
    int cnt;
    for(cnt = 0; cnt < 22; cnt++)
    {
    cout << PropNameArray[cnt];
    }
    return 0;
    }
    //=============================================
    Main.cpp
    Code:
    #include <iostream>
    #include "Monop.h"
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    char DisplayProp(char PropNameArray[],int);
    
    int main()
    {
    
    PurchaseProperty object;
    object.PropInit();
    object.DisplayProp(PropNameArray,22);
    
    return 0;
    }
    //=============================================

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    Code:
     void PurchaseProperty::PropInit()
    {
    char *PropNameArray[] = {"Mediterranean Avenue","Baltic Avenue",
    "Oriental Avenue","Vermont Avenue","Connecticut Avenue",
    "St. Charles Place","States Avenue",
    "Virginia Avenue","St. James Place","Tennessee Avenue",
    "New York Avenue","Kentucky Avenue","Indiana Avenue",
    "Illinois Avenue","Atlantic Avenue","Ventnor Avenue",
    "Marvin Gardens","Pacific Avenue","North Carolina Avenue",
    "Pennsylvania Avenue","Park Place","Boardwalk"};
    In the above code your variable PropNameArray is local to this function. When the function ends the variables are destroyed. This is true for all the variables you have declared inside this function. You will need to put the variables into the class declaration as either private, or public data.

    example:
    Code:
    class mclass
    {
       public:
          int this_is_a_public_variable;
          void public_function(double v) { this_is_a_private_variable = v;}
       private:
          double this_is_a_private_variable;
    };
    Jim

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    27

    Posting Style.

    Elysia,

    Thank you for your response. I will remember to indent my code next time.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    27
    Jimblumberg,

    Thank you for your response. This is my first program and I now I recognize the function scope issue. I recall from my study this issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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