【发布时间】:2015-03-14 00:37:38
【问题描述】:
我正在尝试比较一个琐事程序的两个字符串,一个由用户输入,另一个从节点访问。比较在 IF 语句中,并且总是返回 false。以下是用于此功能的代码。变量 userAnswer 和 answer 都是字符串类型。
cout << "Question: " << cur_ptr->question << endl;
cout << "Answer: ";
getline(cin, userAnswer);
if (userAnswer == cur_ptr->answer) {
cout << "Your answer is correct. You receive " << cur_ptr->points << " points." << endl;
totalPoints += cur_ptr->points;
}
else {
cout << "Your answer is wrong. The correct answer is: " << cur_ptr->answer << endl;
}
cout << "Your total points: " << totalPoints << endl << endl;
cur_ptr = cur_ptr->next;
每当我的程序运行时,它都会生成这样的输出
Question: How long was the shortest war on Record? (Hint: how many minutes)?
Answer: 38
Your answer is wrong. The correct answer is: 38
Your total points: 0
【问题讨论】:
-
我无法重现这个。也许字符串包含一些空格或奇怪的字符? ideone.com/xnYHYg
标签: c++ string if-statement compare