简体   繁体   中英

How to input strings that start with special characters

I am trying to input all words into a map in C++, but the program freezes only when a word starts with a special character. The code works when there is a special character at the end.

I haven't been able to find proper documentation for the >> operator in C++, or been able to google my problem properly.

//Map values and find max value
//The code works for all words except the ones that start with special characters

    while(myFile >> cWord){

        //put the characters into a string

        //DEBUG: cout << "real word: " << cWord << " | ";

                cWord = stripWord(cWord);

                //delete common words before they're in the system

        if(cWord == "a" ||
           cWord == "an" ||
           cWord == "and" ||
           cWord == "in" ||
           cWord == "is" ||
           cWord == "it" ||
           cWord == "the"){
            continue;
        }

        if (wordMap.count(cWord) == 0){
            wordMap.insert({cWord, 1});
        }
        else{
            wordMap[cWord]++;
            if(wordMap[cWord] > maxWordRep){
                maxWordRep = wordMap[cWord];
            }
        }
        //DEBUG: cout << cWord << " | " << wordMap[cWord] << endl;

    }

I expect the debug to print all of the words and then follow the rest of the code, but the code stops running and freezes at the exact line

while(myFile >> cWord)

My input are long song lyric files. Here are the words that program froze on:

Counting Stars: Completed.

I can make your hands clap: Stuck at 'cause

One more night: Stuck at (yeah

Run on test (a file to test combined words): Completed

Safety Dance: Stuck at 'em

Shake it off: Stuck at "oh

There are a bunch of others that follow the same pattern. Always 1 or more special characters in front. You can try on your own, and cin >> string will get stuck when you input a string with a special character in front.

Final Edit: The bug was in the stripWord function, so this question is just a bad question.

This code:

while(myFile >> cWord)

The >> operator returns a std::istream&, and so the operator called here is: http://www.cplusplus.com/reference/ios/ios/operator_bool/

Notice it says it is looking for a failbit to be set on the istream? Reading to the end of a file is NOT an error, so really you should be checking to see if you've hit the end of a file, eg

while(!myFile.eof())
{
  myFile >> cWord;

  /* snip */
}

If the file has a bunch of pointless whitespace at the end of the file, you might end up reading an empty string at the end of the file, which should also be taken care of, eg

while(!myFile.eof())
{
  myFile >> cWord;

  if(cWord.empty()) break;

  /* snip */
}

The rest of the code (assuming it's bug free) should be fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM