C ++家庭作业不循环

时间:2017-09-02 17:28:32

标签: c++ while-loop char

所以,我是C ++的新手,正和朋友分享一本书。我正在创建一个简单的猜谜游戏,用户想象一个数字,计算机试图猜测它。当我在Visual Studio中进行调试时,该项目确实进行了猜测,并正确打印“我该怎么办?”。此时,它应该获得“反馈”变量的用户输入。然而,在提示之后,似乎它只会在'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 个答案:

答案 0 :(得分:1)

一千英里的旅程从一步开始,所以这里有一些帮助你的第一步:

using namespace std;

不要那样做。 std::拥有您可能会使用的标识符,并且可以保证问题。

char feedback[1];

你的输入永远不会超过1,所以

char feedback;

非常合适。 (另外:数组基于0,因此它应该是char feedback[0];而不是char feedback[1];

cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << endl << endl;

std::endl刷新缓冲区,无需执行两次。只需使用'\n'

std::cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << "\n\n";

您将获得feedback中密钥的字符代码。 '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;
}
相关问题