Curso de Programación en C/Prog89
								
								Ir a la navegación
				Ir a la búsqueda
				
					
								
							
		Prog89
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define PAIS "India"
 5 
 6 void ObtenRespuesta( void );
 7 
 8 char respuesta[40];
 9 
10 int main( void )
11 {
12   printf( "En que pais es el Taj Mahal? " );
13   ObtenRespuesta();
14 
15   while( strcmp( respuesta, PAIS ) )         //strcmp(): Compara dos strings 
16   {
17     printf( "No, incorrecto. Otra ves: " );
18     ObtenRespuesta();
19   }
20   puts( "¡Correcto!" );
21 
22   return 0;
23 }
24 
25 void ObtenRespuesta( void )
26 {
27   fgets( respuesta, 37, stdin );
28   if( *(respuesta + strlen( respuesta ) - 1) == '\n' )
29     *(respuesta + strlen( respuesta ) - 1) = '\0';
30 }
Resultado
[rrc@llawyr CClase]$ gcc -Wall -o Prog89 Prog89.c [rrc@llawyr CClase]$ ./Prog89 En que pais es el Taj Mahal? Italia No, incorrecto. Otra ves: Grecia No, incorrecto. Otra ves: india No, incorrecto. Otra ves: India ¡Correcto! [rrc@llawyr CClase]$