Jump to content

Help please?

- - - - -

  • Please log in to reply
7 replies to this topic

#1
SlimShady

SlimShady

    Newbie

  • Members
  • Pip
  • 6 posts
Hi,
So I am trying to develop a program to convert Date Formats. i.e If the date entered (mm,dd,yy):02,16,91. The output should be like this : This the 16th day of February 1991.

And I've done this so far :


#include <stdio.h>

#include <stdlib.h>


int main()

{

    int m,d,y;

    printf("Please Enter the Date like this format (mm/dd/yy)>\n");

    scanf("%d/%d/%d/\n",&m,&d,&y);

    printf("The Day is %2d\n",d);

    printf("The Month is %2d\n",m);

    printf("The Year is %2d\n",y);

    printf("This is the %2d Day of %2d %2d",d,m,y);


    return 0;

}

Can you help me please ? What is the best way to do it and how ?
Thanks

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You'll need a case statement to get the text for your month.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
SlimShady

SlimShady

    Newbie

  • Members
  • Pip
  • 6 posts

WingedPanther said:

You'll need a case statement to get the text for your month.

aha I see, but what about the days ? .. 1st , 2nd , 3rd,....5th ....16th and so on ??

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I'd say most easiest way is to use switch statement, check for values and then save the postfix(?); I'm not sure if those 2 letters in day are called like this but I think you know what I mean.

std::string postfix;

switch (day) {

    case 1: postfix = "st"; break;

    case 2: postfix = "nd"; break;

    case 3: postfix = "rd"; break;

    case 4:

    case 31: postfix = "th"; break;

}

// you can do the same to get month names

printf("Today is %d%s day of %s %d", day, postfix, month_name, year);


EDIT: Took me so long that WingedPanther beaten me in a way. ;P
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
SlimShady

SlimShady

    Newbie

  • Members
  • Pip
  • 6 posts

Flying Dutchman said:

I'd say most easiest way is to use switch statement, check for values and then save the postfix(?); I'm not sure if those 2 letters in day are called like this but I think you know what I mean.
std::string postfix;
switch (day) {
    case 1: postfix = "st"; break;
    case 2: postfix = "nd"; break;
    case 3: postfix = "rd"; break;
    case 4:
    case 31: postfix = "th"; break;
}
// you can do the same to get month names
printf("Today is %d%s day of %s %d", day, postfix, month_name, year);

EDIT: Took me so long that WingedPanther beaten me in a way. ;P

Thanks ,but How to declare this string ?.. and what does this sentence means :
std::string postfix; ??
:)

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
std::string postfix;
string is a class that lives in std namespace and here we declared an object named postfix of type string. It's like when you declared d, m and y variables as int, int m,d,y;
string is a C++ object, and if you're doing in C then you'll have to use char arrays. And use strcpy.
char postfix[2];
switch (day) {
    case 1: strcpy(postfix, "st"); break;
    case 2: strcpy(postfix, "nd"); break;
    case 3: strcpy(postfix, "rd"); break;
    case 4:
    case 31: strcpy(postfix, "th"); break;
}

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
@Flying Dutchman, boring English lesson (nd, rd, th, etc..): It's called an English ordinal suffix.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
@Nullw0rm, thanks. It's just unfair that English is universal language. Lucky punks! :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users