int display help

This is a discussion on int display help within the C Programming forums, part of the General Programming Boards category; Hi, im trying to write a date problem but im having a little problem. The program will accept an int ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    4

    int display help

    Hi, im trying to write a date problem but im having a little problem.
    The program will accept an int value in the form of yyyymmdd. And i want it to display only partial of the int with printf function. Is there any function to make this work or do i need to modify printf function so it only display first 4 number for year,middle 2 number for month and last 2 for day.

    ex.
    date();
    printf("%d",year);

    Thanks.

  2. #2
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Oops, I guess I didn't read your whole post. Why don't you post the code you have tried and we will help you out from there.
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    The code is something like this:
    Code:
    #include<stdio.h>
    int main()
    {
        void date(int,int *,int *,int *);
        int x,year,month,day;
        printf("Enter a date in yyyymmdd: ");
        scanf("%d",&x);
        date(x,&year,&month,&day);
        printf("\nThe year is %d",year);
        printf("\nThe month is %d",month);
        printf("\nThe day is %d",day);
        
        return 0;
    }
    
    void date(int z,int *y,int *m,int *d)
    {
         *y=(something will make it only display first 4 number)
         *m=(something will make it only display middle 2 number)
         *d=(something will make it only display last 2 number)
    
     }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, you don't want printf, then, to get integer values; you need arithmetic. Specifically, division.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    Quote Originally Posted by tabstop View Post
    Well, you don't want printf, then, to get integer values; you need arithmetic. Specifically, division.

    Im not sure what you meant. Im only a C beginner. Care to explain more deeply?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, you've got an integer such as 20080101. The year part is 2008; to get 2008 from 2008xxxx you need to divide (in this case by 10000). There's not really any C involved here, just arithmetic, except of course that (1) you can do arithmetic in C and (2) integer arithmetic in C discards remainders after dividing.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    4
    Quote Originally Posted by tabstop View Post
    Well, you've got an integer such as 20080101. The year part is 2008; to get 2008 from 2008xxxx you need to divide (in this case by 10000). There's not really any C involved here, just arithmetic, except of course that (1) you can do arithmetic in C and (2) integer arithmetic in C discards remainders after dividing.
    Thanks tabstop. I didnt think of that, how dumb of me . Thanks for the help again.
    Last edited by darkevildemon; 01-03-2008 at 10:11 PM.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    Quote Originally Posted by darkevildemon View Post
    The code is something like this:
    Code:
    #include<stdio.h>
    int main()
    {
        void date(int,int *,int *,int *);
        int x,year,month,day;
        printf("Enter a date in yyyymmdd: ");
        scanf("%d",&x);
        date(x,&year,&month,&day);
        printf("\nThe year is %d",year);
        printf("\nThe month is %d",month);
        printf("\nThe day is %d",day);
        
        return 0;
    }
    
    void date(int z,int *y,int *m,int *d)
    {
         *y=(something will make it only display first 4 number)
         *m=(something will make it only display middle 2 number)
         *d=(something will make it only display last 2 number)
    
     }
    Don't use local function declarations. Put them at the top of the program, below the includes or in a separate header file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21