problem with scanf

0

I have a problem with my programs and sometimes I try programs with which I have to use more than one entry data, however when writing them, only the first entry data is read, skipping the others, and if I modify the program removing the only data that if read then if I can read the others, as an example I have this program:

#include <stdio.h>

int main ()
{
    char n = 0;
    int a = 0, a1 = 0, e = 0;
    system("cmd /c cls");

    printf("Hola \t");

    scanf(" %c", n);
    printf("%c\n", n);

    printf("NACIO DEL AÑO \t");
    scanf("%d", &a);
    printf("año actual :\t");
    scanf("%d", &a1);

    printf("\n\n");
    printf("En el año 2030 cumpliras \t");

    e = 2030 - a;
    printf("%d\t", e);            
}

As you can see, first ask to type my name, but in doing so it does not allow entering the other input data, it jumps, but if I delete everything related then if I can enter the other input data. What can you owe? I've also tried it with examples already solved and I'm not allowed either:

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

 int main ()
{
    double c, intereses, capital;
    float r;
    int t;

    system ("cmd /c cls");               //Limpiar pantalla

    /*Entrada de datos*/
    printf("Capital invertido          ");
    scanf("lf", &c);
    printf("\nA un porcentaje anual del          ");
    scanf("%f", &r);
    printf("\nDurante cuántos días        ");
    scanf("%d", &t);
    printf("\n\n");

    /*Cálculos*/
    intereses = c * r * t / (360L * 100);
    capital = c + intereses;

    /*Escribir resultados*/
    printf("Intereses producidos...%10.0f\n", intereses);
    printf("Capital acumulado......%10.0f\n", capital);
}

in this it only allows me to enter the three input data, the first and the third, but not the second, and I can only enter it when I delete the other input data from the program. PS I use netbeans

    
asked by isaac 19.07.2018 в 20:22
source

1 answer

2
system ("cmd /c cls");

system already invokes a system command in its own right. In your case, you are invoking the cmd command so that, in turn, it invokes another additional command, cls . The generation of a new console, as well as complex, does not sound right ... what would happen if you start writing in this second console instead of in the first one? Your program will receive absolutely nothing. To clean the screen you can opt for a slightly simpler version:

system("cls");
  

Only the first entry data is read, skipping the others

To begin with, the following reading is incorrect:

scanf(" %c", n);

Since so that scanf can store the character read in n , it is necessary to pass a pointer to it, that is:

scanf(" %c", &n);

The rest of the program is not only good but works as expected ... changing the use of system , of course.

  

As you can see, first ask to type my name, but doing so no longer allows entering other input data

Error. To request a string of characters you have to use %s , but the thing does not end here, you also need a variable that can store the introduced sequence:

char n[100];
scanf(" %s",n);

Now you no longer need to provide the n reference since n has gone from being a variable of type char to an array of type char , and the arrays, in this case, behave exactly same as pointers.

  

in this only allows me to enter the three input data, the first and the third

I doubt it very much. Look at the first reading:

scanf("lf", &c);

The reading string does not have the percentage symbol % , then that scanf will not read anything . Fix that line and the program should work:

scanf("%lf", &c);
    
answered by 20.07.2018 в 11:22