开发者

Why am I getting the error "no match for 'operator==' " when comparing strings? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

For every line where I compare a string to another string, I keep getting the error:

No match for 'operator=='

Code:

#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

The errors occur on lines 72, 85, and 98

EDIT: After removing the labels, as suggested, the solution to my problem was simply to replace the comparison instances of input to a char with input[0] to a char.


if (input == alphabet[i]) has a std::string on its left and a char on its right, so the comparison is obviously not defined.

You perhaps wanted to iterate through all characters in input and compare input[n] (n being the current index) if it is contained in alphabet. You can use alphabet.find to get this much quicker (or you can exploit the ASCII representation of the characters).


In each of these lines you enumerated you compare a std::string with a char.

In line 72, for example, are you checking if input has a character outside of alphabet? If so, and assuming the alphabet is sorted, try

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

where in is input and alph is alphabet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