Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scanf isn't executing on every loop iteration

I am writing a program for fun (not for school), and am having a hard time figuring out why the scanf function isn't executing on every iteration of my loop - I've toyed with both 'for' loops and 'while' loops.

I know that depending on how I write the scanf function (i.e. scanf("%s", &variablename); VS scanf("%99[^\n]s", &variablename);) makes a difference, but I have tried everything and I'm desperate!

When I do a printf check on my input from the scanf, on every iteration it is only intaking one string per iteration, so if I enter two words in my first input, then it takes two iterations to process - one word per. Here is the segment of code I'm describing:

int main(void){
    int tries = 0;
    int score = 0;
    char question[100];
    char useranswer[100];
    const char *phrase = {"our favorite saying\0"};

    printf("\nQuestion #3 (10 points): What is our secret saying?\n");
        sleep(1);
        tries = 1;

    while (tries<=3){
        printf("YOUR ANSWER:");
        scanf("%s[^\n]", useranswer); 

        if(strncmp(useranswer, phrase, 15) != 0){
            printf ("Nope, try again!\n");
            printf("You have used %d out of 3 tries!\n", tries);
            if (tries == 2){
                printf("Here's your final hint:xxx...\n");
            }
            if (tries == 3){
            printf("You didn't get it. The answer is: our favorite saying!\n");
            }
            tries++;
        }   
        if (strncmp(useranswer, phrase, 15) == 0){
            printf("Damn, you're good.  Well done.\n");
            score += 10;
            break;
        }
    }   

The output of this code is:

Question #3 (10 points): What is our secret saying?
YOUR ANSWER:our favorite saying
Nope, try again!
You have used 1 out of 3 tries!
YOUR ANSWER:Nope, try again!
You have used 2 out of 3 tries!
Here's your final hint:xxx...
YOUR ANSWER:Nope, try again!
You have used 3 out of 3 tries!
You didn't get it. The answer is: our favorite saying!

(It only allowed me to input once, and I typed "our favorite saying".)

like image 967
blarpsplat Avatar asked Feb 10 '26 21:02

blarpsplat


1 Answers

In comments you could find why your format specifier in scanf doesn't work.

An alternative is to use fgets instead, maybe in an helper function which takes care of some of the corner cases that can arise while reading user input:

#include <ctype.h>

char *read_line( char *buf, size_t n, FILE *pfin )
{
    ssize_t length = 0;
    int ch;

    if ( !buf || n == 0 )
        return NULL;
    /*  Consume trailing control characters, like '\0','\n', '\r', '\f'...
        but also '\t'. Note that ' ' is not skipped. */
    while ( (ch = fgetc(pfin)) != EOF  &&  iscntrl(ch) ) { }
    if ( ch == EOF )
        return NULL;
    /*  At least one char is printable  */
    *buf = ch;
    ++length;

    /*  Read from file till a newline or up to n-2 chars. The remaining chars
        are left in the stream buffer. Return NULL if no char is read.      */
    if ( fgets(buf + 1, n - 1, pfin) )
    {
        /*  Trim the string to the first control character                  */
        while ( !iscntrl(buf[length]) ) 
        {
            ++length;
        }
        buf[length] = '\0';       
    }
    return buf;
}

I'd change the following logic too. OP uses strncmp(useranswer, phrase, 15) multiple times, but that magic number 15 is lower then phrase's size so it ends up comparing only a substring.

while ( tries <= 3 ) {
    printf("YOUR ANSWER:");
    if ( !read_line(useranswer, sizeof useranswer, stdin) ) {
        printf("Error: Unexpected end of input.\n");
        exit(EXIT_FAILURE);
    }
    if( strcmp(useranswer, phrase) == 0 ) {
        printf("Damn, you're good.  Well done.\n");
        score += 10;
        break;
    } else {
        printf ("Nope, try again!\n");
        printf("You have used %d out of 3 tries!\n", tries);
        if (tries == 2) {
            printf("Here's your final hint:xxx...\n");
        }
        if (tries == 3) {
            printf("You didn't get it. The answer is: our favorite saying!\n");
        }
        tries++;
    }
}


As a final note, I found OP declaration of phrase a bit weird (maybe a typo):
const char *phrase = {"our favorite saying\0"};
//            string literals are already ^^ null terminated...

While we can use a simple array declaration, like:

const char phrase[] = "our favorite saying";

Consider also what values sizeof phrase returns in those two different cases.


Thanks to @chux for all the valuable hints and the interesting links provided:
https://stackoverflow.com/a/27729970/4944425
https://stackoverflow.com/a/28462221/4944425
And to @Dmitri for having pointed out in his comment that once we are sure that both the strings are null terminated, we can use strcmp instead of strncmp.
like image 72
Bob__ Avatar answered Feb 12 '26 14:02

Bob__



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!