-3
cout << key;    
cout << " " << temp_char << " - " << key_char << " " << (temp_char-key_char) << endl;

enter image description here

    /*
 * Function: StringToInteger
 * Usage: n = StringToInteger(s);
 */

int StringToInteger(string str)
{
    int returnVal;
    istringstream result(str);
    string rest;

    if ((result >> returnVal).fail()) {
    Error("Not a valid number!");
    }
    result >> rest;
    if (rest != "") {
    Error("Extra characters not allowed");
    }
    return returnVal;
}

void Lexicon::buildMapFromFile(string filename )  //map
{
    ifstream file;
    file.open(filename.c_str(), ifstream::binary);
    string mem, key;
    string wow;
    unsigned int x = 0;

    cout << endl;
    string temp;

    while(true) {
        getline(file, wow);
        if (file.fail()) break; //boilerplate check for error
        while (x < wow.length() ) {
            if (wow[x] == ',') { //look for csv deliniator
                key = mem;
                mem.clear();
                x++; //step over ','
            } else 
                mem += wow[x++];
        }

        char key_char = (char)StringToInteger(key); //cast integer into a char
        key = key_char; //change from char to string for the map

        cout << key_char << " is " << (temp[0]-key[0]) << " " << mem << " " << endl;

        tokenToChar_map[key] = mem; //char to string
        charToToken_map[mem] = key; //string to char
        mem.clear(); //reset memory
        x = 0;//reset index
        temp = key;
    }
    //printf("%d\n", tokenToChar_map.size());
    file.close();
}

A section of the input file:

103,089
104,090
105,091
106,092
107,093
108,094
109,095
110,096
111,097
112,098
113,099
114,100
115,101
116,102
117,103
118,104
119,105
120,106
121,107
122,108
123,109
124,110
125,111
126,112
127,113
128,114
129,115
130,116
131,117
132,118
133,119
134,120
135,121
136,122
137,123
138,124
139,125
140,126
141,127
142,128
143,129
144,130
145,131
146,132
147,133
148,134
149,135
150,136
151,137
152,138
180,RW4
181,R1
182,RW1
183,NBE
184,RW3
185,NB0
186,D1
187,EPS
188,C0
189,0M0
190,C1
191,RWY
192,D2
193,SB0
194,SBE
195,R2
196,RW2
197,RW5
210,0A1
211,0B2
212,0B3
213,0B4
214,0A5
250,*
251,?
252,z
253,test
254,test
255,test

Integers 7,8,9,10,11,12, and 13 do not have an associated char

Integers 114 <-255>, 180 <28>, 210 <13>, and 250 <36> are more than 1 away from their predecessor integer

what is going on, why are these not sequential?

Dave Rager
  • 8,002
  • 3
  • 33
  • 52
