Home > Back-end >  How to initialize private variables in a different file and make it so it holds the value for other
How to initialize private variables in a different file and make it so it holds the value for other

Time:03-01

I just need to know how to have size, index, and counter keep their values when they are called in another function. Right now they are just being given random values when they are called instead of the values I'm initializing them with in the constructor. How can I fix this? The objective for this code is to make a program that Captures a string of words from a user of the program (via the keyboard) and adds the entered words to a dynamic array of strings. This is the array.cpp file

#include "array.h"
using namespace std;

Array::Array()
{
  int size = 100;
  int index = 0;
  int counter = 0;
  counter   ;
  index  ;
  string *ptr = new string[size];
}

Array::~Array()
{
  delete ptr;
  ptr = nullptr;
}

void Array::populate()
{
  string word;
  cout << "Enter word to add to array: ";
  cin >> word;
  ptr[index] = word;
}

void Array::printContent()
{
  cout << "Number of words in array: " << counter << endl;
  cout << "Array size: " << size << endl;
  cout << "Words in array: " << endl;
  for (int i = 0; i < counter; i  )
  {
    cout << ptr[i] << endl;
  }
}

void Array::displayMenu() const
{
  cout << "[1]  Add Word\n"
       << "[2]  Print Array Information\n"
       << "[3]  Quit Program\n"
       << "Enter Choice:  ";
}
int Array::getChoice(int & choice1) 
{
  cin >> choice1;   
    while (choice1 < 1 || choice1 > 3) {
      cout << endl;
      cout << "Invalid Entry!!" << endl;
      cout << "Enter Choice:  ";
      cin >> choice1; 
    }
  return choice1;
}
int Array::endProgram(int & start2)
{
  start2 = 0;
  cout << "\n\n\t\tThank you for using this system!!\n\n";
  return start2;
}

This is the array.h file

#include <iostream>
#include <string>
using namespace std;

class Array {
public:
  Array();
  ~Array();
  void populate();
  void printContent();
  void displayMenu() const;
  int getChoice(int & choice1);
  int endProgram(int & start2);
private:
  int size;
  int index;
  int counter;
  string *ptr;
};

Lastly this is the main.cpp file

#include <iostream>
#include "array.h"
using namespace std;

int main() {
  int choice = 0;
  int start = 1;
  Array theArray;
  while(choice != 3)
  {
    theArray.displayMenu();
    theArray.getChoice(choice);
    if(choice == 1)
    {
      theArray.populate();
    }
    if(choice == 2)
    {
      theArray.printContent();
    }
    if (choice == 3)
    {
      theArray.endProgram(start);
    }
  }
}

CodePudding user response:

You are defining new local variables inside of your Array constructor and shadowing the member variables of the same name -- which is why the value isn't being preserved.

You only need to specify the type when defining new variables, but not when assigning to existing ones. To assign to the member variables, this should be:

Array::Array()
{
  size = 100;
  index = 0;
  counter = 0;
  ptr = new string[size];
  ...
}

Additionally, in constructors its more correct to use constructor initializer lists to initialize the values:

Array::Array()
  : size{100},
    index{0},
    counter{0},
    ptr{new string[size]}
{
  ...
}
  • Related