-2

Below is the file I need to extract data from.

    auto1
    engine: gasoline
    max_speed: 250
    engine_cc: 1980
    avg_consumption_urban: 11
    avg_speed_urban: 50
    avg_consumption: 8
    avg_speed: 100
    auto2
    engine: diesel
    max_speed: 230
    engine_cc: 1600
    avg_consumption_urban: 9
    avg_speed_urban: 50
    avg_consumption: 6
    avg_speed: 80
    auto3
    engine: hybrid
    max_speed: 190
    engine_cc: 1450
    avg_consumption_urban: 7
    avg_speed_urban: 50
    avg_consumption: 4
    avg_speed: 90

I need to create three auto objects, auto1, auto2, auto3.

The code I have so far:

[my code] http://pastebin.com/2TMhQX9b

I need to modify this method to skip over the "engine:", "max_speed:" ... etc and get the information that is after those attribute titles and insert the respective value in the corresponding attribute.

Also I need to find a way for the compiler to know for example when creating auto2 to get the information for auto2 and not auto1 from the file

    friend ifstream& operator>>(ifstream& in, Auto &a)
        {
            delete[]a.engine;
            a.engine = new char[10];
            in >> a.engine;
            in >> a.max_speed;
            in >> a.engine_cc;
            in >> a.avg_consumption_urban;
            in >> a.avg_speed_urban;
            in >> a.avg_consumption;
            in >> a.avg_speed;
            return in;
        }

This question is different from my previous question. Here I need to find out how I can get my data from the file and insert it into multiple variables.

Alexandru Nutu
  • 431
  • 2
  • 5
  • 11
  • Why are you using a dynamically allocated array for `a.engine` instead of e,g, `std::string`? Or at least if you're using a size fixed at time of compilation use an array. – Some programmer dude Dec 18 '15 at 21:27
  • @Alexandru Didn't [my answer](http://stackoverflow.com/a/34345571/3552770) help? – LogicStuff Dec 18 '15 at 21:28
  • I kept reading your answer. I'm still a newbie and I cannot implement what you wrote :) I tried and tried. Just a few minutes ago I realized that I need to modify my code in the class and not main, in the method above. – Alexandru Nutu Dec 18 '15 at 21:33
  • You should have commented then. – LogicStuff Dec 18 '15 at 21:34
  • I'm searching and searching on google, but I cannot find any example that is similar to mine, or so I think. – Alexandru Nutu Dec 18 '15 at 21:34
  • I thought that I didn't post the right question, and this is why I posted a new question – Alexandru Nutu Dec 18 '15 at 21:35
  • @AlexandruNutu You can always *edit a question* to include more details. But if it will change the question too much and you already have answers it might actually be a good idea to post a new question, but then link to the original question and also add some text describing what makes the new question different from the old. – Some programmer dude Dec 18 '15 at 21:38
  • I already provided an answer to this question. You should study all of the answers to your previous question. – Thomas Matthews Dec 18 '15 at 22:11
  • @ThomasMatthews I did study your answer. I could not do what you told me. I'm a noob in c++ but also stackoverflow. In the previous question I did not mention I wanted the values to go in a variable from my friend function. – Alexandru Nutu Dec 18 '15 at 22:27

1 Answers1

0

One way to solve this is to read one line (for e.g. auto1 etc.), then use your custom operator>> function to read the actual data for the object, probably much like you do already.

The input operator function needs to be changed a bit from what you have now though. You can continue on a similar way like you do know, but you first need to read every lines "tag" (e.g. engine: or max_speed:), the simplest is to do it something like this:

std::string dummy;
in >> dummy >> a.engine;
in >> dummy >> a.max_speed;

I would probably not do like that, because what if some other program writes this file in the wrong order from what you currently read it? Instead I would probably, again, read one line at a time, and split the input into two sub-strings on the ':' character, the "tag" or "key, and the value. Then you could check the key to see what value to assign to in the structure.

For converting strings to an integer, there is a nice function to help you.

For example:

if (key == "max_speed")
    a.max_speed = std::stoi(data);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank you! :) But now I have two more questions. In my friend ifstream method do I declare string key; ? And 'data', what is that? the file? – Alexandru Nutu Dec 18 '15 at 21:59
  • I'm trying to implement what you said. But I'm having no luck. I need to read some more. In my friend method I added string s; getline(in, s); But I don't know what the s will store, just the first line? And then the splitting using s.substr(), again I don't know what I have to insert as paramethers (i know the first one is the position and the second is how many from that position) – Alexandru Nutu Dec 18 '15 at 22:53
  • @AlexandruNutu You need to [*find*](http://en.cppreference.com/w/cpp/string/basic_string/find) the position of the `':'`. – Some programmer dude Dec 18 '15 at 22:55