Saturday, May 22, 2010

C++, im taking in int inputs and i have to give out the input but it must be two characters long, please read

ok, so im working with C++ here, and im asking people to input a date in the form mm/dd/yyyy





im using a void function that looks like this:





void output_date (int month, int day, int year)


{


char SLASH = '/';


cout %26lt;%26lt; month %26lt;%26lt; SLASH %26lt;%26lt; day %26lt;%26lt; SLASH %26lt;%26lt; year %26lt;%26lt; endl;


}





when they input the date like this "12/20/2007" the output will look like this "12/20/2007"





but i have to make sure that the 'day' is two digits long, so if they input "12/4/2007" i have to output "12/04/2007"





i only have to do that for the value of 'day'





will someone please help me! i dont know how to make the day number have a '0' infront if it to make it a two digit int, and to not add anything to it if its arleady a two digit int!





p.s. im doing this for a class and i can not change the void function or anything else, we have to only use things we've gone over in class so i can not go and rewrite the whole thing in a different way. it has to be this way or i get an F. i just need a way to put that '0' in there

C++, im taking in int inputs and i have to give out the input but it must be two characters long, please read
void output_date (int month, int day, int year)


{


char SLASH = '/';


cout %26lt;%26lt; month %26lt;%26lt; SLASH;


if (day %26lt; 10) cout %26lt;%26lt; "0";


cout %26lt;%26lt; day %26lt;%26lt; SLASH %26lt;%26lt; year %26lt;%26lt; endl;


}





In programming, you need to SIMPLIFY things and some times look at them in a simple way instead of making them too complex!


Days can consist of either two or one digit(s), right? so they can consist of only one digit if and only if the day is less than 10, right? So you'd make a simple condition in such if the day is less than 10, then add a leading zero, that's all!
Reply:int getInput(char *month) {


month=malloc(10);


*(month+2)=NULL; *(month+1)=NULL;


scanf("%s",month);


if(*(month+2)!=NULL) return 1;


if(*(month+1)==NULL) {


*(month+1)=*month; *(month+1)=0; }


return 0;


}





this wont work as is for year because it is four characters, but u can do it similarly, i think requires header file stdio.h or something like that, look it up if you need to, i am fairly sure that works


No comments:

Post a Comment