Reading numbers from a date

So I am given a date of "June 10, 1960". My job is to write a program that takes "6/10/60" as the user input all on one line. I am told I need to use the extraction operator several times, I can't use strings. I made variables for month, day, and year to hold each number.

If you multiply the month and the day together they equal the year, this means it's a magic date, and I need to print that when it's true. This is probably really easy, but I've never really done anything like read from the input multiple times w/o strings. I know I need to ignore all the "/" symbols so I have that in my code. This is what I have:
*I added "month = cin.get();
cin.ignore(7, '/');" to try and capture the dates but what I'm getting stored in month is "47" which is ASCII for "/". Also I can't use cin.get because it's only going to store one number.


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
#include <iostream>
using namespace std;

int main()
{
    int month = 0, day = 0, year = 0, date = 0;
    bool magicFalse = 0;

    cout << "Please enter a date in the format " << "\"month/day/2-digit-year\": " << "\n";
    cin >> date;
    // For testing
    //cout << "you have entered: " << date << "\n";

    month = cin.get();
    cin.ignore(7, '/');
    day = cin.get();
    //cin.ignore(0,'/');
    //day = cin.get();

    cout << month;
    //cout << day;


    if(day * month == year)
       {
           magicFalse = true;
       }
    else
        {
            magicFalse = false;
        }
    if(magicFalse == true)
    {
        cout << "That is a magic date!";
    }
    else
    {
        cout << "The date is not magic.";
    }

    return 0;
}
Last edited on
way too complicated.
char slash;
int month,day,year;
cin >> month>>slash>>day>>slash>>year;
if(month*day == year) //don't need extra variable just for condition
..
else
..

that said it could be worth your time to get what you have working, as you will need ignore and similar tools one day. It will work, its just exceedingly complex for the problem at hand.
Last edited on
Ok thanks, that seems to do it. I though doing multiple cin >> "variable" >> "variables" on one line would make multiple inputs.
unfortunately, but in your favor this time, cin runs until the enter key is pressed on a typical console.
For entering time/date, there's also std::get_time manipulator where the format of the input can be specified. See http://www.cplusplus.com/reference/iomanip/get_time/
Topic archived. No new replies allowed.