problem comparing strings in c language

Asked

Viewed 222 times

0

I need to answer a question that asks to type two words, like this:

is a canteen and there are 4 possible requests.

Two options for food: lasagna and stroganoff;

Two drink options: juice and soda.

I find problems with the code because when I type the first word scanf asks, the program already closes.

And it is necessary that the user can type interspersed between uppercase and minuscule, how to do so that there is no differentiation?

    float total_a_pagar;
    char comida[20];
    char bebida[20];
    scanf("%c\n%c", &comida, &bebida);

    if (strcmp(comida, "Lasanha") == 0 && strcmp(bebida, "Refrigerante") == 0)
    {
        total_a_pagar = 8+3;
        printf("%.1f\n", total_a_pagar);
    }
    else if (strcmp(comida, "Lasanha") == 0 && strcmp(bebida, "Suco") == 0)
    {
      total_a_pagar = 8+2.5;
      printf("%.1f\n", total_a_pagar);
    }


    else if (strcmp(comida, "Estrogonofe") == 0 && strcmp(bebida, "Refrigerante") == 0)
    {
      total_a_pagar = 11+3;
      printf("%.1f\n", total_a_pagar);
    }
    else if (strcmp(comida, "Estrogonofe") == 0 && strcmp(bebida, "Suco") == 0)
    {
      total_a_pagar = 11+2.5;
      printf("%.1f\n", total_a_pagar);
    }
  • 2

    For reading a string (string) the tag is %s and not %c. Note that the name of an array is already the address of the beginning of the array. Trade scanf("%c n%c", &food, &drink); by scanf("%s n%s", food, drink);

  • To compare by ignoring the letter box you can use the strcasecmp function of <strings. h>.

  • regarding: char comida[20];&#xA; char bebida[20];&#xA; scanf("%c\n%c", &comida, &bebida); should be: char comida[20];&#xA; char bebida[20];&#xA; if( scanf("%19s\n%c-19s", comida, bebida) !=2) {// handle error}

2 answers

0

Corrected program.

#include <stdio.h>

int main(void)
{
  char comida[20];
  char bebida[20];
  float total_a_pagar;

  // entrada de dados separada para comida e bebida...fica mais facil de
  // controlar o layout da entrada de dados, e tambem mais facil de entender
  // o funcionamento (esse formato "%s/n%s" e' estranho, nunca vi ser usado)

  // formato: "%19s%*[^\n]"
  // considera no maximo 19 caracteres, e ignora o restante da linha
  // (reserva espaco para o zero binario no final da string)

  // notar que o formato para string e' %s e nao %c, e que nao
  // se coloca '&' para indicar endereco de array de caracteres
  // (o proprio nome do array ja' e' interpretado como endereco)

  printf("*\n");
  printf("* comida: ");
  scanf("%19s%*[^\n]", comida); // le primeira palavra, despreza resto da linha

  printf("* bebida: ");
  scanf("%19s%*[^\n]", bebida);  // le primeira palavra, despreza resto da linha 

  if (strcmp(comida, "Lasanha") == 0 && strcmp(bebida, "Refrigerante") == 0)
    total_a_pagar = 8+3;

  else if (strcmp(comida, "Lasanha") == 0 && strcmp(bebida, "Suco") == 0)
    total_a_pagar = 8+2.5;

  else if (strcmp(comida, "Estrogonofe") == 0 && strcmp(bebida, "Refrigerante") == 0)
    total_a_pagar = 11+3;

  else if (strcmp(comida, "Estrogonofe") == 0 && strcmp(bebida, "Suco") == 0)
    total_a_pagar = 11+2.5;

  else
  {
    printf("* comida ou bebida invalida!\n%");
    printf("*\n");
    exit(0);
  }

  printf("* conta: %.1f\n", total_a_pagar);
  printf("*\n");
}

-1

When you execute the command:

scanf("%c\n%c", &comida, &bebida);

You are only asking to read 2 characters. That’s why the program closes after typing the first word, because it probably already provided the 2 characters requested and did not enter any of the if’s.

To solve this problem, simply exchange the %c of the scanf for %s:

scanf("%s\n%s", comida, bebida);

But you would also like the comparison made not differentiate capital from minuscule, for this just leave all words in minuscule or capital.

A way to accomplish this task would be to import the library ctype and use the function toupper (converts a char to uppercase) or tolower (converts to minuscule).

These functions convert char by char, so you need to create a loop to convert the entire string.

int i = 0;
while (comida[i] != '\0') {
    comida[i] = toupper(comida[i]);
    i++;
}

Note that it is not necessary to calculate the total size of the String, as the end of all String in C is delimited by the special character ' 0'.

Finally, when checking through strcmp(), just check using the chosen format

For example:

if (strcmp(comida, "LASANHA == 0" && strcmp(bebida, "REFRIGERANTE == 0)
 { [...] 
  • 1

    Do not have this & em: scanf("%s n%s", &food, &drink);, exchange for: scanf("%s n%s", food, drink);.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.