String comparison help?

Dec 4, 2014 at 2:06am
Hi all,

Long story short I'm creating a program that takes user input in the form of a string and tests to see if it matches a word. Each correct word will increase their score by one. Here is a portion of the code that is not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 ...
else if(s == 32) {  
     if(!currentLetter.empty()) {
          currentLetter.erase(currentLetter.length() - 2, currentLetter.length() - 1); 
     }
							
     if(currentLetter.compare(oWord) == 0) {
          currentLetter = "";
          score[a]++;
          oWord = getRandWord();
          nWord = scramble(oWord);
     }
					    	
     else {
          currentLetter = "";
          cout << endl << "Incorrect. Please try again." << endl;
     }
}


To me, this looks like it should do a very simple task as intended-take a String, compare it to another, and reset the word if they match or output incorrect if not. But, I'm not sure if there is some quirk in C++ with Strings, because this code always outputs
Incorrect. Please try again.
and the score never increases. I also tested this by literally setting the strings equal in the code, which still resulted in it not doing what it's supposed to. Any ideas on what I could be doing wrong?
Dec 4, 2014 at 2:10am
Could you start by explaining what your code above is intended to be doing? For example, what is the type of currentLetter? Its name suggests it is a character but its usage suggests it is a std::string.
Dec 4, 2014 at 4:11am
I'm sorry, currentLetter started as a char but I later realized it had to be changed to a string and didn't want to change its' name in every place.

s = string (The last letter typed, not necessary for this problem)
currentLetter = string (The current WORD (not letter) that the user is inputting)
score[] = int[] (The player's score)
oWord, nWord = strings (Old word/new word. Not really necessary for this, all you need to know is that they are strings)

Any other info you need?
Dec 4, 2014 at 4:50am
If s is a std::string, why are you comparing it for equality to the integer 32? That should be a compile error.
Topic archived. No new replies allowed.