fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. double getDouble(char message[]){
  5.  
  6. double val;
  7. double var = 0.0;
  8. char input[80];
  9.  
  10. do{
  11. printf("%s\n", message);
  12. fgets(input, 80, stdin);
  13.  
  14. if (input[strlen(input)-1] == '\n'){
  15. input[strlen(input)-1] = '\0';
  16. }
  17. val = atof(input);
  18.  
  19. if (val != 0.0){
  20. var = 1.0;
  21. }
  22. else{
  23. printf("Error. Please try again.(Only entering numbers is valid)\n");
  24. }
  25. }while(var == 0.0);
  26. printf("The result is: %f\n", val);
  27.  
  28. return val;
  29.  
  30.  
  31. }
  32.  
  33. int main() {
  34. double x = getDouble("Enter a real number: ");
  35. printf("The number you entered was %g\n", x);
  36. }
Success #stdin #stdout 0s 2172KB
stdin
137.42
stdout
Enter a real number: 
The result is: 137.420000
The number you entered was 137.42