| ndawg201 (18) | |
|
Ok, so I'm given let's say a set of data in a .txt file that reads: width length height area width length height area width length height area How do you just put all the length datas into an array, like ignoring or overwriting everything but length. I had already opened the file and got the data like this, which I believe is correct? int main() { ifstream rectangle; string line; rectangle.open ("rectangle.txt", ifstream::in); if (!rectangle) { return -1; } while (rectangle.good()) { getline(rectangle, line); } rectangle.close(); return 0; } | |
|
Last edited on
|
|
| Stewbond (1842) | |||||||
The easiest way is to use a structure:
Then if you want to input your structure without much hassel:
That function will let you do the following:
| |||||||
|
|
|||||||
| ndawg201 (18) | |
| Yes, I realize that using a struct would be much easier, however, I am not allowed to do that in my assignment which is why I'm completely clueless on how to do this | |
|
|
|
| toum (203) | |||
Maybe something like that:
| |||
|
|
|||
| Stewbond (1842) | |||
In that case use parallel arrays:
| |||
|
|
|||
| toum (203) | |
| He said he wants to ignore everything but length. | |
|
|
|
| ndawg201 (18) | |
| I mean, I don't really know. What we have to do is for the read function, open the file, pull out the four lines at a time till eof, take the 2nd line (length), put that into an array (which is passed by reference) and then return nothing. Then in the main function we have to define the array, call the read function, and when the array is done being built, find the sum of the contents and print out the sum. And we are given this function prototype: void read_data(ifstream& input, int arr[]); to pass the array as a parameter. I am just completely confused, but thanks for trying to help me. | |
|
|
|