Reading in Mixed data from a file using double for loops?

Hello,
First time posting and I'm already asking something from you guys wow not a good first impression. My homework requires me to read in data and do something with the data simple enough. However the data is "not neat" weird so I'll be using a double for loop (or something you geniuses come up with hopefully more efficient creative new for me to learn w/e I'll do anything at this point *been banging my head against the wall for a couple of hours now*)

One of my theories is reading the data into an array and running the loops off of that but I don't know if that will work but yea. Anyways below this I'm going to post the assignment data I need to read.

// Thats the heading which is relevant || irrelevant w/e floats your bubble. What I really want is a way to read this set of data into my program.

Name of the caravan driver The number of donkeys in that caravan
Name of the donkey The number of boxes on that donkey The weight of each box

Here is your data: (copy & paste into your input file) (call your input files DONKEYS)

Driver1 3
Donkey11 4 500 800 1200 450
Donkey12 3 800 700 600
Donkey13 5 120 150 200 500 300
Driver2 2
Donkey21 3 910 220 410
Donkey22 6 130 150 170 120 160 140
Driver3 5
Donkey31 5 120 210 230 330 400
Donkey32 4 800 700 600 500
Donkey33 2 1000 800
Donkey34 1 1750
Donkey35 3 500 1000 400

Outer loop for each driver. A nested loop inside the outer loop for each donkey, and a nested loop inside the nested loop for the box weights.

Two nested for loops inside a while loop should do the trick.

You don't supply any code or indicate what you need to do with the data, so it's hard to give germane suggestions. Here's one way it might be done:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// http://ideone.com/pGVzZM
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

// simulate the file:
std::istringstream in(
R"(Driver1 3
Donkey11 4 500 800 1200 450
Donkey12 3 800 700 600
Donkey13 5 120 150 200 500 300
Driver2 2
Donkey21 3 910 220 410
Donkey22 6 130 150 170 120 160 140
Driver3 5
Donkey31 5 120 210 230 330 400
Donkey32 4 800 700 600 500
Donkey33 2 1000 800
Donkey34 1 1750
Donkey35 3 500 1000 400
)"
);


int main()
{
    using weight_container = std::vector<unsigned>;
    using donkey_type = std::pair<std::string, weight_container>;
    using donkey_container = std::vector<donkey_type>;

    std::map<std::string, donkey_container> driver_map;

    std::string driver_name;
    while (in >> driver_name)
    {
        donkey_container donkeys ;

        unsigned num_donkeys;
        in >> num_donkeys;

        for (unsigned i = 0; i < num_donkeys; ++i)
        {
            donkey_type donkey;
            in >> donkey.first;

            unsigned num_boxes;
            in >> num_boxes;
            donkey.second.resize(num_boxes);

            for (unsigned j = 0; j < num_boxes; ++j)
                in >> donkey.second[j];

            donkeys.emplace_back(std::move(donkey));
        }

        driver_map.emplace(driver_name, donkeys);
    }

    for (auto& item : driver_map)
    {
        auto& donkeys = item.second;

        std::cout << item.first << " has " << donkeys.size() << " donkeys.\n";
        for (auto& donkey : donkeys)
        {
            auto& name = donkey.first;
            auto& weights = donkey.second;

            std::cout << '\t' << name << " has " << weights.size() << " boxes weighing ";
            for (auto weight : weights)
                std::cout << weight << ' ';
            std::cout << '\n';
        }
    }
}
Topic archived. No new replies allowed.