Advertisement
YellowAfterlife

C++: String to float

Feb 21st, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <stdlib> // atof
  2. #include <stdio> // gets, printf
  3. #include <conio> // getch
  4.  
  5. int main(int argc, char* argv[])
  6. {
  7.     // пользователь вводит число:
  8.     char buff[256]; buff[0] = 0;
  9.     printf("Enter a number: ");
  10.     gets(buff);
  11.     // исправляем запятые на точки в введенных данных:
  12.     for (int i = 0; buff[i] != 0; i++) {
  13.         if (buff[i] != ',') continue;
  14.         buff[i] = '.';
  15.         break;
  16.     }
  17.     // преобразовываем и выводим:
  18.     float num = atof(buff);
  19.     printf("You entered: %f.\n", num);
  20.     getch();
  21.     return 0;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement