Hello, I'm currently trying to figure out how I could go about taking a getline putting the one line into an array and then putting the first value aka array[o] into a command variable and then all the next values on the line into seperate variables of integer for key value and string for assosciated data that are linked to each other.

I figure I would use https://en.wikipedia.org/wiki/Hash_map_%28C%2B%2B%29 #include <unordered_map> from this page.

I can't seem to figure out how I would go about this.

    while(!inputFile.eof() ) {
        getline (inputFile, line);
        array[i] = line; 
        i++;
    }


    if(array[i] == "B"){
    // do function for B with data (key, assosciateData) 
    }

Is my code so far. I take the input and put it into the array for however many lines where i = 0. Then I was thinking I would make an if else statement where it chooses the command variable i.e. if "B" do this function if "C" do this function

My input looks like this. "C 129.2 hi 69.8 This 38.9 World"

so I'm trying to make it where "C" = Command, 129.2 = key, hi = assosciatedData. As shown below:

    std::unordered_map<std::string, int> junk;
    junk["hi"] = 129.2;
    junk["This"] = 69.8;

My main problem is there are multiple lines on the input file that go into array[i], so I can't just choose to do the first line and choose the first letter as the command (at least I don't know how to") cause when I do array[0] I would just get the whole line "C 129.2 hi 69.8 This 38.9 World", and I'm a bit at a loss of how to seperate them. Thanks guys.

Recommended Answers

All 8 Replies

Could you not just read the values inside a char array and then tokenise it?

It's very unclear, I don't understand really what you want to do. Is it possible to show the type of data in the file you're trying to process?

That's basically what i'm asking. I'm not sure how to tokenise the string array. Even if I made it char, I'm still unsure of how to seperate the first letter from the second double(key) + third string word (data) seperate from fourth double + fifth word and so on. By the + I mean that they both go together in the map. I just have never parsed an array this way, and I can't seem to find documentation on it.

So, just to be clear: You want to tokenise the value, and then place them inside a std::map, which contains:

["c"] = 129.2
["b"] = 131.1

and then acess from this?

Also, can you give a small snippet of what the text file looks like, i.e. a few lines =)

Also, how do you plan on setting this data out? I.e.

If you took the char array of vals as: char s[] = "C 129.2 hi 69.8 This 38.9 World"; and tokenising would become:

Token: 0 -> C
Token: 1 -> 129.2
Token: 2 -> hi
Token: 3 -> 69.8
Token: 4 -> This
Token: 5 -> 38.9
Token: 6 -> World

How would you potentially store this as a std::map since:

std::map<char, std::string> vals {

     myMap['C'] = "129.2 hi 69.8 This 38.9 World"; 

};`

Is a possibility, however, if 129.2 hi 69.8 This 38.9 World are paramaters (for some function) would you not need to tokenise these seperately as well? Are you sure using a std::map is the best way to go for this?

Sure, here is the input file:

B 128.9 hello 2918.1 hi 2938.2 world
H 12.2 India 218.6 Lost 29.9 Place
C 28.3 Watch 28.4 Found 293.9 Palace
...
...
...

this is for multiple lines. so I take the input in from this while loop(bit redone from earlier):

    while ( getline (inputFile,line) ) {
        array[i] = line; 
        i++;
    }

Now i'm trying to basically seperate the line. where I make "B" in the first line the command variable. Then I make 128.9 and hello go into seperate variable key = 128.9 and data = "hello", put these into a map which I think once I understand seperating them I can do just fine. then make a different variable for key2 = 218.6 and data2 = "hi". And so on until the end of the line. then do this for the next lines until input.eof

Hopefully this helps. Sorry, i'm trying to explain it to the best of my abilities.

Well for this project I need to use the data and key as function parameters for a priorityqueue made with a hash. I need to make sure the key/data are assosciated with each other.

Oh, one more thing. The "B" command would not be a parameter other than for an initial if else statement which calls something that looks like this function(key,data).

Babble, babble, babble. Please, just tell us precisely what you are trying to accomplish! I do this stuff for a living - reading big long lines of log data, and then parsing that into stuff that we can store in a database (mysql or hadoop) and then apply analytical algorithms to them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.