0

I am a very new coder and am having a strange problem with a code I have to do for an assignment. The assignment requires that I create a menu offering options, and if you select one of those options, it (among other things) will ask you for an input and then verify that your input consist only of lower-case letters or spaces. Right now, i have this code:

#include <stdio.h>
#include <stdbool.h>
void menu(void)
{
     printf("Por favor introduce una de las siguientes opciones: \n");
     printf("\n");
     printf("1. las frases crecen\n");
     printf("2. las frases decrecen\n");
     printf("3. Salir del programa\n");
     printf("\n");
     printf("Opcion: ");
}

bool es_caracter_admitido(char letra1)
{
    if((letra1 >= 97 && letra1 <= 122) || letra1 == 32 )
    {
        return true;
    }
    else 
    {
        return false;
    }
}

int main()
{
    int a;
    char letra1, letra2;
    menu();
    scanf("%d",&a);
    if (a == 3)
        {
          return 0;  
        }

    else if (a < 0 || a > 3)
        {
            puts("opcion incorrecta");
            return 0;
        }
    printf("introduce una frase (enter para acabar): ");
    scanf("%c", &letra1);
    scanf("%c", &letra2);
            if (es_caracter_admitido(letra1) == true)
                {
                    puts("true");
                }
            else 
                {
                    puts("false");
                }

    getchar();   
}

The problem is that whenever I run this, no matter what input I put for letra1, it returns as false. The really weird thing is that the following piece of code works fine:

#include <stdio.h>
#include <stdbool.h>

bool test(char letra1)
{
    if((letra1 >= 97 && letra1 <= 122) || letra1 == 32 )
        {
           return true;
        }
        else 
        {
            return false;
        }
}
int main()
{

char letra1, letra2;



        puts("introduce una frase (enter para acabar");
        scanf("%c", &letra1);
        scanf("%c", &letra2);

        if (test(letra1) == true)
            {
            puts("true");
            }
            else 
            {
            puts("false");
            }

return 0;
}

The only discernible difference between the two, as far as I can tell, is that the first one calls the menu function, and the code that calls the es_caracter_admitido function is contained within an if statement. Somehow, that is causing this to fail, and I can't for the life of me undersand why. does anyone know why the boolean es_caracter_admitido always returns false, even if, for example, i input 'a' or a character that falls within the conditions outlined in the if statement contained in the function?

I apologize if this is a simple question, i've researched it a good bit and asked friends of mine that are experienced coders, and have yet to figure it out. Thank you!

1 Answers1

0

scanf("%d", &a); leaves the newline character \n in the buffer.

This means letra1 contains \n unless you do:

scanf(" %c", &letra1);

Further explanation:

The scanf() function removes whitespace automatically before trying to parse things other than characters. The character formats (primarily %c; also scan sets %[…] — and %n) are the exception; they don't remove whitespace.

Use " %c" with a leading blank to skip optional white space. Do not use a trailing blank in a scanf() format string.

Community
  • 1
  • 1
aaron
  • 39,695
  • 6
  • 46
  • 102