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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex T
    New Member
    • Oct 2010
    • 29

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

    This is a class in my code:

    Code:
    #include <string>
    
    typedef unsigned short ushort;
    using namespace std;
    
    class numvalue
    {
    private:
    	ushort day;
    	ushort month;
    	ushort year;
    	char number;
    	string str_month;
    	string curdate;
    
    public:
    	numvalue(){};
    	
    	ushort getmonth(string curdate)
     	{
    		str_month = curdate.substr(0,3); //get first 3 chars
    		
    		if (str_month.c_str() == "Jan") month = 1;
    		else if (str_month.c_str() == "Feb") month = 2;
    		else if (str_month.c_str() == "Mar") month = 3;
    		else if (str_month.c_str() == "Apr") month = 4;
    		else if (str_month.c_str() == "May") month = 5;
    		else if (str_month.c_str() == "Jun") month = 6;
    		else if (str_month.c_str() == "Jul") month = 7;
    		else if (str_month.c_str() == "Aug") month = 8;
    		else if (str_month.c_str() == "Sep") month = 9;
    		else if (str_month.c_str() == "Oct") month = 10;
    		else if (str_month.c_str() == "Nov") month = 11;
    		else month = 12;
    
    		return month;
    	};
    
    	ushort getday(string curdate)
    	{
    		number = curdate.at(5);
    		day = (int) number - 48;
    		number = curdate.at(4);
    		day += 10 * ((int) number - 48);
    
    		return day;
    	};
    
    	ushort getyear(string curdate)
    	{
    		number = curdate.at(11);
    		year = (int) number - 48;
    		number = curdate.at(10);
    		year += 10 * ((int) number - 48);
    		number = curdate.at(9);
    		year += 100 * ((int) number - 48);
    		number = curdate.at(8);
    		year += 1000 * ((int) number - 48);
    
    		return year;
    	};
    };
    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?
  • Alex T
    New Member
    • Oct 2010
    • 29

    #2
    Nevermind, I figured it out:

    I can't use c_str();

    Comment

    Working...