forest.peterson
  • 755
  • 2
  • 13
  • 30
  • 4
    There is not telling what's going on without seeing the implementation of `StringToInteger()`, the value of `temp_char` and your explanation of what the hell you are trying to accomplish. – StoryTeller - Unslander Monica Jan 18 '13 at 20:33
  • #StoryTeller (1) added the rest of the code (2) goal: a set of strings mapped with a single char keys. – forest.peterson Jan 18 '13 at 20:44
  • 114 is not more than one away from its predecessor integer, 113. –  Jan 18 '13 at 20:46
  • #Sancho when temp_char-key_char is 113-114 I get 255 - I posted a screenshot of the output – forest.peterson Jan 18 '13 at 20:57
  • 113-114 = 255 in unsigned integer arithmetic. –  Jan 18 '13 at 20:58
  • from a laypersons perspective, if I read in a sequence of integers then cast them to chars - then they should also be a sequence - and they almost are with the exceptions I gave. why are they exceptions; did I do something wrong or is that just how c++ works and I need to understand it better. – forest.peterson Jan 18 '13 at 21:02
  • A sequence of integers cast to chars _does_ result in a sequence of chars. There are no exceptions. You haven't shown any exceptions. –  Jan 18 '13 at 21:04
  • The implementation of `StringToInteger()` still isn't posted. What are the input values? – Chad Jan 18 '13 at 21:08
  • #Sancho, hey... I am confused now - are we talking about the same thing here. If I subtract 114 from 113 the result is 255, yes? And, if I subtract 180 from 179 the result is -28? So that seems like something is not right? – forest.peterson Jan 18 '13 at 21:10
  • That is not right no. 114-113 is -1, 180-197 is -1. Print out your full values (temp[0] and key[0]) to see what you're actually subtracting. As stated above, if the types are `unsigned char` then '255' has the same binary representation as `signed char` '-1'. – Chad Jan 18 '13 at 21:11
  • did you look at the output screen shot? It looks like -28 to me? Why does the rest of the sequence have -1 consistently? – forest.peterson Jan 18 '13 at 21:12
  • Screenshots don't prove that 179-180=-28. –  Jan 18 '13 at 21:13
  • See this: http://ideone.com/KcQMOm – Chad Jan 18 '13 at 21:13
  • #Chad yes I understand what you are saying and I think we have miscommunication here; 114-113 is -1. Try, (char)StringToInteger(114) - (char)StringToInteger(113) is 225. And (char)StringToInteger(113) - (char)StringToInteger(112) is -1, that is inconsistent – forest.peterson Jan 18 '13 at 21:19
  • What does (char)StringToInteger("114") return? What does (char)StringToInteger("113") return? –  Jan 18 '13 at 21:22
  • 1
    Then there is a bug in your `StringToInteger()` function, or your input is corrupt. According to http://ideone.com/PhFmXi it appears to be the latter. – Chad Jan 18 '13 at 21:25
  • input is a .csv ANSI encoding file, difficult to corrupt that... StringToInteger has been used in the universities CS course for the past 15years or something, it seems reliable - someone would have fixed any bugs by now – forest.peterson Jan 18 '13 at 21:32
  • What is your input data, exactly? –  Jan 18 '13 at 21:35
  • posted a sample to the question - it is just a csv file with 2 columns, column 1 is 0 to 255 and column 2 has the string that will be mapped. Opened in text it is 255 rows with two strings separated by a comma. – forest.peterson Jan 18 '13 at 21:37
  • And what is your expected output? –  Jan 18 '13 at 21:39
  • something sequential ;) I posted a new image that has the imput and output as the difference between n and n+1; it will be a bit before my next post – forest.peterson Jan 18 '13 at 21:43
  • What is your expect output, exactly? Please write it down and post it. –  Jan 18 '13 at 21:44
  • 1
    What are the types of `temp_char` and `key_char`? `char` or `unsigned char`? – Chad Jan 18 '13 at 21:48
  • #Chad I don't know the difference between char and unsigned char but I will try the unsigned, guessing they are char now. #Sancho, it was not a joke, I am not concerned with the output only that whatever it is that it is sequential. – forest.peterson Jan 18 '13 at 21:55
  • #Chad changed 'char' to 'unsigned char' and that resolved 114, but 180, 210, and 250 still are not sequential – forest.peterson Jan 18 '13 at 22:03
  • #Sancho the expected output is a sequential sequence of 256 chars; now unsigned char – forest.peterson Jan 18 '13 at 22:07
  • What is wrong with the output associated with 180, 210, and 250? –  Jan 18 '13 at 23:29

1 Answers1

1

Look at your input data:

[...]
152,138
180,RW4
[...]
197,RW5
210,0A1
[...]
214,0A5
250,*
[...]

What is wrong with (char)StringToInteger("152") - (char)StringToInteger("180") producing -28?

If you can't tell us what you specifically what you expect your program to output, we can't help you with this question.

  • yes, yes, yes... I should be thrown into the brain dead pile; I re-sequenced my input file for a continuous 0 to 255, not sure how the break from 152 to 180 was introduced but it was and I did not notice it – forest.peterson Jan 23 '13 at 22:46
  • I am reviewing my most downvoted questions to learn from past mistakes. This question should be deleted - I don't think this question will ever be helpful to anyone. Other than a lesson to check your work before asking on stack. – forest.peterson Jul 07 '14 at 20:20