How do I solve this problem with the output being unable to get the statement of the input(myflush) in coding “Random Number Guessing” in C

眉间皱痕 提交于 2020-01-16 08:59:18

问题


I tried going beyond just guessing random numbers. Basically, the program should be made to show like this :

insert a number : 70
bigger than 0 smaller than 70.
insert a number : 35
bigger than 35 smaller than 70.
insert a number : 55
bigger than 55 smaller than 70.
insert a number : 60
bigger than 55 smaller than 60.
insert a number : 57
You got it right on your 5th try!

I had some problems, but already through brilliant people in stackoverflow, I am able to get all the results I wish, but with one problem remaining.

I changed and kind of developed it and everything worked except than when I insert a number that is out of range of 1~100, at the beginning, it works, and the next line would be to ask again the insert a number as written in the code of the inputblock.

However, starting from the second inserting number, if I insert the number out of range, it can't catch that the number is out of range and would just print out that number.

insert a number : 1000
insert a number : -90
insert a number : 101
insert a number : 70
bigger than 0 smaller than 70.
insert a number : 35
bigger than 35 smaller than 70.
insert a number : 1000
insert a number : 

where as this should happen, what actually happens is the following:

insert a number : 1000
insert a number : 200
insert a number : 70
bigger than 0 smaller than 70.
insert a number : 2000
bigger than 0 smaller than 2000.

Could anyone give me some advice on how to get this solved? I have a hunch that it is a problem with the output being unable to get what the input is doing, but I'm not sure...

The following is the code that has the current problem.

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#pragma warning (disable : 4996)

int input();
int random(int);
void myflush();
int output(int, int);

int main()
{
    int num;
    int i;
    int ran;
    int count = 0;

    srand((unsigned int)time(NULL));
    i = 0;
    while (i < 1) {
        ran = 1 + random(101);  // random number 1~101
        ++i;
    }

    num = input();
    count = output(ran, num);

    printf("You got it right on your %d try!", count);

    return 0;
}



int input()
{
    int num;
    printf("insert a number : ");
    scanf("%d", &num);
    while (num < 1 || num > 100 || getchar() != '\n') {
        myflush();
        printf("insert a number : ");
        scanf("%d", &num);
    }
    return num;
}


int random(int n)
{
    int res;
    res = rand() % n;
    return res;
}


void myflush()
{
    while (getchar() != '\n') {
        ;
    }
    return;
}


int output(int ran, int num) {
    int count = 0;
    int large = 100;
    int small = 0;
    while (1) {
        if (num != ran) {
            count++;
            if (num < ran) {
                small = num;
                printf("bigger than %d and smaller than %d\n", small, large);
            }
            else if (num > ran) {
                large = num;
                printf("bigger than %d and smaller than %d\n", small, large);
            }
            printf("insert a number : ");
            scanf("%d", &num);
        }
        else {
            count++;
            break;
        }
    }
    return count;
}

来源:https://stackoverflow.com/questions/59632351/how-do-i-solve-this-problem-with-the-output-being-unable-to-get-the-statement-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!