944,204 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 506
  • C++ RSS
Oct 24th, 2009
0

Help with my code/string compare

Expand Post »
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)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
number87 is offline Offline
83 posts
since Jan 2008
Oct 24th, 2009
-7
Re: Help with my code/string compare
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
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C / C++ FAQ's and Practice problems
Next Thread in C++ Forum Timeline: Win32 App Not Responding while working





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC