Restart or close a program by entering a word in C?

3

I have this code, the program does its thing and when it ends it indicates that to continue the 0 is entered 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;
}
    
asked by CarlosDayan 29.03.2017 в 20:22
source

2 answers

3

There is an easy solution for this, use an array of char :

#include <stdio.h>
#include <string.h>
#include <math.h>

//Funciones para Area

float areftcir(float r); //Area de un circulo

int main(void) {
    float ra, res;
    char cadena[10]; // Lo suficientemente grande como para aceptar "continuar"

    do {
        printf("\n\nIntrduzca el valor del radio: ");
        scanf("%f", &ra);
        res = areftcir(ra);
        printf("Respuesta: %f", res);

        printf("\n\nIngrese 'continuar' o 'salir': ");
        scanf("%s[^\n]", &cadena);    
    } while (strcmp(cadena, "continuar") == 0);
}

float areftcir(float r) {
    float raftc;
    raftc = (3.1416) * (r * r);
    return raftc;
}

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, without punctuation or anything, and will ignore any other words you enter.

Additionally, something bad can happen if you spend 10 characters.

I have changed your cycle while with a do { ... } while to perform the operations of the program at least once.

EDIT :

To make it more comfortable for the user, I have made a small improvement, which is to ignore the (Uppercase or Lowercase) typeface :

First, we add the function prototype:

char *str_tolower(char *ptr);

And we define it in the following way:

char *str_tolower(char *ptr) {
    char *test = ptr;
    while (ptr && *ptr) {
        *ptr = tolower(*ptr); ++ptr;
    }
    return test;
}

Then we just change the following line:

} while (strcmp(cadena, "continuar") == 0);

By:

} while (strmp(str_tolower(cadena), "continuar") == 0);

And now the user is able to write "continuar" in any way.

    
answered by 29.03.2017 / 20:37
source
1

An alternative response to the good Cloruro de Sodio (too salted for my taste ;-)).

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void) {
  char resp[255] = { 0 };

  while( 1 ) {
    printf("\n\nIntrduzca el valor del radio, o [salir]: " );
    scanf("%s", resp);

    if( !strcmp( resp, "salir" ) )
      break; 

    printf("Respuesta: %f\n", areftcir( strtof( resp ) );
  }
}

strtof( ) converts a string to float .

strcmp( ) compares strings. == 0 without without equal.

    
answered by 29.03.2017 в 20:51