Help with my code/string compare

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

Help with my code/string compare

 
0
  #1
Oct 24th, 2009
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.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <iomanip>
  5. #include <cstdlib>
  6. #include <cctype>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. const int MAX_ACCOUNTS = 20;
  12.  
  13. class Customer
  14. {
  15. public:
  16. void setCustomer(string,string,string,string,string,string);
  17. bool checkCustomerData(string, string, string);
  18. void displayTypes(Customer*) const;
  19. private:
  20. string custId, firstName, lastName, address, phoneNum, custType;
  21. static int normCount,silCount,goldCount,platCount;
  22.  
  23. };
  24.  
  25.  
  26. int Customer::normCount = 0;
  27. int Customer::silCount = 0;
  28. int Customer::goldCount = 0;
  29. int Customer::platCount = 0;
  30.  
  31.  
  32. void Customer::setCustomer(string id, string fname, string lname, string add, string pnum, string type)
  33. {
  34. custId = id;
  35. firstName = fname;
  36. lastName = lname;
  37. address = add;
  38. phoneNum = pnum;
  39.  
  40. //convert to lower case
  41. for(int i = 0; i < type.size() - 1; i++)
  42. {
  43. custType[i] = tolower(type[i]);
  44. }
  45.  
  46. }
  47.  
  48.  
  49. bool Customer::checkCustomerData(string id, string pnum, string type)
  50. {
  51. bool validity = true;
  52. string normal = "normal";
  53. string silver = "silver";
  54. string gold = "gold";
  55. string plat = "platinum";
  56.  
  57.  
  58. if(id.size() != 5 || pnum.size() != 8)//check number of digits
  59. validity = false;
  60.  
  61. for(int i = 0; i < id.size() - 1; i++)//check if all are digits
  62. {
  63. if(!isdigit(id[i]))
  64. validity = false;
  65. }
  66.  
  67. for(int i = 0; i < pnum.size() - 1; i++)
  68. {
  69. if(!isdigit(pnum[i]))
  70. validity = false;
  71. }
  72.  
  73. //check if normal gold silver or platinum
  74. if(strcmp(type.c_str(), normal.c_str()) != 0)
  75. validity = false;
  76. else if(strcmp(type.c_str(), silver.c_str()) != 0)
  77. validity = false;
  78. else if(strcmp(type.c_str(), gold.c_str()) != 0)
  79. validity = false;
  80. else if(strcmp(type.c_str(), plat.c_str()) != 0)
  81. validity = false;
  82.  
  83.  
  84. return validity;
  85.  
  86. }
  87.  
  88.  
  89. void Customer::displayTypes(Customer* c) const
  90. {
  91. string normal = "normal";
  92. string silver = "silver";
  93. string gold = "gold";
  94. string plat = "platinum";
  95.  
  96. for(int i = 0; i < MAX_ACCOUNTS; i++)
  97. {
  98. if(int a = strcmp(c[i].custType.c_str(), normal.c_str()) == 0)
  99. normCount++;
  100. else if(int a = strcmp(c[i].custType.c_str(), silver.c_str()) == 0)
  101. silCount++;
  102. else if(int a = strcmp(c[i].custType.c_str(), gold.c_str()) == 0)
  103. goldCount++;
  104. else if(int a = strcmp(c[i].custType.c_str(), plat.c_str()) == 0)
  105. platCount++;
  106. }
  107.  
  108.  
  109. cout << endl << endl;
  110. cout << "***Overall Customer Statistics***" << endl;
  111. cout << "Number of Customers Type Normal: " << normCount << endl;
  112. cout << "Number of Customers Type Silver: " << silCount << endl;
  113. cout << "Number of Customers Type Gold: " << goldCount << endl;
  114. cout << "Number of Customers Type Platinum: " << platCount << endl;
  115. cout << "Total Number of Customers: " << normCount + silCount + goldCount + platCount << endl;
  116. }
  117.  
  118.  
  119.  
  120.  
  121. int main()
  122. {
  123. Customer c[MAX_ACCOUNTS];
  124. string id, fname, lname, addr, pnum, ctype;
  125. int i = 0;
  126. ifstream infile;
  127.  
  128. infile.open("customers.txt");
  129.  
  130. while(infile >> id >> fname >> lname >> addr >> pnum >> ctype)
  131. {
  132. if(c[i].checkCustomerData(id,pnum,ctype)== true)
  133. c[i].setCustomer(id,fname,lname,addr,pnum,ctype);
  134.  
  135. i++;
  136. }
  137.  
  138. c[MAX_ACCOUNTS].displayTypes(c);
  139.  
  140. system("pause");
  141. return 0;
  142. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-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 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
  1. if( type != normal && type != silver && type != gold && type != plat)
  2. 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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC