fork download
  1. /*
  2.   Student: Colin Redpath
  3.   Class: CSC-134-301
  4.   Assignment: Chapter 10 Homework #1
  5.   Due Date: 4/28/2017
  6.   Description: Sentence Capitalizer (5)
  7. Write a function that accepts a pointer to a C-string as an argument and capitalizes the first character of each sentence in the strPtring. For instance, if the string argument is “hello. my name is Joe. what is your name?” the function should manipulate the strPtring so it contains “Hello. My name is Joe. What is your name?” Demonstrate the function in a program that asks the user to input a string and then passes it to the function. The modified strPtring should be displayed on the screen.
  8. */
  9. #include <iostream>
  10. #include <string>
  11. #include <string.h> //needed for strlen
  12. #include <cctype> //needed for char testing
  13. #include <iomanip>
  14. using namespace std;
  15.  
  16. //function prototypes
  17. string capitalizeFirst(char *);
  18. void resetInput();
  19.  
  20. const int LENGTH = 200; //allocates enough memory for the char array
  21.  
  22. int main()
  23. {
  24. char input[LENGTH];
  25.  
  26. //Validate user input
  27. do {
  28.  
  29. // Get input from the user to test capitalization function
  30. cout << "Enter a few sentences, without capitalizing the first word of each sentence.\n";
  31. cin.getline(input, LENGTH);
  32.  
  33. if (isdigit(input, LENGTH))
  34. {
  35. cout << "Whoops! You must enter letters!\n"
  36. << "Enter a few sentences, without capitalizing the first word of each sentence.\n";
  37. resetInput();
  38. cin.getline(input, LENGTH);
  39. }
  40.  
  41. }while(isdigit(input, LENGTH));
  42.  
  43. cout << capitalizeFirst(input) << endl;
  44.  
  45. return 0;
  46. }
  47.  
  48.  
  49.  
  50.  
  51. /********************************************************************
  52. * Function: string capitalizeFirst(char *strPtr)
  53. * Purpose: Function accepts a C-string argument entered by the user. The function
  54. then capitalizes the first word of every sentence, returning the newly formated
  55. * Parameters: char *strPtr - a pointer to a C-string entered by the user.
  56. * Return: Returns the new formated pointer strPtr
  57. ********************************************************************/
  58. string capitalizeFirst(char *strPtr)
  59. {
  60. for (int i = 0; i < strlen(strPtr); i++)
  61. {
  62. if(ispunct(strPtr[i]) || i == 0)
  63. {
  64. if (isalpha(strPtr[i]))
  65. {
  66. strPtr[i] = toupper(strPtr[i]);
  67. }
  68.  
  69. if(isspace(strPtr[i+1]))
  70. {
  71. strPtr[i+2] = toupper(strPtr[i+2]);
  72. i += 2;
  73. }
  74. }
  75. }
  76. return strPtr;
  77. }
  78.  
  79. /********************************************************************
  80. * Function: void resetInput()
  81. * Purpose: This function may be called to reset the input apparatus
  82. by flushing the cin buffer. Also provides code reuability since
  83. we are testing multiple input queries
  84. * Parameters: none
  85. * Return: none
  86. ********************************************************************/
  87. void resetInput()
  88. {
  89. // flush cin's buffer to start next input request with a clean slate
  90. cin.clear();
  91. cin.ignore(100, '\n');
  92.  
  93. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
hello. what is  your name. 
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:33:28: error: no matching function for call to ‘isdigit(char [200], const int&)’
   if (isdigit(input, LENGTH))
                            ^
In file included from /usr/include/c++/6/cctype:42:0,
                 from /usr/include/c++/6/bits/localefwd.h:42,
                 from /usr/include/c++/6/ios:41,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:9:
/usr/include/ctype.h:113:1: note: candidate: int isdigit(int)
 __exctype (isdigit);
 ^
/usr/include/ctype.h:113:1: note:   candidate expects 1 argument, 2 provided
In file included from /usr/include/c++/6/bits/basic_ios.h:37:0,
                 from /usr/include/c++/6/ios:44,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:9:
/usr/include/c++/6/bits/locale_facets.h:2601:5: note: candidate: template<class _CharT> bool std::isdigit(_CharT, const std::locale&)
     isdigit(_CharT __c, const locale& __loc)
     ^~~~~~~
/usr/include/c++/6/bits/locale_facets.h:2601:5: note:   template argument deduction/substitution failed:
prog.cpp:33:28: note:   cannot convert ‘LENGTH’ (type ‘const int’) to type ‘const std::locale&’
   if (isdigit(input, LENGTH))
                            ^
prog.cpp:41:30: error: no matching function for call to ‘isdigit(char [200], const int&)’
  }while(isdigit(input, LENGTH));
                              ^
In file included from /usr/include/c++/6/cctype:42:0,
                 from /usr/include/c++/6/bits/localefwd.h:42,
                 from /usr/include/c++/6/ios:41,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:9:
/usr/include/ctype.h:113:1: note: candidate: int isdigit(int)
 __exctype (isdigit);
 ^
/usr/include/ctype.h:113:1: note:   candidate expects 1 argument, 2 provided
In file included from /usr/include/c++/6/bits/basic_ios.h:37:0,
                 from /usr/include/c++/6/ios:44,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from prog.cpp:9:
/usr/include/c++/6/bits/locale_facets.h:2601:5: note: candidate: template<class _CharT> bool std::isdigit(_CharT, const std::locale&)
     isdigit(_CharT __c, const locale& __loc)
     ^~~~~~~
/usr/include/c++/6/bits/locale_facets.h:2601:5: note:   template argument deduction/substitution failed:
prog.cpp:41:30: note:   cannot convert ‘LENGTH’ (type ‘const int’) to type ‘const std::locale&’
  }while(isdigit(input, LENGTH));
                              ^
stdout
Standard output is empty