Thread: save a data within a struct?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    25

    save a data within a struct?

    Hi guys

    since I'm new to struct, I don't know how to use it properly apparently....I wanna save some time zone in a struct from scanf?

    is it possible? how to save something inserted from keyboard in the struct?? however, the code seems like this :

    Code:
    typedef struct{
    			int hour;
    			int minute;
    			int second;
     	} date1;
    
    printf("give your time zone as for the moment");
    
    scanf("%d %d %d",&date1);
    can you correct this so I can save the data in hour,minute and second variables?


    PS: For some bad reason I must use struct in this part. other ways to save the time wouldn't be helpful

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Each member in the struct is like its own variable. Try it like this:

    Code:
    scanf("%d %d %d", &date1.hour, &date1.minute, &date1.second);
    Quote Originally Posted by Aitra View Post
    PS: For some bad reason I must use struct in this part. other ways to save the time wouldn't be helpful
    Why is it a bad reason? Structs are for grouping related things. It seems natural to group hour, minute and second, so perfect.

  3. #3
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    if u didnt use struct it will be like this,
    Code:
    int hour,minute,second;
    printf("give your time zone as for the moment\n"); 
    
    scanf("%3d%3d%3d",&hour,&minute,&second);
    printf("%4d%4d%4d",hour,minute,second\n);
    Why is it a bad reason? Structs are for grouping related things. It seems natural to group hour, minute and second, so perfect.
    yeah struct making easier to making a program especially when u making a program with many printing and reading variable that can be repeated,

    struct can be useful to combine declaring variables, like int and char together

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    Quote Originally Posted by c99tutorial View Post

    Code:
    scanf("%d %d %d", &date1.hour, &date1.minute, &date1.second);


    Why is it a bad reason? Structs are for grouping related things. It seems natural to group hour, minute and second, so perfect.

    thanks a lot but it's not working..chances are it's a go on C99 but not C..not sure though

    the only error sounds "expected expression before date1" btw.

    I believe I'd use struct more later on but for now it's a hassle everytime I use it as it just seems...
    Last edited by Aitra; 01-15-2013 at 05:40 AM.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Your typedef should be like this:

    Code:
    typedef struct{
                int hour;
                int minute;
                int second;
        } DATE;
        
        DATE date1;
    What it means is that DATE becomes a new custom typename for your structure type. Then of course the line "DATE date1;" declares one variable of your new type.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    it worked and so did your first answer !! the only missing was adding thins line

    Code:
    void getdate (date1 time1){
    
    scanf("%d %d %d", &time1.hour, &time1.minute, &time1.second);
    
    }
    using structure as argument is useful as it seems

    Thanks for your help

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Aitra View Post
    Code:
    void getdate (date1 time1){
    
    scanf("%d %d %d", &time1.hour, &time1.minute, &time1.second);
    
    }
    The only problem is that struct parameters are passed by value. It is not like an array where you modify them inside the function. If you want your function to actually fill in the contents of your struct, you should use a pointer parameter and then address the members like

    Code:
    void getdate (DATE* d)
    {
        scanf("%d %d %d", &d->hour, &d->minute, &d->second);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-14-2012, 06:46 AM
  2. save struct in a txt file problem!
    By xcubis in forum C Programming
    Replies: 1
    Last Post: 05-30-2011, 02:01 PM
  3. Save struct with pointer
    By kovitch in forum C Programming
    Replies: 2
    Last Post: 05-29-2011, 02:43 PM
  4. Save data from two struct arrays in one .dat file
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 03-27-2008, 03:50 PM
  5. Save data
    By jkw2068 in forum C++ Programming
    Replies: 3
    Last Post: 06-20-2003, 06:04 PM