C++;家庭作业不需要';t形环 P> >我是C++的超级新手,和朋友分享一本书。我正在创建一个简单的猜谜游戏,用户想象一个数字,计算机试图猜它。当我在VisualStudio中调试时,项目确实进行了猜测,并正确地打印“我是怎么做的?”。此时,它应该为“反馈”变量获取用户输入。然而,在提示之后,它似乎只会重复'while'语句之前的所有内容。这个问题是否与反馈char变量有关(也许我应该使用'cin'和整数?),或者我只是缺少了一些非常明显的东西 //Game that attempts to guess a number from one to twenty. #include "stdafx.h" #include <iostream> using namespace std; int main() { auto lowbound = 1; auto highbound = 20; auto guess = 10; auto gamecont = true; char feedback[1]; cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << endl << endl; cout << " If my guess is too low, just say (1). If too high, say (2). Say (3) if I've got it. It's (ENTER) to get going!" << endl << endl; cout << " Waiting on you..." << endl << " "; cin.get(); while(gamecont) { cout << " I'm thinking your number is " << guess << "." << endl << endl; cout << " How did I do?" << endl << endl << " "; cin.get(feedback, 1); if (feedback[1] == 1) // The guess was too low. { if (guess == 10) { guess = 15; } else if (guess >= 15) { guess++; } else if (guess < 10) { guess++; } } else if (feedback[1] == 2) // The guess was too high. { if (guess == 10) { guess = 5; } else if (guess <= 5) { guess--; } else if (guess > 10) { guess--; } } else if (feedback[1] == 3) // The guess was correct. { gamecont = false; } } return 0; } //尝试从1到20猜一个数字的游戏。 #包括“stdafx.h” #包括 使用名称空间std; int main() { 自动下限=1; 自动上限=20; 自动猜测=10; 自动游戏控制=真; 字符反馈[1]; 千里之行始于足下,这里有一些帮助你迈出第一步: using namespace std;

C++;家庭作业不需要';t形环 P> >我是C++的超级新手,和朋友分享一本书。我正在创建一个简单的猜谜游戏,用户想象一个数字,计算机试图猜它。当我在VisualStudio中调试时,项目确实进行了猜测,并正确地打印“我是怎么做的?”。此时,它应该为“反馈”变量获取用户输入。然而,在提示之后,它似乎只会重复'while'语句之前的所有内容。这个问题是否与反馈char变量有关(也许我应该使用'cin'和整数?),或者我只是缺少了一些非常明显的东西 //Game that attempts to guess a number from one to twenty. #include "stdafx.h" #include <iostream> using namespace std; int main() { auto lowbound = 1; auto highbound = 20; auto guess = 10; auto gamecont = true; char feedback[1]; cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << endl << endl; cout << " If my guess is too low, just say (1). If too high, say (2). Say (3) if I've got it. It's (ENTER) to get going!" << endl << endl; cout << " Waiting on you..." << endl << " "; cin.get(); while(gamecont) { cout << " I'm thinking your number is " << guess << "." << endl << endl; cout << " How did I do?" << endl << endl << " "; cin.get(feedback, 1); if (feedback[1] == 1) // The guess was too low. { if (guess == 10) { guess = 15; } else if (guess >= 15) { guess++; } else if (guess < 10) { guess++; } } else if (feedback[1] == 2) // The guess was too high. { if (guess == 10) { guess = 5; } else if (guess <= 5) { guess--; } else if (guess > 10) { guess--; } } else if (feedback[1] == 3) // The guess was correct. { gamecont = false; } } return 0; } //尝试从1到20猜一个数字的游戏。 #包括“stdafx.h” #包括 使用名称空间std; int main() { 自动下限=1; 自动上限=20; 自动猜测=10; 自动游戏控制=真; 字符反馈[1]; 千里之行始于足下,这里有一些帮助你迈出第一步: using namespace std;,c++,while-loop,char,C++,While Loop,Char,不要这样做。std::中挤满了您可能也会使用的标识符,问题是肯定的 char feedback[1]; 您的输入永远不会超过1,所以 char feedback; 更合适。(此外:数组基于0,因此应该是字符反馈[0];而不是字符反馈[1];) 您将在反馈中获得钥匙的字符代码'1'不等于1,因此 if (feedback == 1) 应该是 if (feedback == '1') 就是这样。你还有一些工作要做,例如猜测策略很差,但这应该是一个开始 //Game that attempts

