简体   繁体   中英

Why does my do/while loop not work properly in C?

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int n = get_int("Please input a number between 1 and 8.\n");
    if( n >= 1 && n <= 8 )
    {
        printf( "Thank you for choosing within the boundaries. The number you entered was %i.\n", n );
    }

    do 
    {
        get_int( "Please enter a number that matches the critera.\n" );
    }
    while ( n > 8 || n < 1);
}

I just started off by listing my code right off the bat. Please go easy on me, I'm a newbie at this and am taking CS50 on edX to become better. When I enter an integer that "matches the criteria" (is between 1 and 8, inclusive), my code works like its supposed to. Similarly, when I enter an integer that is beyond those boundaries, it works like tis supposed to. However, after the prgram reprompts me to enter another integer, this time with me entering an integer within the boundary, the code does not work like it should. Instead of just saying "Thank you for choosing within the boundaries. The number you entered was %i", it says "Please enter a number that matches the critera", even though I entered something within the boundaries.

I hope you guys will be able to help. Thanks.

I think that in the line of "do while" the variable n is missing before get _...., I don't use cs50.h but is very important and c the use of variable also, hopefully run the code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM