2

I'm trying to implement a structure in C++ 14. I have made a structure that has 3 int values

struct mystruct{
    int a;
    int b;
    int c;
};

In my main function, I'm initializing a structure array in the following way:

int main(){
    mystruct X[] = {{1,2,3}, {4,5,6}};
    .
    .
}

I'll pass this array to a function where I'll perform some operations on it. That function could be like:

int myfunc(mystruct X[]){
    //do something
}

How can I take the values for this array as user input using cin, instead of hardcoding them (perhaps using objects)? I'm not sure how to go about this.

Edit: I was hoping this could somehow be achieved using objects

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
decukor
  • 23
  • 4
  • 1
    That depends entirely on how the input should be structured: one value per line, all three values for one object on a single line, separated by whitespace, or perhaps something else. First, you need to figure out what's your expected input format, then you should find a complete, detailed information of how to process input in your C++ book. This is too broad. – Sam Varshavchik Feb 11 '18 at 16:54
  • Your code and your question looks irrelevant. – llllllllll Feb 11 '18 at 16:54
  • Use a `std::vector` instead of a raw array. –  Feb 11 '18 at 16:54
  • 1
    Possible duplicate of [initializing structs using user-input information](https://stackoverflow.com/questions/2710539/initializing-structs-using-user-input-information) – codebender Feb 11 '18 at 16:56
  • I want my input to be an array with elements of type mystruct – decukor Feb 11 '18 at 16:57
  • 2
    "input to be an array with elements of type mystruct" is not a logically meaningful sentence, in C++. "Input" is not an array of anything, or of any `mystruct`. It's a character stream. – Sam Varshavchik Feb 11 '18 at 16:58
  • @decukor _"I want my input to be an array ..."_ You can always access the underlying _array_ of a `std::vector`, see `data()`. –  Feb 11 '18 at 16:59
  • We know where you want your input to go. But what do you want the user experience to be? What do you want your user to have to do to provide these values? – Galik Feb 11 '18 at 17:12
  • I should have worded it clearly. I want to create an array after reading input (character stream) from user. So after 1,2,3,4,5,6 has been entered, I would like to form X like I have hardcoded in my example – decukor Feb 11 '18 at 17:12
  • 1
    Do you want the user to type in one value after another and never stop? Do you want your user to choose how many array elements they wish to enter before typing the data? Will the user enter three values per line or one value on each line? – Galik Feb 11 '18 at 17:14
  • You can write a template that knows the underlying size of the array, but I suspect you probably need the user to specify the size as an extra input, so vector if the best way forward. – Gem Taylor Feb 12 '18 at 19:07

1 Answers1

7

You could implement an input operator for your struct. Something like this would work:

std::istream& operator>>(std::istream& is, mystruct& st)
{
    return is >> st.a >> st.b >> st.c;
}

Now you can read in from a mystruct like this:

mystruct t;
std::cin >> t;

(Note that the function above doesn't handle errors)

Now adding these new structs to an array could be done very simply through the use of a loop. (I would recommend the use of std::vector here).

Here is an example that uses std::vector:

std::vector<mystruct> arr;

for (mystruct t; std::cin >> t;)
{
    arr.push_back(t);
}

myfunc(arr.data()); // Or you could change the signature of the 
                    // function to accept a vector
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • @TheDude I'm fine with a vector as well – decukor Feb 11 '18 at 17:15
  • Can you please elaborate `for (mystruct t; std::cin >> t;)`. I have seen such syntax for the first time – decukor Feb 11 '18 at 17:27
  • 1
    @decukor Since now you have defined `operator>>` for `mystruct`, you can read it in using a loop. Basically, each iteration, this creates a new `mystruct`, and then you enter its values. To terminate the loop, you would enter an EOF, or the input has to fail somehow. This is equivalent to: `mystruct t; while (std::cin >> t) { arr.push_back(t); }`. – Arnav Borborah Feb 11 '18 at 17:43
  • @Arnav Borborah. This is new for me. Thank for the help – decukor Feb 11 '18 at 17:48