简体   繁体   中英

scanf single char not working with my loops

So I've been working on a code for a 2d treasure hunt where I use single char's from a scanf and whether it is n,s,w,e it does a specific output. Unfortunately my code is not listening to my input, for example, when I type in n, nothing happens. I was wondering if you guys could point me onto the right path

do
{
    printf("Please press n, s, w, or e to find the treasure\n");
    scanf(" %c", &n);
    if (scanf(" %c", &n) == 'n')
    {
        current - 11;
        new = current - 11;
        if (abs(potofgold - current) < abs(potofgold - new))
            printf("getting warmer\n");
        else if (abs(potofgold - current) > abs(potofgold - new))
            printf("Getting colder\n");
        else
            printf("Hooray\n");
        current = new;
    }
    else if (scanf("%c", &s) == 's')
    {
        current + 11;
        new = current + 11;
        if (abs(potofgold - current) < abs(potofgold - new))
            printf("Getting warmer\n");
        else if (abs(potofgold - current) > abs(potofgold - new))
            printf("getting colder\n");
        else
            printf("Hooray\n");
        current = new;
    }
    else if (scanf("%c", e) == 'e')
    {
        current + 1;
        new = current + 1;
        if (abs(potofgold - current) < abs(potofgold - new))
            printf("getting warmer\n");
        else if (abs(potofgold - current) > abs(potofgold - new))
            printf("getting colder\n");
        else
            printf("Hooray\n");
        current = new;
    }
    else (scanf("%c", &w) == 'w');
    {
        current - 1;
        new = current - 1;
        if (abs(potofgold - current) < abs(potofgold - new))
            printf("getting warmer\n");
        else if (abs(potofgold - current) > abs(potofgold - new))
            printf("getting colder\n");
        else
            printf("Hooray\n");

        current = new;
    }
}
while (current != potofgold);

return 0;

These conditions won't be true in any case-

if (scanf(" %c", &n) == 'n')       // compare variable with character n=='n' 
....
else if (scanf("%c", &s) == 's')              // s=='s'
....
else if (scanf("%c", e) == 'e')               // e=='e'
                    ^ & missing here

scanf here in each case return 1 on success . Don't compare it with characters.

Instead you can write conditions like this -

 if (scanf(" %c", &n) == 1 && n=='n')     // if scanf will be successful then only n=='n' will be evaluated. Thus , on failure of scanf scond condition will not be evaluated
  ....
 else if (scanf("%c", &s) ==1 && s=='s')  
 ....
 else if (scanf("%c", &e) == 1 && e=='e')              

And this should be else if -

 else (scanf("%c", &w) == 'w');   // else ends here (It should be else if)
{                                      // this is after else part 
    current - 1;           // remove this , compiler will issue an error\warning
    new = current - 1;     
    if (abs(potofgold - current) < abs(potofgold - new))
        printf("getting warmer\n");
    else if (abs(potofgold - current) > abs(potofgold - new))
        printf("getting colder\n");
    else
        printf("Hooray\n");

    current = new;
}

And these

 current - 11;  // do you mean current-=11;
 current + 11;  // as current+=11;
 current +1 ;   // as current +=1;
 current -1 ;   // as current -=1;

Take input first scanf("%c", &variable) above the if condition. Use if (n == 'n') instead of if (scanf(" %c", &n) == 'n') for each if condition. Because scanf return the number of input, it can take successfully

There are multiple problems, even in a fragment this small:

do
{
    printf("Please press n, s, w, or e to find the treasure\n");
    scanf(" %c", &n);
    if (scanf(" %c", &n) == 'n')
    {

It is good that you are using " %c" to read the characters.

First, you don't check whether the first scanf() succeeded. It is going to return one of 3 values in theory (2 in practice). Those values could be EOF if it detects end-of-file on standard input, 1 if it reads a non-blank character (possibly after multiple newlines and blanks and tabs), and in general it could return 0 if it fails to read input that satisfies the conversion specification without encountering EOF or an error. With a numeric conversion specification (such as %d ), that could happen if the first non-blank character was a letter; when the conversion specification is simply reading a character ( %c ), you won't get 0 returned.

Then, in the if statement, you read another character. But, since scanf() only returns EOF or 1 , it is not going to equal 'n' , so the if will fail, and will move onto the else where you read another character, and so on.

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