ok so I am trying to read in a file. Do some string checking. And output the counters. But somehow I think something is wrong with my strcmp. And I do not know where is the problem. Everything compiles fine too.

#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
#include <cctype>


using namespace std;

const int MAX_ACCOUNTS = 20;

class Customer
{
      public:
             void setCustomer(string,string,string,string,string,string);
             bool checkCustomerData(string, string, string);
             void displayTypes(Customer*) const;
      private:
              string custId, firstName, lastName, address, phoneNum, custType;
              static int normCount,silCount,goldCount,platCount;
      
};


int Customer::normCount = 0;
int Customer::silCount = 0;
int Customer::goldCount = 0;
int Customer::platCount = 0;


void Customer::setCustomer(string id, string fname, string lname, string add, string pnum, string type)
{
     custId = id;
     firstName = fname;
     lastName = lname;
     address = add;
     phoneNum = pnum;
     
     //convert to lower case
     for(int i = 0; i < type.size() - 1; i++)
     {
         custType[i] = tolower(type[i]);
     }
     
}


bool Customer::checkCustomerData(string id, string pnum, string type)
{
     bool validity = true;
     string normal = "normal";
     string silver = "silver";
     string gold = "gold";
     string plat = "platinum";    
     
     
     if(id.size() != 5 || pnum.size() != 8)//check number of digits
        validity = false;
     
     for(int i = 0; i < id.size() - 1; i++)//check if all are digits
     {
         if(!isdigit(id[i]))
            validity = false;
     }
     
     for(int i = 0; i < pnum.size() - 1; i++)
     {
         if(!isdigit(pnum[i]))
            validity = false;             
     }
     
     //check if normal gold silver or platinum
     if(strcmp(type.c_str(), normal.c_str()) != 0)
        validity = false;
        else if(strcmp(type.c_str(), silver.c_str()) != 0)
                validity = false;
        else if(strcmp(type.c_str(), gold.c_str()) != 0)
                validity = false;
        else if(strcmp(type.c_str(), plat.c_str()) != 0)
                validity = false;
                
                
     return validity;
     
}


void Customer::displayTypes(Customer* c) const
{
     string normal = "normal";
     string silver = "silver";
     string gold = "gold";
     string plat = "platinum";  
     
     for(int i = 0; i < MAX_ACCOUNTS; i++)
     {
       if(int a = strcmp(c[i].custType.c_str(), normal.c_str()) == 0)
          normCount++;
        else if(int a = strcmp(c[i].custType.c_str(), silver.c_str()) == 0)
                silCount++;
        else if(int a = strcmp(c[i].custType.c_str(), gold.c_str()) == 0)
                goldCount++;
        else if(int a = strcmp(c[i].custType.c_str(), plat.c_str()) == 0)
                platCount++;
     }
     
     
     cout << endl << endl;
     cout << "***Overall Customer Statistics***" << endl;    
     cout << "Number of Customers Type Normal: " << normCount << endl;
     cout << "Number of Customers Type Silver: " << silCount << endl;
     cout << "Number of Customers Type Gold: " << goldCount << endl;
     cout << "Number of Customers Type Platinum: " << platCount << endl;
     cout << "Total Number of Customers: " << normCount + silCount + goldCount + platCount << endl;
}
     
     


int main()
{
    Customer c[MAX_ACCOUNTS];
    string id, fname, lname, addr, pnum, ctype;
    int i = 0;
    ifstream infile;
    
    infile.open("customers.txt");
    
    while(infile >> id >> fname >> lname >> addr >> pnum >> ctype)
    {
         if(c[i].checkCustomerData(id,pnum,ctype)== true)
            c[i].setCustomer(id,fname,lname,addr,pnum,ctype);
         
         i++;
    }
    
    c[MAX_ACCOUNTS].displayTypes(c);
 
 system("pause");
 return 0;    
}

lines 74-80: you don't need to call strcmp() to compare two std::string objects -- just use their == operators if(type == normal) Recode lines 98-105 similarily.


Also those if statements will give you a false validity result. Lets say the type is gold. The very first if statement about normal will set valid to false, which is incorrect. The way to do it is to have just one if statement that tests all of them at the same time

if( type != normal && type != silver && type != gold && type != plat)
    valid = false;

Edited 7 Years Ago by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.