By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

Why does this not compare 2 strings and how do I fix it?

P: 29
This is a class in my code:

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2.  
  3. typedef unsigned short ushort;
  4. using namespace std;
  5.  
  6. class numvalue
  7. {
  8. private:
  9.     ushort day;
  10.     ushort month;
  11.     ushort year;
  12.     char number;
  13.     string str_month;
  14.     string curdate;
  15.  
  16. public:
  17.     numvalue(){};
  18.  
  19.     ushort getmonth(string curdate)
  20.      {
  21.         str_month = curdate.substr(0,3); //get first 3 chars
  22.  
  23.         if (str_month.c_str() == "Jan") month = 1;
  24.         else if (str_month.c_str() == "Feb") month = 2;
  25.         else if (str_month.c_str() == "Mar") month = 3;
  26.         else if (str_month.c_str() == "Apr") month = 4;
  27.         else if (str_month.c_str() == "May") month = 5;
  28.         else if (str_month.c_str() == "Jun") month = 6;
  29.         else if (str_month.c_str() == "Jul") month = 7;
  30.         else if (str_month.c_str() == "Aug") month = 8;
  31.         else if (str_month.c_str() == "Sep") month = 9;
  32.         else if (str_month.c_str() == "Oct") month = 10;
  33.         else if (str_month.c_str() == "Nov") month = 11;
  34.         else month = 12;
  35.  
  36.         return month;
  37.     };
  38.  
  39.     ushort getday(string curdate)
  40.     {
  41.         number = curdate.at(5);
  42.         day = (int) number - 48;
  43.         number = curdate.at(4);
  44.         day += 10 * ((int) number - 48);
  45.  
  46.         return day;
  47.     };
  48.  
  49.     ushort getyear(string curdate)
  50.     {
  51.         number = curdate.at(11);
  52.         year = (int) number - 48;
  53.         number = curdate.at(10);
  54.         year += 10 * ((int) number - 48);
  55.         number = curdate.at(9);
  56.         year += 100 * ((int) number - 48);
  57.         number = curdate.at(8);
  58.         year += 1000 * ((int) number - 48);
  59.  
  60.         return year;
  61.     };
  62. };
  63.  
However, when I compare the current month in the if statement, it does not work, goes like this:

1. str_month = "Oct"

but the code does not go to the month value, and returns 12? Why and how do I fix this?
Oct 30 '10 #1

✓ answered by Alex T

Nevermind, I figured it out:

I can't use c_str();

Share this Question
Share on Google+
1 Reply


P: 29
Nevermind, I figured it out:

I can't use c_str();
Oct 30 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.