Coverting int values to text

Im trying to do an if statement saying that:

// first takes the users input for the int variable "month" and then converts

if (month == 1)
//then I want to change the variable "month" to the string "January"
// and so on for every month.

Please try to keep it simple becouse im only using beggining c++ knowledge
if month is an int, you can't assign a string to it. You'll need a seperate variable:

1
2
3
4
5
6
7
string monthname;
int monthnumber;

monthnumber = /*whatever*/

if(monthnumber == 1)
    monthname = "January";
wow i feel stupid now.... haha thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	int monthnumber;

	std::cout << "Please type in your month number : " << std::enld;
	std::cin >> monthnumber;


	if (monthnumber == 1)
	{
		std::cout << "January" << std::endl;
	}

    system("PAUSE");
}

Try something along the lines of this.
A switch statement would be ideal for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string month( unsigned int number )
{
    switch( number )
    {
        case 1:  return "January";  break;
        //...
        case 12: return "December"; break;
        default: return "";
    }
}

//...

cout << month( 1 ) << endl;
Last edited on
wow i feel stupid now.... haha thanks.


Dont worry about it sirkip, I once spent 20 minutes trying to figure out how to code a complex function that would do something while a conditional was true >->..In my defense I had spent about 16 hours staring at F' it all levels of complex functions before this...
Topic archived. No new replies allowed.