Home > front end >  If function and scanf function not working together
If function and scanf function not working together

Time:12-10

so I have been trying to write a code that displays different messages if different keys are pressed

It should display "Your hair looks nice" if one of the characters of the string is pressed and display "You look like your mom" if any number of symbol is pessedd (anything other than the array's elements)

what is the issue here? (Mind the messages in the code I'm trying to stay chill so I picked random messages) the code:

#include<stdio.h>
int
main ()
{
  char i,o;
  
  char a[54] =
    { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C',
    'D',
    'E', 'F', 'G', 'H', 'I', 'G', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
    'T',
    'U', 'V', 'W', 'X', 'Y', 'Z'
  };
   
    
    

  if (scanf ("%c", &o) == a[i])
    printf ("Your hair looks nice");

  else
    printf ("You look like your mom");

  return 0;

}

CodePudding user response:

C have a set of standard character classification functions, like for example isalpha to check if a character is a letter or not.

With this you can make your program much simpler, and don't need the array:

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int ch = getc();  // Read one character

    if (isalpha(c))
    {
        puts("Your hair looks nice.");
    }
    else
    {
        puts("You look like your mom!");
    }
}

Note that I use the getc function to read a character instead. In most cases scanf shouldn't really be used for input, it's use is unfortunately more non-trivial than the beginners guides and tutorials make it seem.

CodePudding user response:

You need to check all relevant values of i. Currently, you don't even assign a single value to i, but you need to assign 54 different values in turn.

And of course, you need to call scanf only once, not 54 times.

CodePudding user response:

If you want to see whether the inputted character is in the array, then you must compare that character with all 54 characters in the array. However, in your posted code, you are only making one comparison. I suggest that you use a for loop for doing the 54 comparisons. You can do one comparison per loop iteration.

Here is an example:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    char a[54] = {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm',
        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'G', 'K', 'L', 'M',
        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };

    char input;

    //get input character
    if ( scanf( "%c", &input ) != 1 )
    {
        fprintf( stderr, "Input error!\n" );
        exit( EXIT_FAILURE );
    }

    //determine whether input character is in array
    for ( int i = 0; i < 54; i   )
    {
        if ( a[i] == input )
        {
            printf( "Found character in array.\n" );
            return 0;
        }
    }

    printf( "Did not find character in array.\n" );
    return 0;
}

However, in order to determine whether a character is an alphabetical letter, there is a much simpler solution: You can simply call the function isalpha as demonstrated in one of the other answers.

CodePudding user response:

From scanf's man page:

RETURN VALUE:

On success, these functions return the number of input items successfully matched and assigned; this can be fewer than provided for, or even zero, in the event of an early matching failure.

According to this, the statement

if (scanf ("%c", &o) == a[i])

compares the return value of scanf, which in this case would be one 1(if scanf succeeded) to a[i], where i is uninitialised and leads to undefined behaviour. As I remember @Fe203 once saying:

"Leaving variables uninitialised is asking Demon of Hard-To-Find Bugs to co-author your code."

So you ought to first initialise i, then iterate through the array, comparing o to all the 54 values of i.

Aside:

You shouldn't be using scanf for user-interactive stuff. It is not supposed to be used as such, and it's quite hard to get right. In this case, consider using getc or getchar which reads one character at a time.

Or at least, check the return value of scanf.

  •  Tags:  
  • c
  • Related