user2971623
user2971623

Reputation: 3

C store values in an array

My program reads the following data: Date, Distance and Time.

So for example an input would look like:

Date: 10.10.2013

Distance (m): 500

Time (HH:MM:SS): 01:20:05

I want to store all this information in an array. The problem is that I want the user to input date, distance and time several times and the program has to save all the data in an array.

I can't store it like this, because then how would I know which index is the date index? And how should I store the time? I can't store it with :.

arr[0] = 10.10.2013

arr[1] = 500

arr[2] = 012005

arr[3] = 22.10.2013

arr[4] = 200

arr[5] = 000510

Upvotes: 0

Views: 5256

Answers (2)

ryyker
ryyker

Reputation: 23208

Because the type of data you will need to store, it needs type char, so it can be stored in strings. (i.e. "10.10.2013")
First, Define a struct PARAM:

typedef struct {
   char date[20];
   char distance[20];
   char time[20];
} PARAM;

use PARAM to create an array of your struct:

PARAM param[20];

Now, you can use it like this for example:

int main(void)
{
    strcpy(param[0].date, "10.10.2013");    
    strcpy(param[0].time, "05:02:10");    
    strcpy(param[0].distance, "500");
    //and so on for all the struct array elements, 0 through 20
    return 0;
}

Or, better yet, using printf() and scanf() statements as necessary, you can prompt the user for input in a loop, and store in your struct array:

int main(void)
{
    int i;
    for(i=0;i<20;i++)
    {
        printf("enter date %d:", i+1);
        scanf("%s", param[i].date);


        printf("enter distance %d:", i+1);
        scanf("%s", param[i].distance);


        printf("enter time %d:", i+1);
        scanf("%s", param[i].time);
    }
    return 0;
}  

EDIT Regarding question in comment Therefore I guess its best to store them in date.day, date.month and date.year. Right? That approach would work, and I include it below, but it is a little more tedious to enter data that way. I included a second example below that might improve both, entering data, and storing data.

So, Per your comment, two ways jump to mind:

ONE, create struct members that contain the discrete members of time and date as integers: i.e.

typedef struct {
    int day;
    int month;
    int year;
    int hour;
    int minute;
    int second;
}TIMEDATE;

Use this struct as a member of the PARAM struct;

typedef struct  {
    int distance;
    TIMEDATE timedate;
}PARAM;  

PARAM param[20];

Now, just modify and expand the example of the last main() function to include scanning in values for the new struct members. This will be more tedious for the person using your program, but will allow you to keep all the input values as numbers as you have indicated.

//Note the changes in scanf format specifiers for int, "%d":
//     in all the statements
int main(void)
{
    int i;
    for(i=0;i<20;i++)
    {
        printf("enter date %d:", i+1);
        scanf("%d", &param[i].timedate.day);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].timedate.month);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].timedate.year);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].timedate.hour);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].timedate.minute);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].timedate.second);

        printf("enter time %d:", i+1);
        scanf("%d", &param[i].distance);
    }
    return 0;
}

Two, modify the first approach to include using strings AND integers, all in the same struct. This will make it easier for the user user to enter time and date information, and possible for you to manipulate the data easier. And a bonus, it will demonstrate how to parse the user input string data into integer data.

typedef struct {
    char date[20];//keep as char
    char time[20];//keep as char
    int distance; //changed to int
    TIMEDATE timedate;//container for in data   
} PARAM;

//use PARAM to create an array of your struct:

PARAM param[20], *pParam; //create a pointer to pass

int GetIntData(PARAM *p, int index);//prototype for new function

//Note the changes in scanf format specifiers for int, "%d":
//     in all the statements
int main(void)
{
    int i, loops;
    pParam = &param[0]; //initialize pointer to struct

        printf("How many sets of data would you like to enter? :");
        scanf("%d", &loops);


    for(i=0;i<loops;i++)
    {
        printf("enter date (eg:MM.DD.YYYY): %d:", i+1);
        scanf("%s", pParam[i].date);

        printf("enter time (eg HH:MM:SS): %d:", i+1);
        scanf("%s", pParam[i].time);

        printf("enter distance %d:", i+1);
        scanf("%d", &pParam[i].distance);

        GetIntData(pParam, i);
    }
    return 0;
} 
//reads string members into integer members
int GetIntData(PARAM *p, int index)
{
    char *buf=0;
    if(strstr(p[index].date, ".")==NULL) return -1;

    p[index].timedate.month = atoi(strtok(p[index].date, ".")); 
    p[index].timedate.day = atoi(strtok(NULL, "."));    
    p[index].timedate.year = atoi(strtok(NULL, "."));   

    if(strstr(p[index].time, ":")==NULL) return -1;
    buf=0;

    p[index].timedate.hour = atoi(strtok(p[index].time, ":"));  
    p[index].timedate.minute = atoi(strtok(NULL, ":")); 
    p[index].timedate.second = atoi(strtok(NULL, ":"));

    return 0;

}

Upvotes: 1

LeeNeverGup
LeeNeverGup

Reputation: 1114

You can make a struct:

struct Data{
   Date date;
   Distance distance;
   Time time;
}

Then declare an array of Data and use it like that:

Data arr[5];
arr[0].date = //some date;
arr[0].distane =//some distance
arr[0].time=//some time

Upvotes: 1

Related Questions