Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing Array To Function & Displaying Array Contents.

Passing Array To Function & Displaying Array Contents.

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structurestutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Certini
    wrote on last edited by
    #1

    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

    //Purchase Property Class
    class PurchaseProperty
    {
    public:
    void PropInit(void);
    char Display();
    char DisplayProp(char [],int);
    private:
    };

    //============================================= MonopFunct.cpp

    #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 << PropN

    C N 2 Replies Last reply
    0
    • M Mike Certini

      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

      //Purchase Property Class
      class PurchaseProperty
      {
      public:
      void PropInit(void);
      char Display();
      char DisplayProp(char [],int);
      private:
      };

      //============================================= MonopFunct.cpp

      #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 << PropN

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      There are really a lot of errors in this code: 1) In the init function, you initialiaze a couple of arrays but these arrays are local to the init function, so you won't be able to access them. Which means that the code in the init function is useless. It is better to make these arrays members of your class. 2) Why do you declare a global DisplayProp function (in the main.cpp file) ? This function is a member of your PurchaseProperty class, so here you are simply declaring a new function that won't be used at all (and that doesn't even have a definition). 3) In your main function, you are calling DisplayProp and passing PropNameArray as argument. This variable doesn't exist anywhere, you need to actually pass an array that exists. What do you want to print there ? One of the arrays that was initialized in the Init method of your class ? 4) What is Display supposed to do ? What is the different between Display and DisplayProp ? I really suggest that you start from a good book because it seems you lack some fundamental principles and without understanding them really well, you will have a lot of trouble writing any software.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      M 1 Reply Last reply
      0
      • C Cedric Moonen

        There are really a lot of errors in this code: 1) In the init function, you initialiaze a couple of arrays but these arrays are local to the init function, so you won't be able to access them. Which means that the code in the init function is useless. It is better to make these arrays members of your class. 2) Why do you declare a global DisplayProp function (in the main.cpp file) ? This function is a member of your PurchaseProperty class, so here you are simply declaring a new function that won't be used at all (and that doesn't even have a definition). 3) In your main function, you are calling DisplayProp and passing PropNameArray as argument. This variable doesn't exist anywhere, you need to actually pass an array that exists. What do you want to print there ? One of the arrays that was initialized in the Init method of your class ? 4) What is Display supposed to do ? What is the different between Display and DisplayProp ? I really suggest that you start from a good book because it seems you lack some fundamental principles and without understanding them really well, you will have a lot of trouble writing any software.

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        M Offline
        M Offline
        Mike Certini
        wrote on last edited by
        #3

        Cedric, Thank you for your comments. I have been self studying C++ How to Program as well as C Primer Plus. This is my first program. It is not copied from any source as I am trying to learn how to apply the principles. As for my posting to clarify the problem I am having is the following (I should have been more clear in my initial posting): I am attempting to call DisplayProp function which is a member of the PurchaseProperty class. I am making this call in the program Main. I though am having problems getting the program to recognize "PropNameArray". The following is the error message I am getting in Visual Studio 2008: 1>c:\users\mike certini\documents\visual studio 2008\projects\monopoly3\monopoly3\main.cpp(15) : error C2065: 'PropNameArray' : undeclared identifier 1>Build log was saved at "file://c:\Users\Mike Certini\Documents\Visual Studio 2008\Projects\Monopoly3\Monopoly3\Debug\BuildLog.htm" --------------------------------------------------------------------------------

        S 1 Reply Last reply
        0
        • M Mike Certini

          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

          //Purchase Property Class
          class PurchaseProperty
          {
          public:
          void PropInit(void);
          char Display();
          char DisplayProp(char [],int);
          private:
          };

          //============================================= MonopFunct.cpp

          #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 << PropN

          N Offline
          N Offline
          Niklas L
          wrote on last edited by
          #4

          You are also missing the element count argument in DisplayProp

          char PurchaseProperty::DisplayProp(char PropNameArray[], int count)
          {
          int index;
          for(index = 0; index < count; index++)
          {
          cout << PropNameArray[index];
          }
          return 0;
          }

          No point supplying it if you don't use it.

          home

          1 Reply Last reply
          0
          • M Mike Certini

            Cedric, Thank you for your comments. I have been self studying C++ How to Program as well as C Primer Plus. This is my first program. It is not copied from any source as I am trying to learn how to apply the principles. As for my posting to clarify the problem I am having is the following (I should have been more clear in my initial posting): I am attempting to call DisplayProp function which is a member of the PurchaseProperty class. I am making this call in the program Main. I though am having problems getting the program to recognize "PropNameArray". The following is the error message I am getting in Visual Studio 2008: 1>c:\users\mike certini\documents\visual studio 2008\projects\monopoly3\monopoly3\main.cpp(15) : error C2065: 'PropNameArray' : undeclared identifier 1>Build log was saved at "file://c:\Users\Mike Certini\Documents\Visual Studio 2008\Projects\Monopoly3\Monopoly3\Debug\BuildLog.htm" --------------------------------------------------------------------------------

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #5

            As Cedric already said, you really should get a good book, there's just too many problems with your code way beyond the syntactical issues your compiler complains about. The latter is due to your apparent misunderstanding on the scope of variables in C/C++. If you declare a variable, this variable is only known within the scope it is declared in. In most cases this scope is between the previous opening '{' and the corresponding closing '}'. There are basically two exceptions: If you define a variable within the definition of a class (or a struct), then every function that is also defined as a member of that class has access to it. The other exception is when you declare a variable outside a function or class (or struct), which makes it a global variable. Global variables may be accessed anywhere, provided it's declaration comes prior to the location where it's defined. Check your C/C++ book of choice for further explanations on how to declare and access global variables. However, it is a good practice to keep the number of global variables to a minimum. The compiler states that PropNameArray is an undeclared identifier because within the scope of the main function, the variable PropNameArray is not known. It is only known within the function PropInit(), where this variable was declared. Note that all variables will be automatically destroyed when program execution leaves the scope they are declared in. In other words, when you execute the function PropNameInit(), the system will first create all these variables, then initialize them, and, when it leaves the function, immediately destroy them again. As has been pointed out in another response, the function PropInit() currently does nothing! You have three options: 1. Make the variables global (which is bad design). 2. Create the variables outside of the function (i. e. in main) and pass them as arguments to the function PropInit (which is impractical) 3. Declare these variables as attributes of the class Hope this helps to get you started, but like stated before, do yourself a favor and get a book.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups