Merso1913

input

Nov 8th, 2019
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. long double my_input2() {
  2.     long double data;
  3.     int len = 0;
  4.     char ch = getc(stdin);
  5.     char* str = (char*)malloc(sizeof(char));
  6.     char** endptr = nullptr;  // переменная для strtod()
  7.     bool yea = 1;   // можем ввести символ или нет в цикле и удачное ли считывание в return
  8.  
  9.     while (ch != '\n') {            // считываем строку
  10.  
  11.         if (ch == 48) {
  12.             yea = 1;
  13.         } else if (ch != ' ') {
  14.             yea = 1;
  15.         } else if (ch != 48) {
  16.             yea = 0;
  17.         }
  18.         str[len] = ch;
  19.         str = (char*)realloc(str,(1 + (++len)*sizeof(char)));
  20.         ch = getc(stdin);
  21.     }
  22.  
  23.     data = strtod(str, endptr);
  24.     if (!data && !yea) {
  25.         printf("Vvedite eshe raz:\n");
  26.         return my_input2();
  27.     } else {
  28.         return data;
  29.     }
  30. }
Add Comment
Please, Sign In to add comment