Advertisement
Syndragonic

C-String to Integer CS311

Aug 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cctype>
  4. using namespace std;
  5. void readAndClean (int& n);
  6. void newLine();
  7.  
  8. void readAndClean (int& n)
  9. {
  10. const int ARRAY_SIZE = 6;
  11. char digitString[ARRAY_SIZE];
  12.  
  13. char next;
  14. cin.get(next);
  15. int index = 0;
  16. while (next != '\n')
  17. {
  18. if ((isdigit(next)) && (index < ARRAY_SIZE - 1))
  19. {
  20. digitString[index] = next;
  21. index++;
  22. }
  23. cin.get(next);
  24. }
  25. digitString[index] = '\0';
  26. n = atoi(digitString);
  27. }
  28. void newLine()
  29. {
  30. char symbol;
  31. do
  32. {
  33. cin.get(symbol);
  34. }
  35. while (symbol != '\n');
  36. }
  37. int main()
  38. {
  39. int n;
  40. char ans;
  41. do
  42. {
  43. cout<<"Enter an integer and press Return: ";
  44. readAndClean(n);
  45. cout <<"That string converts to the integer " << n <<endl;
  46. cout <<"Again? (yes/no): ";
  47. cin>> ans;
  48. newLine();
  49. }
  50. while ( (ans != 'n') && (ans != 'N') );
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement