C, Compare does not work for me

0

I want to compare in the if, if it is a number that does the main function of the program and if it is a letter that prints a sentence but does not work:

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

int main(){

char seguir, a;
float gg, tot, n1, n2;

printf("\t\t\tCuanto cuesta.");
printf("\nEste programa te dice cuanto cuesta cada item de un paquete,");
printf("\npor ejemplo, cuanto cuesta cada galleta de un paquete que cuesta n");
printf("\n==============================================================================");

do{
printf("\n\nCuanto costo el paquete / bolsa / etc: ");
scanf("%f", n1);

if ((n1>=48)&&(n1<=57)){
printf("\nCuantos items hay?: ");
scanf("%f", &n2);

tot=n1/n2;
printf("\nEl valor de cada item es: %.5f centavos", tot);
}
else{
    printf("Escribe un numero por favor");
}

printf("\n\nQuieres hacerlo de nuevo?: (s/n) ");
fflush(stdin);
scanf("%c", &seguir);
system("cls");

}while(seguir != 'n');

return 0;

}
    
asked by Jenio158 12.10.2017 в 05:20
source

3 answers

0

To know which address will save the value entered scanf , it does so using the operator & (by reference) what is needed in its exercise. Related question

scanf("%f", n1);

Besides this you have the same error as in a question formulated previously , in the part of the options S/N scanf("%c", &seguir); does not wait if it does not jump this is due to the key ENTER (line break, it is still in the buffer) to solve would suffice to add a space at the beginning.

Final code (I modified the else's message) See Online Demo

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

int main(){

char seguir, a;
float gg, tot, n1, n2;

printf("\t\t\tCuanto cuesta.");
printf("\nEste programa te dice cuanto cuesta cada item de un paquete,");
printf("\npor ejemplo, cuanto cuesta cada galleta de un paquete que cuesta n");
printf("\n==============================================================================");

do{
printf("\n\nCuanto costo el paquete / bolsa / etc: ");
scanf("%f", &n1);

if ((n1>=48)&&(n1<=57)){
printf("\nCuantos items hay?: ");
scanf(" %f", &n2);

tot=n1/n2;
printf("\nEl valor de cada item es: %.5f centavos", tot);
}
else{
    printf("Escribe un numero entre el rango 48-57");
}

printf("\n\nQuieres hacerlo de nuevo?: (s/n) ");
fflush(stdin);
scanf(" %c", &seguir);
//system("cls");

}while(seguir != 'n');

return 0;

}
    
answered by 12.10.2017 / 06:34
source
0

I think the problem you're having, that does not allow you to take the value of n1 is that you forgot to put "&" before the variable. Therefore the Scanf function does not know in which memory address the variable n1 is located.

You should change the line:

scanf("%f", n1);

for the following:

scanf("%f", &n1);

Obviating the logical questions of the program in that way you would get the data to do the calculations you wrote.

More info about Scanf in the web cplusplus

Responding to your comment. If what you want is to do a simple calculation. I think you should multiply instead of divide in addition to the if it would not make much sense. The code I imagine it this way:

do{
    printf("\n\nCuanto costo el paquete / bolsa / etc: ");
    scanf("%f", &n1);

    printf("\nCuantos items hay?: ");
    scanf("%f", &n2);

    tot= n1*n2;
    printf("\nEl valor de cada item es: %.2f centavos", tot);

    printf("\n\nQuieres hacerlo de nuevo?: (s/n) ");
    fflush(stdin);
    scanf("%c", &seguir);
    system("cls");

}while(seguir != 'n');
    
answered by 12.10.2017 в 05:41
0
  

I want to compare in the if, if it is a number that does the main function of the program and if it is a letter that prints a sentence but does not work:

scanf returns an integer indicating how many elements it could read successfully.

What you have to do is use that return value to know if a number has been entered:

if( scanf("%f", &n1) == 1 )

But of course, if the reading has not been correct then it will have been garbage in the input buffer. Unfortunately fflush should not be used to clean the input buffer since this function is intended to work with output buffers. Likewise, there is no proper function to clean the input buffer ... the most common is to do this:

char c;
while ((c = getchar()) != '\n' && c != EOF);

Applying this your code should look like this:

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

int main(){

  char seguir, a;
  float gg, tot, n1, n2;

  printf("\t\t\tCuanto cuesta.");
  printf("\nEste programa te dice cuanto cuesta cada item de un paquete,");
  printf("\npor ejemplo, cuanto cuesta cada galleta de un paquete que cuesta n");
  printf("\n=============================================================================");

  do{
    printf("\n\nCuanto costo el paquete / bolsa / etc: ");
    if( scanf("%f", &n1) == 1 )
    {
      printf("\nCuantos items hay?: ");
      scanf("%f", &n2);

      tot=n1/n2;
      printf("\nEl valor de cada item es: %.5f centavos", tot);
    }
    else{
      printf("\nEscribe un numero por favor");
    }

    char c;
    while ((c = getchar()) != '\n' && c != EOF) { }

    printf("\n\nQuieres hacerlo de nuevo?: (s/n) ");

    scanf("%c", &seguir);

  }while(seguir != 'n');

  return 0;

}
    
answered by 12.10.2017 в 08:55