0

I have a struct that reads data from the user:

typedef struct
{
    int seconds;
} Time;

typedef struct
{
    Time time;
    double distance;
 } Training;


 Training input;

 scanf("%d %lf", input.time.seconds, input.distance);

This scanf will be looped and the user can input different data every time, I want to store this data in an array for later use. I THINK I want something like arr[0].seconds and arr[0].distance.

I tried to store the entered data in an array but it didn't really work at all...

Training data[10];

data[10].seconds = input.time.seconds;
data[10].distance = input.distance;

The data will wipe when the program closes and that's how I like it to be. So I want it to be stored in an array, no files or databases!

1
  • 1
    When you declare Training data[10];, you can only access elements 0..9. Commented Nov 12, 2013 at 21:55

2 Answers 2

1

data[10] is of type Training which does not have seconds. Try:

Training data[10];

data[10].time.seconds = input.time.seconds;
data[10].distance = input.distance;
Sign up to request clarification or add additional context in comments.

1 Comment

You can't use subscript 10 on an array Training data[10]; because the valid subscripts run from 0 to 9.
0

Two things, the training set has a 'time' field to access the seconds, and if you define an array of 10 element, you can only use data[0] to data[9], data[10] will be past the end of the array.

Training data[10];

data[0].time.seconds = input.time.seconds;
data[0].distance = input.distance;

....

data[9].time.seconds = input.time.seconds;
data[9].distance = input.distance;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.