could u pls help mi.my onli problem is the Type.i do knw which part of the code is wrong.i dun knw y it printed out value instead of the word gold.i will be greatly appericated for all the help frm u.

sample file:
customerID  firstName lastName  address   phoneNum  Type
10001        Mickey    Mouse   Sengkang  91111111  Gold

tis is the output wen i compile which is wrong:
*** Individual Customer Statistics ***
*** Customer Particulars ***
CustomerID: 10001
Name: Mickey  Mouse
Address: Sengkang
Phone: 91111111
Customer Type: 4199366//wrong    *should be(Gold)

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;

bool allDigits(const string& input);
void toLowerCase(string& input);

class Customer
  enum CustomerType
      Normal = 1,

  Customer () {}
  void setCustomer(int, string, string, string, int, CustomerType);
  void display();
  bool read(ifstream& fin);
  int customerID;
  string firstName;
  string lastName;
  string address;
  int phoneNum;
  CustomerType type;

  static const int ID_LENGTH = 5;
  static const int PHONE_LENGTH = 8;

void Customer::setCustomer(int id, string fn, string ln, string a, int pn, CustomerType)
  customerID = id;
  firstName = fn;
  lastName = ln;
  address = a;
  phoneNum = pn;

void Customer::display()
  cout << endl;
  cout << "*** Individual Customer Statistics ***" << endl;
  cout << "*** Customer Particulars ***" << endl;
  cout << "CustomerID: " << customerID << ' ' << "
Name: " << firstName << "  " <<  lastName << "  "
       << "
Address: " << address << " " << "
Phone: " << phoneNum << " " << "
Customer Type: " << type << endl;

bool Customer::read(ifstream& fin)
  // First read individual strings from the file
  string idText;
  string firstNameText;
  string lastNameText;
  string addressText;
  string phoneText;
  string typeText;

  // Read the entire line in, then create a string stream from the line.
  // The string stream will break the line down into its pieces.
  string line;
  getline(fin, line);
  istringstream ss (line);
  ss >> idText >> firstNameText >> lastNameText >> addressText >> phoneText >> typeText;

  // Now check that each input is valid

  // Check ID
  if (idText.length() != ID_LENGTH || !allDigits(idText)) return false;

  // Check names
  if (firstNameText.length() == 0) return false;
  if (lastNameText.length() == 0) return false;

  // Check address
  if (addressText.length() == 0) return false;

  // Check phone
  if (phoneText.length() != PHONE_LENGTH || !allDigits(phoneText)) return false;

  // Check customer type
  CustomerType inputType;
  if (strcmp(typeText.c_str(), "normal") == 0) inputType = Normal;
  else if (strcmp(typeText.c_str(), "gold") == 0) inputType = Gold;
  else if (strcmp(typeText.c_str(), "silver") == 0) inputType = Silver;
  else if (strcmp(typeText.c_str(), "platinum") == 0) inputType = Platinum;
  else return false;

  // Now assign all inputs to the customer
  return true;

bool allDigits(const string& input)
  for(size_t ix = 0; ix < input.size(); ++ix)
      if (!isdigit(input[ix])) return false;
  return true;

void toLowerCase(string& input)
  for(size_t ix = 0; ix < input.size(); ++ix) input[ix] = tolower(input[ix]);

int main()
  ifstream customers("customers.txt");
  if (!customers.is_open())
      cout << "Error opening customer file";
      exit (1);

  Customer c;
  while (!customers.eof())
      if (c.read(customers)) c.display();

   return 0;

Hi, Alissa.

You are almost there, you just left out a couple of things.

First, in the "setCustomer" function, you are not setting the type in the customer.  This is why you are getting random, bad values for that.  Second, if you want it to display a string for the type (instead of a number between 1 and 4), then you will need to add code to check what the type is and reinterpret that as a string in your display function.

If you have any further questions, please do not hesitate to ask.  I'm here to help. :)


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2017 About.com. All rights reserved.