不要这样做。
std::
中挤满了您可能也会使用的标识符,问题是肯定的

char feedback[1];
您的输入永远不会超过1,所以

char feedback;
更合适。(此外:数组基于0,因此应该是
字符反馈[0];
而不是
字符反馈[1];

您将在
反馈中获得钥匙的字符代码
'1'
不等于
1
,因此

if (feedback == 1)
应该是

if (feedback == '1')
就是这样。你还有一些工作要做,例如猜测策略很差,但这应该是一个开始

//Game that attempts to guess a number from one to twenty.

#include <iostream>

int main()
{
    auto lowbound = 1;
    auto highbound = 20;
    auto guess = 10;
    auto gamecont = true;

    char feedback;


    std::cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << "\n\n";
    std::cout << " If my guess is too low, just say (1). If too high, say (2). Say (3) if I've got it. It's (ENTER) to get going!" << "\n\n";
    std::cout << " Waiting on you..." << "\n\n";

    std::cin.get();

    while(gamecont)
    {

        std::cout << " I'm thinking your number is " << guess << "." << "\n\n";
        std::cout << " How did I do?" << "\n\n";

        std::cin.ignore();
        std::cin.get(feedback);

        if (feedback == '1') // The guess was too low.
        {
            if (guess == 10)
            {
                guess = 15;
            }
            else if (guess >= 15)
            {
                guess++;
            }
            else if (guess < 10)
            {
                guess++;
            }
        }
        else if (feedback == '2') // The guess was too high.
        {
            if (guess == 10)
            {
                guess = 5;
            }
            else if (guess <= 5)
            {
                guess--;
            }
            else if (guess > 10)
            {
                guess--;
            }
        }
        else if (feedback == '3') // The guess was correct.
        {
            gamecont = false;
        }



    }

    return 0;
}
//尝试从1到20猜一个数字的游戏。
#包括
int main()
{
自动下限=1;
自动上限=20;
自动猜测=10;
自动游戏控制=真;
字符反馈;

STC++:C++中的CUT数组是零基的,“也许我应该只使用‘CIN’和整数”?-Yes Car’1’和整数1是不同的thigsbw,单个字符变量可能比一个1字符数组更容易处理。通常是很棒的建议,我很感激你们。“我想你的数字是”会重复,而
int guess
会保持10。在实现了所有其他功能(并在VS2017中关闭了预编译头)后,我发现只需切换
std::cin.ignore();
std::cin.get(反馈)的位置即可
使一切正常工作。
\n
是炸弹,顺便说一句。我唯一不明白的是,当你提到数组是基于0的,所以它必须以不同的方式编写。我无法区分它们之间的区别:(.TYgreat,这很有帮助。事实上,我没有粘贴正确的代码,我忘了更改if(feedback==1)等到if(feedback==1'),解释是正确的,但代码是错误的,对不起!从零开始的东西不再重要,因为数组已被删除,但如果您有一个数组,它应该是char feedback[0];而不是char feedback[1];。(是的,对不起,请键入我的答案。两个打字错误都已纠正。谢谢您指出这一点!
if (feedback == '1')
//Game that attempts to guess a number from one to twenty.

#include <iostream>

int main()
{
    auto lowbound = 1;
    auto highbound = 20;
    auto guess = 10;
    auto gamecont = true;

    char feedback;


    std::cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << "\n\n";
    std::cout << " If my guess is too low, just say (1). If too high, say (2). Say (3) if I've got it. It's (ENTER) to get going!" << "\n\n";
    std::cout << " Waiting on you..." << "\n\n";

    std::cin.get();

    while(gamecont)
    {

        std::cout << " I'm thinking your number is " << guess << "." << "\n\n";
        std::cout << " How did I do?" << "\n\n";

        std::cin.ignore();
        std::cin.get(feedback);

        if (feedback == '1') // The guess was too low.
        {
            if (guess == 10)
            {
                guess = 15;
            }
            else if (guess >= 15)
            {
                guess++;
            }
            else if (guess < 10)
            {
                guess++;
            }
        }
        else if (feedback == '2') // The guess was too high.
        {
            if (guess == 10)
            {
                guess = 5;
            }
            else if (guess <= 5)
            {
                guess--;
            }
            else if (guess > 10)
            {
                guess--;
            }
        }
        else if (feedback == '3') // The guess was correct.
        {
            gamecont = false;
        }



    }

    return 0;
}