2

I can't figure out why my pull_data function crashes when it is call I narrowed it down to the "myMap[word]++" lines. However if I understand it correctly it should work. Like if the element with key "word" doesn't exist, the element will be created and value initialized.

My first variation would pull 2 different files and save the data to the 2 different maps with this.

if (file.is_open()) { while (file >> word) { myMap[word]++; } file.close(); } else { cout << "Unable to open file."; } 

But instead of pulling from 2 different files I am trying to make it read one that has all the data I need separated with a | to indicate if i am reading from the on side or the off side.

#include <iostream> #include <fstream> #include <map> #include <string> using namespace std; void pull_data(map<string, float>& myMap1, map<string, float>& myMap2); void push_data(map<string, float> myMap1, map<string, float> myMap2); void sync_data(map<string, float>& myMap1, map<string, float>& myMap2); int main() { map<string, float> offData; map<string, float> onData; pull_data(offData, onData); sync_data(offData, onData); push_data(offData, onData); system ("PAUSE"); return 0; } void pull_data(map<string, float>& myMap1, map<string, float>& myMap2) { string word; bool onOff = false; ifstream file("Data.txt"); if (file.is_open()) { while (file >> word) { if(word == "|" && onOff){ onOff = false; }else if(word == "|"){ onOff = true; } if(onOff){ myMap2[word]++; }else{ myMap1[word]++; } } file.close(); } else { cout << "Unable to open file."; } } void push_data(map<string, float> myMap1, map<string, float> myMap2){} void sync_data(map<string, float>& myMap1, map<string, float>& myMap2){} 

Data.txt example

362 364 | 112 304 122 124 | 364 304 901 116 | 351 303 112 | 362 364 311 | 351 612 400 484 303 326 | 
2
  • You are using the operator "++" which should increase the value of data that is returned with the Key [word], but because you may have a null data the operator ++ crach. The Map create and initialize the value with the syntaxis myMap[word] = x (using the operator "=") Commented Mar 16, 2017 at 5:48
  • You can't compare strings with ==, instead of you should use std::strcmp Commented Mar 16, 2017 at 16:44

1 Answer 1

2

It is because you are using a mapped data type that doesn't meet the requirements to be CopyConstructible and DefaultConstructible, float is a primitive data type.

That is the reason that you have a Zero reference and it crashed when you are use the operator "++".

Check it:

http://en.cppreference.com/w/cpp/container/map/operator_at

http://en.cppreference.com/w/cpp/concept/CopyConstructible

Sign up to request clarification or add additional context in comments.

7 Comments

So if I am understanding it correctly if i don't use the & and make the function return the maps it will work?
Ok, @Marzdor, what is the issue? the reason that you program crashed with myMapp[word]++, or another issue?
One last question, what is the compiler that you are using?
I am trying to figure out how to get it to work is all. Since originally it worked with a map using myMapp[word]++ with both int and float as the value and a string for the key but when I started to try and split the data from the file into two different maps it started to crash. Also I am using "TDM-GCC 4.9.2 64-bit Release"
Do you have some sample of "Data.txt"?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.