I have this code, the program does its thing and when it finishes it indicates that to continue enter 0 or to close any other number, what I want to do is that to continue enter the word 'continue' or follow' and to close the word 'leave'
#include <stdio.h>
#include <string.h>
#include <math.h>
//Funciones para Area
float areftcir(float r); //Area de un circulo
int main(void) {
int resp = 0;
float ra, res;
while(resp == 0)
{
printf("\n\nIntrduzca el valor del radio: ");
scanf("%f", &ra);
res = areftcir(ra);
printf("Respuesta: %f", res);
printf("\n\nIngrese '0' para regresar: ");
scanf("%d", &resp);
}
}
float areftcir(float r) {
float raftc;
raftc = (3.1416) * (r * r);
return raftc;
}
There is an easy solution for this, use an array of
char
:Basically what it does is compare if the content of
cadena
is equivalent to"continuar"
.The biggest problem with this is that it will only work if the user types
continuar
in lowercase, no punctuation marks or anything, and it will ignore any other words you enter.Additionally, something bad can happen if you go past 10 characters.
I have changed your loop
while
with ado { ... } while
to perform the program operations at least once.EDIT :
To make it more comfortable for the user, I have made a small improvement, which is to ignore the font (Uppercase or Lowercase) :
First, we add the function prototype:
And we define it as follows:
Then we just change the following line:
By:
And now the user is able to write
"continuar"
in any way.An alternative answer to the good one
Cloruro de Sodio
(too salty for my taste ;-) ).strtof( )
convert a string tofloat
.strcmp( )
compare strings.== 0
without without equal.