Why does not the result of an arrangement print me?

0

I understand that up to a certain point my code is fine but in a certain part of the syntax it does not do the job well

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

main()
{
int a,b,c;
char nom[3][15],bus;

for(b=0;b<3;b++){
        for(c=0;c<1;c++){
            printf("Ingrese el nombre del alumno: ");
            fflush( stdin );
            scanf("%s",&nom[b][c]);
            printf("Ingrese la calificacion del alumno: ");
            fflush( stdin );
            scanf("%d",&a);
        }
}

printf("¿cual alumno deseea buscar? ");
fflush( stdin );
scanf(" %s", &bus);

if(nom[b]=="bus"){
    printf("Nomre: %s Calificacion: %d", nom[b][c], a);
}

getch();
}
    
asked by Victor 23.11.2017 в 08:08
source

1 answer

1

Errors and things to improve in your code:

Use of fflush

fflush( stdin );

This function should only be used with output devices. He says it very clearly in the documentation :

  

In all other cases, the behavior depends on the specific library implementation. In some implementations, flushing a stream open for reading causes its input buffer to be cleared ( but this is not portable expected behavior ).

Be careful when reading strings

scanf("%s",&nom[b][c]);

There are much cleaner ways to pass the pointer to the beginning of the character string:

  • nom is a double pointer char**
  • nom[b] is a simple pointer char*
  • nom[b][c] is a% char

Since the function is asking you for a char* , what you would like to put (for clarity) at this point is:

scanf("%s",nom[b]);

tabulate the code

The well-tabulated code is easy to follow and understand, while the one that is badly tabulated (or is not tabulated) is another song.

Care when comparing strings

if(nom[b]=="bus")

Surely you have thought that this instruction will take the two chains and compare them to each other to know if they are the same ... it does not do exactly that.

We're in C, which is almost the simplest thing just behind the assembler ... and here the code literally does what you ask ... but I literally do not mean that you do what you're thinking is going to do:

nom[b] is a pointer to char , and "bus" is a pointer to const char ... so the comparison operator is limited to comparing those pointers, that is, compares their addresses of memory . If both pointers point to the same address, the result will be true ... it's not what you expected.

To compare character strings you have to use a function or an algorithm that you want to work with ... to start you can use strcmp :

if( strcmp(nom[b],"bus") == 0 )

How does strcmp work? Take the two pointers and compare them character to character until you find a difference or reach the end of the chain. The comparison is done by subtracting the two characters in question ... if the result is different from zero, they are different. Thus, if the returned value is zero then nom[b]=="bus" .

    
answered by 23.11.2017 / 08:32
source