| | |
Help with my code/string compare
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#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; }
-7
#2 Oct 24th, 2009
lines 74-80: you don't need to call strcmp() to compare two std::string objects -- just use their == operators
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) 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
C++ Syntax (Toggle Plain Text)
if( type != normal && type != silver && type != gold && type != plat) valid = false;
Last edited by Ancient Dragon; Oct 24th, 2009 at 10:41 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- convert ascii code to string (C#)
- String compare with Form field (JSP)
- Run code from string? (C#)
- string compare (C++)
- hi.. string compare (Java)
- string compare and fstream (C)
Other Threads in the C++ Forum
- Previous Thread: C / C++ FAQ's and Practice problems
- Next Thread: Win32 App Not Responding while working
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






