Horrible strings and characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aTsFQe3
    New Member
    • Feb 2008
    • 5

    Horrible strings and characters

    Argh!

    This is what I am trying to do: read words from a file, and when the first and/or second letters of the words changes, store the number of words thus far encountered in the "indexes" integer array, with the indexes based on the first and second letters of the word last read.

    I have had A LOT of trouble with this, and really don't know what I'm doing, and the compiler throws up errors (errors that are related to the code below). I've written the code as it makes sense to me, ignoring pointers and referencing/dereferencing stuff.

    Code:
        int indexes[26][26];
    
        int WordIndex=-1;
        char firstLetter = "a";
        char secondLetter = "a";
        char newFirstLetter = "a";
        char newSecondLetter = "a";
    
        cout << currentWord;
    
        ifstream fin("somefile.txt", ios::in);
            string tempStr;
    
            while (fin >> tempStr) {
                wordIndex ++;
    
                newFirstLetter = tolower(atoi(tempStr[0]));
                newSecondLetter = tolower(atoi(tempStr[1]));
    
                if (newSecondLetter != secondLetter) {
                    indexes[atoi(firstLetter) - 65][atoi(secondLetter) - 65] = wordIndex;
                    secondLetter = newSecondLetter;
                }
                if (newFirstLetter != firstLetter) {
                    indexes[atoi(firstLetter) - 65][atoi(secondLetter) - 65] = wordIndex;
                    firstLetter = newFirstLetter;
                }
        }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    How can you be coding if you don't know what you are doing? I mean, have you worked this out on a piece of paper so you know the steps to follow??

    I see:
    Originally posted by aTsFQe3
    char firstLetter = "a";
    "a" is an array of char and firstletter is a char. That won't compile.

    Originally posted by aTsFQe3
    newFirstLetter = tolower(atoi(te mpStr[0]));
    What are trying to do?? Assume tempStr[0] is an A. An A is 65. That makes atoi(65) into 65. atoi() is a relic C function that is deprecated in C++.

    Originally posted by aTsFQe3
    indexes[atoi(firstLette r) - 65][atoi(secondLett er) - 65] = wordIndex;
    indexes is a two-dimensional array that is never initialized.

    Plus, if firstLetter is 'a', which is 97 then 97-65 = 32 but your array has dimensions of 26.

    I seriously suggest you write your processing steps on paper and then process the file using the steps by hand. Once you are certain your steps work, then start coding.

    Comment

    Working...