fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. long double mstold(char str[])
  7. {
  8. const int NUM_LEN = 100;
  9. char number[NUM_LEN];
  10. int str_len = strlen(str);
  11. int j = 0;
  12. for(int i = 0; i < str_len; ++i)
  13. {
  14. if(str[i] >= '0' && str[i] <= '9')
  15. number[j++] = str[i];
  16. }
  17. number[j] = '\0';
  18. long double result_num = strtold(number, NULL);
  19. return result_num;
  20. }
  21.  
  22. int main()
  23. {
  24. const int STR_SIZE = 128;
  25. char choice;
  26. do
  27. {
  28. char str[STR_SIZE];
  29. cout << "Enter a money string: ";
  30. cin >> str;
  31.  
  32. cout << str << endl;
  33.  
  34. long double dd = mstold(str);
  35. cout << dd << endl;
  36.  
  37. cout << "One more try? (y/n) ";
  38. cin >> choice;
  39. } while(choice == 'y');
  40. return EXIT_SUCCESS;
  41. }
Success #stdin #stdout 0s 3344KB
stdin
323.45 n
stdout
Enter a money string: 323.45
32345
One more try? (y/n)