2015-11-05 40 views
-3

我需要检查,如果他们输入高兴,伤心或生气时提示: 输入一个心情(快乐,悲伤,生气) 这是我为它至今 计数代码= 0; 一段时间(数< 1){C++检查,如果用户输入一个有效的答案

cout << ", How are you feeling today? (happy,sad,mad)" << endl; 

    cin >> mood; 

    if (mood == sad){ 

     cout << "im sorry you're feeling sad" << endl; 

      count++; 
    } 

    else if (mood == happy) { 

     cout << "I'm glad you're feeling happy!" << endl; 

      count++; 
    } 

    else if (mood == mad){ 
     cout << "Don't be angry, there's so many happy things in life!" << endl; 
    } 
    else{ 

     cout << " invalid input " << endl; 

     count = 0; 
    } 

    } 

,但如果我把高兴,伤心或生气它只是在无效的输入。 我也试过为他们声明字符串变量,如 string happy = happy; string sad = sad; string mad = mad;

+1

看起来您正在混淆变量名和文本字符串。他们可能是同一个词,但它们是无关的。 (例如,除非mad是一个值为“mad”的std :: string)。 –

+1

这是如何编译的?发布一个MCVE。 –

+0

你应该使用双引号来表示一个字符串。做到这一点,一切都应该在世界上。直到下一个错误。 – user4581301

回答

-4

两件事情:

  1. 确保您初始化您的字符串的正确

    string happy = "happy"; 
    
  2. 尝试使用string.compare()而不是==

    mood.compare(happy); 
    

有关更多详细信息,请参见:http://www.cplusplus.com/reference/string/string/compare/

+0

唯一错误的是'String'应该是小写字母。否则上面的代码肯定是C++ – jsherman256

+0

'=='很好......正如我所说的,这不是Java。 –

相关问题