为什么我在比较字符串时会收到错误“与'= =''匹配?

时间:2011-05-07 14:48:05

标签: c++ string

对于我将字符串与另一个字符串进行比较的每一行,我都会收到错误:

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行

编辑: 根据建议删除标签后,我的问题的解决方案就是将输入的比较实例替换为带有输入[0]的char到char。

2 个答案:

答案 0 :(得分:4)

if (input == alphabet[i])左侧有std::string,右侧有char,因此显然没有定义比较。

您可能希望遍历input中的所有字符,并比较input[n](n是当前索引),如果它包含在alphabet中。你可以使用alphabet.find来更快地获得这个(或者你可以利用字符的ASCII表示)。

答案 1 :(得分: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