Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store values in a structure array

Tags:

c++

I'm having a problem storing certain processed values in my program into a structure array. These values contain 3 long integers, and I'm trying to store these values as arrays in my structure.

This is a simplified, relevant version of my whole code. Please help me make it work.

#include<iostream>
#define MAX 20

using namespace std;

struct DATA
{
    int id, number;
    float height;
} stuarray [MAX];

int main ()
{
 long int1 =0;      //contains integer values obtained from a file                                       
 long int2 =0;      //contains integer values obtained from a file
 long int3 =0 ;     //contains integer values obtained from a file

 cout<< stuarray[MAX].id << endl;
 cout<< stuarray[MAX].number << endl;
 cout<< stuarray[MAX].height << endl;
 return 0;
}

okay, i'll explain my program. im reading values from a file as a string. after this im supposed to split them into 3 parts and store them in an array. this arrays should refer to the member variables in the structure. i have managed to read the values, split them, and convert all 3 of them into long ints. now i have to store these three values in the structure array. thank you all for ur help.

like image 942
anakin57 Avatar asked Feb 24 '23 15:02

anakin57


1 Answers

To store values in a specific element of stuarray:

stuarray[i].id = something;
stuarray[i].number = somethingElse;
stuarray[i].height = aFloatThisTime;

And note that for an array of MAX entries, legal indexes are in the range [0,MAX).

like image 77
James Kanze Avatar answered Mar 06 '23 18:03

James Kanze