If/Else Condition with Characters

Started by
2 comments, last by phil05 20 years, 1 month ago
What I have has no errors, but does a infinite loop. I want it to recognize the "y" the character inputs in a do/while loop in c++.

char abilityAnswer[2];
  do { 
	system("cls");
	cout << "Ability Modifiers:" << endl;
	cout << "------------------\n" << endl;
	cout << "\tStrength: " << Player.STR << "          Intelligence: " << Player.INT << endl;
		cout << "\tDexterity: " << Player.DEX << "     Wisdom: " << Player.WIS << endl;
		cout << "\tConstitution: " << Player.CON << "   Charisma: " << Player.CHA << endl;
	
		cout << "Type ''y'' or ''n'' to continue: ";
		cin.getline(abilityAnswer, 2, ''\n'');
	} while (abilityAnswer != "y");
 
Advertisement
The != operator is not defined for character arrays. Use a std::string, test against a single character (abilityAnswer[0] != ''y'' - note the single quotation), or use strcmp/
I''m extremely new to std::string. Does it need the #include <string> header? And can you fix my code above so I can see it visually? Thanks.
Hmm this happened to work..

while (abilityAnswer[0] != ''y'');

This topic is closed to new replies.

Advertisement