Jump to content

C: Problems using char with %s and %c


#include <stdio.h>

#include <stdlib.h>

int main()

{

//Declaración de variables

char lcTipoVehiculo[10], lcHoraAlta[3];

int liNumeroPasajeros;

//Entrada de datos

printf("Ingrese tipo de veh%cculo (Cami%cn %c Autom%cvil): ", 161, 162, 162, 162);

scanf("%s", lcTipoVehiculo);

printf("N%cmero de pasajeros: ", 163);

scanf("%d", &liNumeroPasajeros);

printf("Hora de alta congesti%cn (Si %c No): ", 162, 162);

scanf("%s", lcHoraAlta);

//Procesamiento

if (lcHoraAlta == 'SI' || lcHoraAlta == 'si' && lcTipoVehiculo == 'AUTOMOVIL' || lcTipoVehiculo == 'automovil' && liNumeroPasajeros >= 3)

{

printf("\nN%c paga peaje.\n", 162);

}

else if (lcHoraAlta == 'SI' || lcHoraAlta == 'si' && lcTipoVehiculo == 'AUTOMOVIL' || lcTipoVehiculo == 'automovil' && liNumeroPasajeros < 3)

{

printf("\nTarifa: EUR$30.\n");

}

//Salida de datos

return 0;

}

The problem is that when entering data, the program stops and will not start reading any condition.

 

I have problems using char, with the %s an%c. I do not understand the difference and also with: char lcTipoVehiculo [10], lcHoraAlta [3];, not know exactly what number should I put.

 

I hope you can help me with this, I want to clarify my doubts using char.
Link to comment
https://linustechtips.com/topic/302291-c-problems-using-char-with-s-and-c/
Share on other sites

Link to post
Share on other sites

%s is used for string (null terminated array of char) while %c is used for single characters...

 

I haven't touched C for a long time but I do remember that using scanf(%s, var) is not recommended... try using fgets() instead...

 

put that into a conditional loop that would exit upon getting a valid input...

 

like so:

printf(/*prompt*/);while(!fgets()){  printf(/*prompt*/);}

check the function's manual pages for additional details...

 

Edit:

 

for lcTipoVehiculo[] and lcHoraAlta[], the number inside is the size of the array (use a larger number to avoid buffer overflows)... it would be wise to initialize it to null first...

 

If you wish to recall a member of the array, the values range from 0 to sizeof(Array)-1.

If you wish to print the entire array as a string, make sure it is null terminated and use printf(%s) or puts().

Link to post
Share on other sites

%s is used for string (null terminated array of char) while %c is used for single characters...

 

I haven't touched C for a long time but I do remember that using scanf(%s, var) is not recommended... try using fgets() instead...

 

put that into a conditional loop that would exit upon getting a valid input...

 

like so:

printf(/*prompt*/);while(!fgets()){  printf(/*prompt*/);}

check the function's manual pages for additional details...

 

Edit:

 

for lcTipoVehiculo[] and lcHoraAlta[], the number inside is the size of the array (use a larger number to avoid buffer overflows)... it would be wise to initialize it to null first...

 

If you wish to recall a member of the array, the values range from 0 to sizeof(Array)-1.

If you wish to print the entire array as a string, make sure it is null terminated and use printf(%s) or puts().

 

thanks men, but, if you open that progra, why it stops?

Link to post
Share on other sites

thanks men, but, if you open that progra, why it stops?

 

not sure... I don't have a compiler on me right now so I can't test it...

 

From my limited understanding of your language, your program is just to decide on something based on inputted values, right?

 

at what point does it stop?

 

Have you tested valid inputs? it could be that the values being put in are too large or are of an incompatible data type...

Link to post
Share on other sites

The reason the conditions don't trip is because the way you're interpreting your if conditions and the way the compiler is interpreting them are two different things. What you think is a string comparison the compiler sees as a pointer comparison, so there is no way the comparisons will ever yield a true result.

 

It sounds like you need to be using the std::string class to contain your strings so you get the comparison operators. Alternatively, you can use the strcmp function to do string comparisons as well.

Wife's build: Amethyst - Ryzen 9 3900X, 32GB G.Skill Ripjaws V DDR4-3200, ASUS Prime X570-P, EVGA RTX 3080 FTW3 12GB, Corsair Obsidian 750D, Corsair RM1000 (yellow label)

My build: Mira - Ryzen 7 3700X, 32GB EVGA DDR4-3200, ASUS Prime X470-PRO, EVGA RTX 3070 XC3, beQuiet Dark Base 900, EVGA 1000 G6

Link to post
Share on other sites

http://www.cplusplus.com/reference/cstring/strcmp/

 

You need to use strcmp() to compare strings.

 

Also, when using %s in scanf you should limit it to your buffer size with %Xs where X is your buffer size minus 1. Buffer overflows will cause you headaches.

 

Since your buffer size is 10 you would write:

 

scanf("%9s", lcTipoVehiculo);

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×