加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 否运算符“<<”匹配这些操作数

发布时间:2020-12-16 05:44:19 所属栏目:百科 来源:网络整理
导读:不知道发生了什么我看过与这个问题类似的其他帖子,但是迄今为止还没有帮助.这里是代码,由错误的部分提供评论.有一点,它表示!=不起作用,而在代码的其余部分中,不工作 #include iostream#include algorithm#include vector#include ctime#include cstdlib#inc
不知道发生了什么我看过与这个问题类似的其他帖子,但是迄今为止还没有帮助.这里是代码,由错误的部分提供评论.有一点,它表示!=不起作用,而在代码的其余部分中,<<不工作
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>

using namespace std;
//Hangman

int main()
{
    //setup
    vector<string> words;
    words.push_back("GUITAR");
    words.push_back("VIRGINIA");
    words.push_back("LAPTOP");
    words.push_back("WIFEY");
    words.push_back("IPHONE");

    srand(static_cast<unsigned int>(time(0)));   //randomly select a word
    random_shuffle(words.begin(),words.end());
    const string THE_WORD = words[0];
    const int MAX_WRONG = 8;                    //initialize wrong guesses
    int wrong = 0;
    string soFar(THE_WORD.size(),'-');         //initalize the word so far with dashes
    string used = " ";                          //initalize letters used


    cout << "Welcome to Hangman. Good luck!/n";

    //game loop
    //continues until a player guesses the word or gets too many wrong guesses
    while ((wrong < MAX_WRONG) && (soFar != THE_WORD)) //ERROR on the "!="
    {
        cout << "nnYou have " << (MAX_WRONG - wrong) << " incorrect guesses left.n";
        cout << "nYou've used the following letters:n" << used << endl; //ERROR on "<<" between the string and used
        cout << "nSo far,the word is:n" << soFar << endl; //ERROR on "<<" between the string and soFar

    //recieve the player's guess
        char guess;
        cout << "nnEnter your guess: ";
        cin >> guess;
        guess = toupper(guess); //makes the guess uppercase since the secret word is uppercase
        while (used.find(guess) != string::npos)
        {
            cout << "nYou've already guessed " << guess << endl;
            cout << "Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }

        used += guess;

        if (THE_WORD.find(guess) != string::npos)
        {
            cout << "That's right! " << guess << " is in the word.n";

            //updated soFar to include newly guessed letter
            for (int i=0; i < THE_WORD.length(); ++i)
            {
                if (THE_WORD[i] == guess)
                {
                    soFar[i] = guess;
                }
            }
        }
        else
        {
            cout << "Sorry," << guess << " isn't in the word.n";
            ++wrong;
        }
    }
    //shut down
    if (wrong == MAX_WRONG)
    {
        cout << "nYou've been hanged!";
    }
    else
    {
        cout << "nYou guessed it!";
    }

    cout << "nThe word was " << THE_WORD << endl; //ERROR on "<<" between the string and THE_WORD


    int pause;
    cin >> pause; //this is here just so my compiler will stay open so I can see what's going on
    return 0;
}

解决方法

如果要使用std :: string可靠,则必须#include< string&gt ;.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读