0

I'm trying to write a code that will get values from a file and store them into a class variable. I'm not saving them according to my code. I would really appreciate the help. I don't know where my error is. The first value from the file is the number of class objects there is in total.

class customer {
    private:
        string *names;
        char *plans;
        int *hours;
        int customerTotal;
    public:
       string name;
       char plan;
       int hour;

       customer() {
              name = "";
              plan = ' ';
              hour = 0;
              customerTotal = 0;
        }
       void setName(string x) {
              names[customerTotal] = x;
        }
       void setPlan(char x) {
              plans[customerTotal] = x;
        }
       void setHours(int x){
              hours[customerTotal] = x;
              customerTotal++;
        }
       void printArray(){
             for (int i = 0; i < customerTotal;i++){
           cout << names[i] << plans[i] << hours[i] << endl;
         }

    }
};

int main() {
    int numCustomers;
    ifstream inFile;
    string n;
    char p;
    int h;


    inFile.open("customers.txt");
    if (!inFile)
    {
        cout << "File not found!" << endl << endl;
        system("pause");
        return 1;
    }
    customer x;
    inFile >> numCustomers;
    while (!(inFile.eof())) {
        inFile >> n >> p >> h;
            x.setName(n);
            x.setPlan(p);
            x.setHours(h);
    }
    x.printArray();
    inFile.close(); 
}
eyllanesc
  • 190,383
  • 15
  • 87
  • 142
  • 1
    There's some fundamental problems with this code. Why is `customer` a collection? Why can't you have your class focused on *one customer* and one customer only and then make a `std::vector` as the collection? This code depends on a very specific `setX` order to increment the array properly, and it never initializes the array correctly in the first place. It's full of *undefined behaviour* which will causes crashes. – tadman Sep 13 '17 at 01:23
  • Sorry, I'm pretty new to this language so I don't know much about it. I'm supposed to save the information from a file in order to figure out how much each customer has to pay. The input file include name, plan rate and the hours spent. Name = string, plan rate = char, and hours spent = int. – Maryfer Sep 13 '17 at 01:26
  • It'd be a lot easier to write a class that handles a singular customer, and then worry about those in aggregate using a Standard Libarary container like `std::vector`. You'll want to write getters/setters for your `customer` class unless you'd prefer to use a `struct` with direct access. It's up to you as to how much formality you want here. – tadman Sep 13 '17 at 01:28
  • 1
    Instructions similar to the following `type * some_name` create pointers, do not create arrays – eyllanesc Sep 13 '17 at 01:29
  • 1
    In any language, it is best to start with something small and simple that works perfectly, then add complexity a little at a time. You are trying to do too many new things at once. – Beta Sep 13 '17 at 01:29
  • Thanks! I'll try to do that now. :) – Maryfer Sep 13 '17 at 01:30
  • as @tadman says `std::vector`, these if they create a structure similar to a vector where you can add elements dynamically to the C ++ style, you are using the C style and you have errors because you do not understand. – eyllanesc Sep 13 '17 at 01:31
  • If you're interested in learning C++ from the ground up, don't forget to get a [good reference book](http://www.stroustrup.com/4th.html). It's a notoriously finnicky language, so things that compile may not work at all, or worse, may appear to work and then suddenly malfunction in catastrophic ways. Attention to detail is paramount. As such, it's always best to write unit tests using something like [Catch](https://github.com/philsquared/Catch) to verify the functionality of small bits of code before integrating them into the greater program. – tadman Sep 13 '17 at 01:33
  • You should read this before you develop too many bad habits. :) https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Retired Ninja Sep 13 '17 at 01:41

0 Answers0