Logic error, reading from file stream.

I'm having issues reading in from a filestream


The code where the error is located.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Customer::Customer()
{
  static int nameNum = 1;
  ifstream fin1("names.dat");
  for(int i = 0; i < nameNum; i ++)
    fin1.getline(m_name, MAX_CUSTOMER_NAME_SIZE);
  
  m_money = (rand() % 51) + 25;
  m_weight = (rand() % 161) + 90;
  m_cholesterol = (rand() % 271) + 30;
  m_alive = true;
  
  nameNum++;
  fin1.close();
}


The class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const int MAX_CUSTOMER_NAME_SIZE = 50;

class Customer
{
  private:
    char m_name[MAX_CUSTOMER_NAME_SIZE];
    float m_money;
    float m_weight;
    short m_cholesterol;
    bool m_alive;
  public:
    Customer();
    void eat(Burger& munchy);
    friend ostream &operator<<(ostream &out, const Customer person);
};


The driver.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include "Burger.h"
#include "Customer.h"
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
  srand(time(NULL));
  const int NUM_BURGERS = 5;
  const int NUM_PEOPLE = 5;

  Burger burger_array[NUM_BURGERS];
  Customer cust_array[NUM_PEOPLE];

  for(int i = 0; i < NUM_BURGERS; i ++)
    cout << burger_array[i];
  for(int i = 0; i < NUM_PEOPLE; i ++) 
    cout << cust_array[i];
  burger_array[0].setPathogen(true);
  cust_array[0].eat(burger_array[0]);
  cout << cust_array[0];

  return 0;
}


There are several hundred lines that I didn't include here, but everything is for sure #include'd, I'm using namespace std;, and I keep getting poor output.

Krusty Saucy Single Overkill Kermit Burger
         Patties: 1
         Oz. of Bacon: 4
         Num Pickles: 3
         No Cheese
         Has Sauce
         Safe(ish)
Price: 4.6
Krusty Saucy Triumph Overkill Kermit Burger
         Patties: 3
         Oz. of Bacon: 4
         Num Pickles: 3
         No Cheese
         Has Sauce
         Safe(ish)
Price: 6.1
Krusty Mountain Wilbur Tasteless Burger
         Patties: 4
         Oz. of Bacon: 2
         Num Pickles: 0
         No Cheese
         No Sauce
         Safe(ish)
Price: 5
Krusty Cheesy Single Klogger Kermit Burger
         Patties: 1
         Oz. of Bacon: 3
         Num Pickles: 3
         Has Cheese
         No Sauce
         Safe(ish)
Price: 4.25
Krusty Mountain Bacon Kermit Burger
         Patties: 4
         Oz. of Bacon: 1
         Num Pickles: 3
         No Cheese
         No Sauce
         Safe(ish)
Price: 5.25
 is alive, has a weight of 229 lbs and a cholesterol level of 240.
 is alive, has a weight of 240 lbs and a cholesterol level of 198.
 is alive, has a weight of 214 lbs and a cholesterol level of 277.
 is alive, has a weight of 229 lbs and a cholesterol level of 177.
 is alive, has a weight of 208 lbs and a cholesterol level of 268.
They died from the deadly pathogen!
 is dead, has a weight of 232.85 lbs and a cholesterol level of 261.


Here is the overloaded cout <<:

1
2
3
4
5
6
7
8
9
ostream& operator<<(ostream &out, const Customer person)
{
  cout << person.m_name << (person.m_alive ? " is alive," : " is dead,") <<
      " has a weight of " << person.m_weight <<
      " lbs and a cholesterol level of " <<
      person.m_cholesterol << ".";
      cout << endl;
  return out;
}



Before each 'is alive . . .' there should be a name from my names.dat file.
They should be read sequentially into the array of 5 customers as they are constructed. (Thus, only the first 5 should be read, because only an array of 5, not 15, is constructed.)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Marge Simpson
Homer Simpson
Bart Simpson
Lisa Simpson
Maggie Simpson
Ned Flanders
Crazy Cat Lady
Grandpa Simpson
Mayor Quimbyn
Milhouse Van Houten
Mr. Teeny
Drederick Tatum
Judge Snyder
Principal Skinner
Luigi
Last edited on
I noticed that I initially posted the wrong class... fixed.
Topic archived. No new replies allowed.