Friday, July 31, 2009

Help on C++ string problem?

Write a program that reads a date as a string in the form


10/19/72 and displays the same date in the form October 19, 1972.





All I have so far is this:





#include %26lt;iostream.h%26gt;


#include %26lt;iomanip.h%26gt;


#include %26lt;string.h%26gt;





int main()


{





string s1, s2, s3;





cout %26lt;%26lt; "\nEnter a date in the form of mm/dd/yy:\n";


cin %26gt;%26gt; s1 %26gt;%26gt; s2 %26gt;%26gt; s3;





//missing





cout %26lt;%26lt; s1 %26lt;%26lt; ' ' %26lt;%26lt; s2 %26lt;%26lt; ', ' %26lt;%26lt; "19" %26lt;%26lt; s3





return 0;


}





When I enter this onto libra to test, there are errors saying that s1, s2, s3 aren't declared.





For the missing part, should it be something like this?


char s[15];


int i;


int January,Febuary,March,April,May,June;





cout %26lt;%26lt; "\nEnter a date in the form of mm/dd/yy:\n";


cin.getline(s,15);





for(i=0;s[i];i++)


{if (((s[0]*=10)+=s[1])==1)


cout %26lt;%26lt; January %26lt;%26lt; ' ';


else if (((s[0]*=10)+=s[1])==2)


cout %26lt;%26lt; Febuary %26lt;%26lt; ' ';


else if (((s[0]*=10)+=s[1])==3)


cout %26lt;%26lt; March %26lt;%26lt; ' ';


}

Help on C++ string problem?
I'd recommend using scanf. Normally I would never recommend this, since scanf is dangerous to use if you don't know your input is well formed, but here it seems like just the thing.





scanf("%d/%d/%d", %26amp;m, %26amp;d, %26amp;y);





This expects to find input of just the form you want, and decodes each section as an integer and stores it in the correct value.





Now, as for decoding the months, I recommend creating an array with the month names and just looking in it.





static string months[] = {


"January", "February", ...


};





Now the program is pretty easy to write. I converted your embedded \n characters to use the endl manipulator, added "using namespace std", which I think you need if you want to use cout and such, and dropped the .h from your header files to use the newer style header files. If you don't have them, you can add the .h back. Here's my code, which compiles and runs (for me) without warnings or errors. Of course, Yahoo! destroys the formatting. :-(





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;string%26gt;


using namespace std;


static string months[] = {


"January", "February", "March", "April", "May", "June",


"July", "August", "September", "October", "November",


"December",


};


int main() {


int m, d, y;


cout %26lt;%26lt; endl %26lt;%26lt; "Enter a date in the form mm/dd/yy:" %26lt;%26lt; endl;


scanf("%d/%d/%d", %26amp;m, %26amp;d, %26amp;y);


cout %26lt;%26lt; months[m-1] %26lt;%26lt; " " %26lt;%26lt; d %26lt;%26lt; ", " %26lt;%26lt; y %26lt;%26lt; endl;


}





Hope this helps!





EDIT:


I said scanf was dangerous if you didn't know your input was well formed. To see what I mean, try running the program and entering gibberish instead of a date. You might get bad output, errors, or even a segfault!





If I were implementing this in a product, I'd probably just read a line of text and then use a regular expression library to try to match the regular expression ([0-9]+)/([0-9]+)/([0-9]+), and issue an error message if it didn't match. Then I could use atoi to convert each substring into a number, and check the range (0%26lt;month%26lt;13, etc.) and validate the date before I did anything else.





There are date/time libraries on the system I could use to get the date formatted in the correct locale (different countries format dates differently, after all), so I wouldn't need an array of month names at all.


No comments:

Post a Comment