對於每一行,我一個字符串比較另一個字符串,我不斷收到錯誤:爲什麼在比較字符串時出現錯誤「不匹配'operator ==''?
No match for 'operator=='
代碼:
#include <iostream>
#include <conio.h>
#include <string>
#include <curses.h>
using namespace std;
int word_number, state, i, x, n;
bool correct[25], playing = true, set_complete, valid, match;
string word, word_list[25], head, upper, body, lower, blanks, input, guessed, alphabet = "abcdefghijklmnopqrstuvwxyz";
size_t found;
void play(), initialize(), print(), validate(), progress();
int main()
{
initscr();
while (playing)
{
play();
printw("Would you like to continue playing?");
input = getch();
if (input == "n"||input == "N")
{
playing = false;
}
}
endwin();
}
void initialize()
{
if (word_number != 0)
{
word_number++;
}
if (word_number == 25)
{
set_complete = true;
}
state = 0;
head = "";
upper = "";
body = "";
lower = "";
blanks = "";
guessed = "";
word = word_list[word_number];
for (i = 0; i<strlen(word.c_str()); i++)
{
blanks += "_";
}
}
void play()
{
initialize();
while(!set_complete)
{
start:
input = getch();
validate();
}
}
void validate()
{
for (i = 0, valid = false; i <= 25; i++)
{
if (input == alphabet[i])
{
valid = true;
}
}
if (!valid)
{
goto start;
}
for (i = 0, match = false; i<strlen(guessed.c_str()); i++)
{
if (guessed[i] == input)
{
match = true;
}
}
if (!match)
{
guessed += input;
}
for (i = 0, match = false; i<strlen(word.c_str()); i++)
{
if (input == word[i])
{
blanks[i] = input;
match = true;
}
}
if (!match)
{
state++;
}
else
{
x++;
}
if (x == strlen(word.c_str()))
{
correct[word_number] = 1;
initialize();
}
}
void print()
{
switch (state)
{
case 1:
head = "(Q)";
break;
case 2:
upper = " |";
break;
case 3:
upper = "\|";
break;
case 4:
upper = "\|/";
break;
case 5:
body = "|";
break;
case 6:
lower = "/ ";
break;
case 7:
lower = "/ \\";
break;
default:
break;
}
printw(" ______\n");
printw("/ \\\n");
printw(" | %s\n", head.c_str());
printw(" | %s\n", upper.c_str());
printw(" | %s\n", body.c_str());
printw(" | %s\n", lower.c_str());
printw("__|__\n");
if (!valid)
{
printw("Only lowercase letters are allowed!\n");
valid = true;
}
printw("%s\n", guessed.c_str());
for (i = 0; i<strlen(word.c_str()); i++)
{
printw("%s ", blanks[i].c_str());
}
refresh();
}
void progress()
{
for (i = 0; i<25; i++)
{
if (correct[i] = = 1)
{
printw("%s -- Correct!\n", word_list[word_number].c_str());
}
else
{
printw("%s -- Incorrect.\n", word_list[word_number].c_str());
}
}
}
Here is my source code in its entirety
上線72發生錯誤, 85和98
編輯: 刪除th e標籤,正如所建議的那樣,我的問題的解決方案只是簡單地將輸入的比較實例替換爲輸入[0]到字符的char。
您將會發現'validate()'中的'goto start;'不能進入'play()'中的'start'標籤。你也許會更好地使用更少的全局變量;要回頭看文件的頂部來確定什麼是一切都是很困難的。傳遞參數;避開全局。 – 2011-05-07 14:53:56
全局變量是什麼? – 2011-05-07 14:54:11
輸入應該是我猜的字符,但是這個代碼有很多問題! – 2011-05-07 14:56:07