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"
.