2

I'm doing an assignment that involves structs.

You are to make a struct with three variables and declare 3 instances of that struct. The program will then ask for the user to input 9 pieces of information. I don't like having to type cout and cin unnecessarily (9 sequential times?), so I was thinking I could use loops to handle the input, like I've done with arrays earlier. I tried it out, but have been unsuccessful so far. 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 make the struct(i).var(i) thing work. Is it even possible?

2
  • You need to give some background on what C++ features can be used to solve this problem. Since this is an assignment, I would assume that only certain topics have been covered. For instance, are you allowed to use functions? Are you allowed to use pointers to structures? Commented Sep 18, 2009 at 21:10
  • You're on the right track, but your syntax is wrong. Commented Sep 18, 2009 at 21:13

3 Answers 3

2

Just a sligh tweak 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];
       }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Doh! You've given the game away! Use arrays. OK, yes, we're here to help the OP. +1 for correct answer.
0

No

The struct(i).var(i) will not work.

You could make vectors/arrays but as you have written the task it sounds it's not allowed.

Example

struct Randomstruct {
    int var[3];
};

...and access with

struct1.var[i]

1 Comment

Which makes me wonder if a union, pointer majicks, or member functions are allowed.
0

You could 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'd need to make sure that both random_struct and other_struct have the same structure in memory, or bad things might happen.

Really though, if you accessing individual fields is too painful, you either want to just directly use arrays, or re-think your code.

Comments

Your Answer

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