I had previously posted about a program that is converting a numerical date over into the correct spelled expression. Example:Input 20080101 Output: January 1, 2008.
My instructor tells me to use one variable for the input of the date. And then pull out the two digits for my if statements to convert that number into the correct month.
I'm not asking for my homework to be completed for me. I'm just looking for a start on the "pulling out the digits from the integer". Thanks for any help you may have.
You can try taking in the date as a string. With string you can use thing like length, position, find pos...etc. To seperate them....Then use stringstream to convert it back into int. hehe. I don't know, but that is how I would go about it. :). Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <sstream>
#include <string>
usingnamespace std;
int main(){
cout << "Please enter date: ";
string input("0");
cin >> input;
string year = input.substr(0,4);
int yearInt(0);
stringstream(year) >> yearInt; //<--convert the string into an integer.
cout << yearInt; //<---Do what you want with the number.
}//main
I wish I could do it like that. There expecting me to do this(convert the expression) with Arithmetic Operators (specifically the % and /). I'm totally lost on this issue.
Do you have to use formulae to convert this or will simple subtraction and addition be acceptable?
For a simple method I suggest:
yyyymmdd - yyyy, then mmdd - dd, if you get my meaning?
Its possible to do it this way but I'm not sure if your instructor will accept it. If not you should try working with Faldrax's method.
int main ()
{
int year, month, day;
string date;
cout << "Please enter a date in the following format: YYYYMMDD" << endl;
cin >> date;
year = atof(date.substr(0, 4).data());
month = atof(date.substr(4, 2).data());
day = atof(date.substr(6, 2).data());
cout << year << month << day << endl;
return 0;
}
If you can pull out the day and year then do a modulo so that you get the DDMM part (%10000 I believe) and subtract the MM that you already pulled out. That will leave you with DD00, then just divide that number by 100 and bam, you have DD.
btw- your month is wrong right now. You need to mod by 100, not 10. Otherwise your remainder is the last digit, not the last two. If you're not sure what I mean, try using November or December (11 or 12) in your program.
edit: getting the Month and Day in the right order is confusing me, lol.
edit2: btw, it's a lil sloppy to just divide by 10000 and consider that the equivalent of the year. All you've really done is hide behind the fact that there's no decimal places for an int. Well...I say sloppy...but really it's more just from a pure math standpoint.
You were born in 2008? Holy cow, QWERTYman is the baby who was born speaking code. The legends have long spoken of the child who can run programs without compiling and whose stack is limitless. I never thought I'd live to see it!
Oh and one more thing, you need brackets after your if ( month >= 1 && month <= 12 )
to contain all your month statements, otherwise your else for the invalid input will never trigger.
edit: and make sure you put your cout for the day and month INSIDE those brackets, or it will print even when the invalid input cout triggers.
Everything seems to be working. But jpeg mentioned the date format off. If I put the format as YYYYMMDD I can't get it to print the MM in my if's it only recognizes the last digits? How can I change this by my code?