0

I'm a beginner at C++ and I am trying to prompt the user to enter the month and year in the format XX/XX an print the written date, day, and season. Although, I can't figure out how to enter the values in the specific format XX/XX in one line. For example, 01/22 for January 22. Also, I'm using a switch statement since it's the focus of the chapter I'm working on so I would like to keep it that way.

#include <iostream>
using namespace std;
int main()
{
    int month, day;
    
    cout << "Enter the date in XX/XX: \n";
    cin >> month;// Using a "space bar" instead of "enter" prevents the new line in cin
    cout << "/";
    cin >> day;
    
    switch (month)
    { // The integers don't accept the single quotes '1'
        case 1: cout << "The date entered is January " << day << ". The season is winter.\n0"; break;
        case 2: cout << "The date entered is February " << day << ". The season is spring.\n"; break;
        case 3: cout << "The date entered is March " << day << ". The season is spring.\n"; break;
        case 4: cout << "The date entered is April " << day << ". The season is spring.\n"; break;
        case 5: cout << "The date entered is May " << day << ". The season is spring.\n"; break;
        case 6: cout << "The date entered is June " << day << ". The season is summer.\n"; break;
        case 7: cout << "The date entered is July " << day << ". The season is summer.\n"; break;
        case 8: cout << "The date entered is August " << day << ". The season is summer.\n"; break;
        case 9: cout << "The date entered is September " << day << ". The season is fall.\n"; break;
        case 10: cout << "The date entered is October " << day << ". The season is fall.\n"; break;
        case 11: cout << "The date entered is November " << day << ". The season is winter.\n"; break;
        case 12: cout << "The date entered is December " << day << ". The season is winter.\n"; break;
        default: cout << "The input is invalid.\n" << endl; break;
    }
    
    return 0;
}

I tried writing cin >> month >> day; but I didn't receive what I wanted by entering in the format I wanted, 01/22. I also tried

cin >> month;
cout << "/";'
cin >> day;

but that required me to press the enter key and then it created a new line.

  • Read a string and then parse the string? stringstream? – Thomas Weller Jun 27 '23 at 06:21
  • You should change the multiline `switch` to using an array in a single line. – 273K Jun 27 '23 at 06:27
  • See https://stackoverflow.com/questions/20688037/how-to-read-stdin-in-standard-c-for-two-numbers-separated-by-comma https://stackoverflow.com/questions/21837521/how-to-read-in-user-entered-comma-separated-integers https://stackoverflow.com/questions/47841396/parse-string-into-integers-using-delimiter-c. @john these questions might be using different separators but the answer is still the same, we really don't need separate questions for every possible separator you can put between numbers – Alan Birtles Jun 27 '23 at 06:39
  • @AlanBirtles When I reopened the question only the first of those answers was listed as a duplicate. I thought the techniques described were far too advanced for a beginner. Frankly I would not write such code. – john Jun 27 '23 at 06:44
  • In this case, there's a completely different technique specific to reading dates that deserves to be mentioned. – Jerry Coffin Jun 27 '23 at 07:09
  • @JerryCoffin there's plenty of questions on how to read dates too, you can always add to the duplicate list – Alan Birtles Jun 27 '23 at 07:40
  • @AlanBirtles: there are lots about reading dates more generally, but at least in the first 10 or so I looked through, all of them were either missing something important here, or else mixed in a lot that was irrelevant, making it hard to find the parts that would really answer this question. – Jerry Coffin Jun 27 '23 at 07:51

3 Answers3

4

The standard library has an I/O manipulator called std::get_time that's specifically intended for reading dates and times like this. There's also a matching std::put_time for printing out dates and times. To get the month and day like this, you could use something on this order:

#include <ctime>
#include <iomanip>
#include <iostream>

int main() {
    std::tm input{};

    std::cout << "Please enter month and day as XX/XX: ";
    std::cin >> std::get_time(&input, "%m/%d");

    std::cout << std::put_time(&input, "The date entered is %B\n");
}

Running this I entered: 03/22 as input, and got:

The date entered is March

As the output.

After entering the date, the month will be stored as an integer in input.tm_mon and the day of the month in input.tm_mday. And as you can see, it already has code to translate month numbers to names (as well as read month names). The month names are also localized, so you can set things up to display the month name in a specified language, or semi-automatically use the language preferred by the user (translating the rest of the string is up to you though).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    I can't believe that has been part of the standard library since C++11 and I'd never seen it before. Obviously this is the best answer. – john Jun 27 '23 at 07:22
1

C++20 answer :

#include <chrono>
#include <format>
#include <iostream>

int main()
{
    std::chrono::year_month_day ymd;
    std::istringstream is{ "6/2023" };

    is >> std::chrono::parse("%m/%Y", ymd);

    std::cout << std::format("Year = {}\n", ymd.year());
    std::cout << std::format("Month = {}\n", ymd.month());

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
0

The simplest way is

int month, day;
char slash;
cin >> month >> slash >> day;

The slash variable reads the slash character in the input.

Now this method does not do any error checking but seems the best option for a beginner.

More sophisticated methods are reading the input as a string and then parsing said string or the methods suggested in the duplicates.

john
  • 85,011
  • 4
  • 57
  • 81