2011-05-07 51 views
-1

對於每一行,我一個字符串比較另一個字符串,我不斷收到錯誤:爲什麼在比較字符串時出現錯誤「不匹配'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。

+4

您將會發現'validate()'中的'goto start;'不能進入'play()'中的'start'標籤。你也許會更好地使用更少的全局變量;要回頭看文件的頂部來確定什麼是一切都是很困難的。傳遞參數;避開全局。 – 2011-05-07 14:53:56

+0

全局變量是什麼? – 2011-05-07 14:54:11

+0

輸入應該是我猜的字符,但是這個代碼有很多問題! – 2011-05-07 14:56:07

回答

4

if (input == alphabet[i])在其左側有一個std::string,右側有一個char,所以顯然沒有定義比較。

您可能想遍歷input中的所有字符,並比較input[n](n是當前索引)是否包含在alphabet中。你可以使用alphabet.find來獲得更快的速度(或者你可以利用字符的ASCII表示)。

2

在您列舉的每一行中,您都會將std::stringchar進行比較。

例如,在第72行,您是否檢查input是否有alphabet以外的字符?如果是這樣,假設字母進行排序,儘量

std::set<char> ins(in.begin(), in.end()); 
if(*ins.begin() < *alph.begin() || *ins.rbegin() > *alph.rbegin()) 
    valid = false; 

其中ininputalphalphabet

+0

儘可能多的我感謝你的幫助,我試圖堅持代碼,我可以理解:) – 2011-05-07 15:26:32

+0

你需要'#包括'編譯代碼。我正在做的是用'input'的內容創建一個_set_。一個'std :: set'是一個容器,它保存其數據_sorted_並且沒有重複。現在,如果'ins'和'alph'都是排序的,那麼我可以通過比較ins和alph的第一個字符,然後比較最後一個字符來快速判斷ins中是否有字符不在'alph'中「ins」和「alph」的特徵。我利用了英文小寫字母排序並連續位於ASCII字符集的事實。 – wilhelmtell 2011-05-07 15:38:19