数字Gusser在c ++中使用函数和中点 [英] Number Gusser in c++ using function and midpoint

查看:157
本文介绍了数字Gusser在c ++中使用函数和中点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用funcitons编写gusser的代码:
playOneGame函数应该有一个void返回类型。它应该在1到100的范围内实现一个完整的猜测游戏。

shouldPlayAgain函数应该有一个布尔返回类型。它应该提示用户确定用户是否想要再次播放,读取字符,然后如果字符是y则返回真,否则返回假。
此外,你应该实现帮助函数getUserResponseToGuess和getMidpoint。它们应该在您的playOneGame函数中调用。
getUserResponseToGuess。此功能应提示用户使用短语是吗? (h / l / c):,其值替换了令牌。它应该返回一个char。 char应该是三个可能的值之一:'h','l'或'c'。它应该有以下签名:
char getUserResponseToGuess(int guess)
getMidpoint。这个函数应该接受两个整数,它应该返回两个整数的中点。如果在范围的中间有两个值,那么你应该一致选择两个中的较小者。它应该有以下签名:
int getMidpoint(int low,int high)
我开始withit但我仍然无法得到它的逻辑。这是我到目前为止。

I am trying to write a code for number gusser using funcitons: The playOneGame function should have a return type of void. It should implement a complete guessing game on the range of 1 to 100.
The shouldPlayAgain function should have a boolean return type. It should prompt the user to determine if the user wants to play again, read in a character, then return true if the character is a ‘y’, and otherwise return false. In addition, you should implement the helper functions getUserResponseToGuess, and getMidpoint. They should be invoked inside your playOneGame function. getUserResponseToGuess. This function should prompt the user with the phrase "is it ? (h/l/c): " with the value replacing the token . It should return a char. The char should be one of three possible values: ‘h’, ‘l’, or ‘c’. It should have the following signature: char getUserResponseToGuess(int guess) getMidpoint. This function should accept two integers, and it should return the midpoint of the two integers. If there are two values in the middle of the range then you should consistently chose the smaller of the two. It should have the following signature: int getMidpoint(int low, int high) I start withit but I still can't get the logic of it.This what I have so far.

#include<iostream>
using namespace std;

 void playOneGame;
 char getUserResponseToGuess(int guess);
 int getMidpoint ( int low, int high);

int main() {
do
 {
 playOneGame();
} while (shouldPlayAgain());
return 0;
}
void playOneGame
{
int a = 100;


cout << "\nGuess a number between 1 and 100. " <<endl;
getUserResponseToGuess ( a);

 }


char getUserResponseToGuess(int guess)
{
while (true)
{
int guess = getMidpoint(minimum, maximum);
std::cout << "\nIs it [h]igher/[l]ower/[e]qual to " << guess << "? ";
char answer;
if (!(std::cin >> answer))
{
    std::cerr << "error reading user input, program exiting\n";
    exit(1);
}
if (answer == 'h')
    minimum = guess + 1;
else if (answer == 'l')
    maximum = guess - 1;
else if (answer == 'e')
{
    std::cout << "Well, isn't that nice.\n";
    return;
}
if (minimum > maximum)
{
    std::cerr << "hey, you lied to me!\n";
    exit(1);
}
}
}

int getMidpoint ( int low, int high)
{

int mid;

mid = (low + high) / 2;

return mid;

}


推荐答案

猜测循环应该像这样:

while (true)
{
    int guess = getMidpoint(minimum, maximum);
    std::cout << "\nIs it [h]igher/[l]ower/[e]qual to " << guess << "? ";
    char answer;
    if (!(std::cin >> answer))
    {
        std::cerr << "error reading user input, program exiting\n";
        exit(1);
    }
    if (answer == 'h')
        minimum = guess + 1;
    else if (answer == 'l')
        maximum = guess - 1;
    else if (answer == 'e')
    {
        std::cout << "Well, isn't that nice.\n";
        return;
    }
    if (minimum > maximum)
    {
        std::cerr << "hey, you lied to me!\n";
        exit(1);
    }
}

这篇关于数字Gusser在c ++中使用函数和中点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