Problem with program in C, scanf does not respond

1

I have the following code in C:

#include <stdio.h>
#include <string.h>

struct alumnos{
char nombre[10];
char dni[20];
int edad;
};

int main(){
struct alumnos myAlumnos[2];
char opcion[20];
char salir[20] = "salir";
int i,status;

for(i=0;i<=2;i++){
    printf("Ingresa el nombre del alumno  numero %i: ",i+1);
    scanf("%s",&myAlumnos[i].nombre[10]);

    printf("Ingresa el dni del alumno numero %i: ",i+1);
    scanf("%s",&myAlumnos[i].dni[20]);

    printf("Ingresa la edad del alumno numero %i: ",i+1);
    scanf("%i",&myAlumnos[i].edad);
}

do{
    while(getchar() != "\n");
    status = 1;
    printf("Ingrese un dni: ");
    gets(opcion);
    if(strcmp(opcion,salir)!=0){
        i=0;
        while(i<=2 && status!=0){
            if(strcmp(myAlumnos[i].dni,opcion)==0){
                printf("Nombre del alumno: %s\nEdad: %i",myAlumnos[i].nombre,myAlumnos[i].edad);
                status=0;
            }
            i++;
        }
    }
}while(strcmp(opcion,salir)!=0);
}

And when I reach scanf("%i",&myAlumnos[i].edad); of the third iteration of for (boney when i == 2 ) I press enter to continue after entering the data and the program does nothing, I can continue writing on the screen but it's as if the enter key did not respond ... Any ideas?

I use CodeLite IDE in OSX El Capitan

    
asked by Cristofer Fuentes 13.07.2016 в 07:35
source

2 answers

2

By way of answer, a large part of your problem lies in this line:

while(getchar() != "\n");

Precisely in this part ; when there is no statement or sentence to execute, he simply stays in that% while .

But and if I send a \n from the keyboard, why do not you recognize it? I do not have this answer very clear, but it must be because it is not the last character to be sent.

Solution:

Fix that block with the following:

while(getchar() != "\n") 
{
    status = 1;
    printf("Ingrese un dni: ");
    gets(opcion);
    printf("%i", strcmp(opcion, salir));
    if(strcmp(opcion, salir) != 0) 
    {
        i = 0;
        while (i <= 2 && status != 0) 
        {
            if(strcmp(myAlumnos[i].dni, opcion) == 0) 
            {
                printf("Nombre del alumno: %s\nEdad: %i",myAlumnos[i].nombre,myAlumnos[i].edad);
                status = 0;
            }
            i++;
        }
    }
}

Oh wow, I was wrong, but we are all human!

Edit 2:

You are making a comparison of string to two data of type int ... They will never be the same: (

strcmp(myAlumnos[i].dni,opcion) == 0

The type char if it is an integral base type, but example: ' ' es igual a 32 but, myAlumnos[i].dni = 321 and opcion = "321" What is the value of the result of strcmp ? (He gave me -66).

And by way of addition, what you say dminones in your answer, is right, in other languages of programming you could not access the [2] of your Array if the size of this is precisely 2.

Edit:

I do not know if you understand very well the use of strcmp(str, str) but I will make a brief reference:

string A = "Hola";
string B = "Hola";
int Comparacion = strcmp(A, B); // Comparacion == 0

strcmp() returns zero if both strings are equal, any other number if they are not.

So I do not think you should compare within if : (strcmp(opcion,salir)!=0) . rather I think you should compare if there is a record with that ID, if it does not exist, the condition established in the cycle do .. while will be automatically responsible for knowing if what was entered on the keyboard was "salir" .

Reference: strcmp

    
answered by 13.07.2016 / 17:49
source
0

you should use < since your array has 2 values not 3

for(i=0;i<2;i++)

Or put 3 in the array definition

struct alumnos myAlumnos[3];
    
answered by 13.07.2016 в 15:40