Handling input to structure elements using an array

I am doing an assignment that includes structures.

You have to create a structure with three variables and declare 3 instances of this structure. The program will then ask the user to enter 9 pieces of information. I don't like typing cout and cin unnecessarily (9 consecutive times?), So I thought I could use loops to process input like I did before with arrays. I tried this but no success yet. Am I on to something with this?

struct Randomstruct {
  int var1, var2, var3;
}

int main() {
  Randomstruct struct1, struct2, struct3;
  for(int i = 1; i<=3; i++) {
    for(int j = 1; j<=3; j++) {
      cout << "Enter data for var" << j << " in struct" << struct(i) << ": ";
      cin struct(i).var(i);
      }
    }
  }
}

      

I'm really wondering how I can work on the construct (i) .var (i). Is it possible?

+2


source to share


3 answers


Just customize the slides to your syntax:



struct Randomstruct
{
    int var[3];  // Use an array to index things by integer.
}; // You forgot this semicoln

int main()
{
    Randomstruct struct[3];  // Again another array

    // Nearly all programming languages used 0 based arrays.
    // So loop from  0 -> (array size -1) ie use smaller than
    for(int i = 0; i<3; i++)
    {
       // Again user 0 based index.
       for(int j = 0; j<3; j++)
       {
           cout << "Enter data for var" << j << " in struct" << struct(i) << ": ";

           // Use the >> operator to get stuff from the stdin.
           // Arrays are indexed via the operator [<index>]
           cin >> struct[i].var[j];
       }
    }
}

      

+2


source


Not

Line (i) .var (i) won't work.

You can create vectors / arrays, but as you wrote the problem it sounds not allowed.

Example



struct Randomstruct {
    int var[3];
};

      

... and access with

struct1.var[i]

      

0


source


You can do this, but it seems a little silly:

#include <iostream>

typedef struct _random_struct
{
    int var1;
    int var2;
    int var3;
} random_struct;

typedef struct _other_struct
{
    int var[3];
} other_struct;

union being_lazy
{
    random_struct r;
    other_struct o;
};


int main()
{
    random_struct st[3];
    being_lazy l[3];
    for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 3; ++j)
        {
            std::cout << "Enter data for var" << j << " in struct" << i << ": ";
            std::cin >> l[i].o.var[j];
        }
    }

    for(int i = 0; i < 3; ++i)
    {
        st[i] = l[i].r;
    }

    for(int i = 0; i < 3; i++)
    {
        std::cout << "struct" << i << ".var1 == " << st[i].var1 << std::endl;
        std::cout << "struct" << i << ".var2 == " << st[i].var2 << std::endl;
        std::cout << "struct" << i << ".var3 == " << st[i].var3 << std::endl;
    }
}

      

You need to make sure that both random_struct and other_struct have the same structure in memory, or bad things could happen.

Indeed, if you access individual fields, it is too painful, you either want to just use arrays or change your mind about your code.

0


source







All Articles