convert int to string in leap year calculation

int month;
while(std::cin >> month)
if(month > 0 and month <= 12)
std::cout << "The " << month << " month is " << month_names[month-1] << '\n';
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

int main(){
	std::string month_names[] = {
		"January",
		"February",
		"March",
		"April",
		"May",
		"June",
		"July",
		"August",
		"September",
		"October",
		"November",
		"December"
	};

	int month;
	while(std::cin >> month)
		if(month > 0 and month <= 12)
			std::cout << "The " << month << " month is " << month_names[month-1] << '\n';
}

use std::to_string to convert an int to a std::string

http://www.cplusplus.com/reference/string/to_string/

From your code, I'm not actually sure what you are trying to do. You can print 'month' by just doing:

 
cout << month;
Last edited on
here is a sample of the output:

Enter month: (1-12): 3 // user enters the month , say its march, user enters 3

Enter year: 2020 // user enters year

March, 2020, has 31 days // this is the final results , the user will see March , instead of 3
Last edited on
std::string month_names[] = {
"January",

this is [0], right? Your months MUST be 0-11, not 1-12, to use this array of names.

the user types in 3, they are NOT gonna get march.
try
cout << month_names[month-1]; //this will produce march for 3
it will also crash if the user type in 1234 for the month.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

int main()
{
    // INPUT MONTH
    int month = 0;
    std::cout << "Enter month (1-12): ";
    std::cin >> month;
    
    // INPUT YEAR
    int year = 0;
    std::cout << "        Enter year: ";
    std::cin >> year;
    
    // DATA ARRAYS
    std::string month_name[]{"dummy", "January", "February", "March", "April"};
    int no_days[]{0, 31, 28, 31, 30};
    
    int days = 0;
    
    // CHECK FOR LEAP YEAR
    if( (year % 4 == 0)  && (month == 2) )
        days = 29;
    else
        days = no_days[month];
    
    // DISPLAY ANSWER
    std::cout
    << month << '-' << year << " -> "
    << month_name[month] <<  " of " << days << " days\n";
    
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>

int main()
{
    // INPUT MONTH
    int month = 0;
    std::cout << "Enter month (1-12): ";
    std::cin >> month;
    
    // INPUT YEAR
    int year = 0;
    std::cout << "        Enter year: ";
    std::cin >> year;
    
    // DATA ARRAYS
    std::string month_name[]
    {"** ERROR **", "January", "February", "March", "April"};
    
    int no_days[]{0, 31, 28, 31, 30};
    
    int days = no_days[month];
    
    switch(month)
    {
        case 1: case 3 ... 12:
            break;
            
        case 2:
        {
            // CHECK FOR LEAP YEAR
            if( year % 4 == 0 )
                days = 29;
            break;
        }
            
        default:
            std::cout << "Error - invalid month number\n";
    }
    
    // DISPLAY ANSWER
    std::cout
    << month << '-' << year << " -> "
    << month_name[month] <<  " of " << days << " days\n";
    
    return 0;
}
To check if a year is a leap year, then as well as checking for a multiple of 4 you also need to check that it's either not a multiple of 100 or is a multiple of 400. This gives:

1
2
3
4
inline bool isLeapyear(int year)
{
	return (year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0));
}

Last edited on
Topic archived. No new replies allowed.