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. using namespace std;
  14.  
  15. //function prototypes
  16. string capitalizeFirst(char *);
  17. void resetInput();
  18.  
  19. const int LENGTH = 200; //allocates enough memory for the char array
  20.  
  21. int main()
  22. {
  23. char input[LENGTH];
  24.  
  25. //Validate user input
  26. do {
  27.  
  28. // Get input from the user to test capitalization function
  29. cout << "Enter a few sentences, without capitalizing the first word of each sentence.\n";
  30. cin.getline(input, LENGTH);
  31.  
  32. if (!isalpha(input))
  33. {
  34. cout << "Whoops! You must enter letters!\n"
  35. << "Enter a few sentences, without capitalizing the first word of each sentence.\n";
  36. resetInput();
  37. cin.getline(input, LENGTH);
  38. }
  39.  
  40. }while(!isalpha(input));
  41.  
  42. cout << capitalizeFirst(input) << endl;
  43.  
  44. return 0;
  45. }
  46.  
  47.  
  48.  
  49.  
  50. /********************************************************************
  51. * Function: string capitalizeFirst(char *strPtr)
  52. * Purpose: Function accepts a C-string argument entered by the user. The function
  53. then capitalizes the first word of every sentence, returning the newly formated
  54. * Parameters: char *strPtr - a pointer to a C-string entered by the user.
  55. * Return: Returns the new formated pointer strPtr
  56. ********************************************************************/
  57. string capitalizeFirst(char *strPtr)
  58. {
  59. for (int i = 0; i < strlen(strPtr); i++)
  60. {
  61. if(ispunct(strPtr[i]) || i == 0)
  62. {
  63. if (isalpha(strPtr[i]))
  64. {
  65. strPtr[i] = toupper(strPtr[i]);
  66. }
  67.  
  68. if(isspace(strPtr[i+1]))
  69. {
  70. strPtr[i+2] = toupper(strPtr[i+2]);
  71. i += 2;
  72. }
  73. }
  74. }
  75. return strPtr;
  76. }
  77.  
  78. /********************************************************************
  79. * Function: void resetInput()
  80. * Purpose: This function may be called to reset the input apparatus
  81. by flushing the cin buffer. Also provides code reuability since
  82. we are testing multiple input queries
  83. * Parameters: none
  84. * Return: none
  85. ********************************************************************/
  86. void resetInput()
  87. {
  88. // flush cin's buffer to start next input request with a clean slate
  89. cin.clear();
  90. cin.ignore(100, '\n');
  91.  
  92. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
hey. how are u.
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:32:21: error: no matching function for call to ‘isalpha(char [200])’
   if (!isalpha(input))
                     ^
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:111:1: note: candidate: int isalpha(int) <near match>
 __exctype (isalpha);
 ^
/usr/include/ctype.h:111:1: note:   conversion of argument 1 would be ill-formed:
prog.cpp:32:21: error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
   if (!isalpha(input))
                     ^
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:2595:5: note: candidate: template<class _CharT> bool std::isalpha(_CharT, const std::locale&)
     isalpha(_CharT __c, const locale& __loc)
     ^~~~~~~
/usr/include/c++/6/bits/locale_facets.h:2595:5: note:   template argument deduction/substitution failed:
prog.cpp:32:21: note:   candidate expects 2 arguments, 1 provided
   if (!isalpha(input))
                     ^
prog.cpp:40:23: error: no matching function for call to ‘isalpha(char [200])’
  }while(!isalpha(input));
                       ^
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:111:1: note: candidate: int isalpha(int) <near match>
 __exctype (isalpha);
 ^
/usr/include/ctype.h:111:1: note:   conversion of argument 1 would be ill-formed:
prog.cpp:40:23: error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
  }while(!isalpha(input));
                       ^
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:2595:5: note: candidate: template<class _CharT> bool std::isalpha(_CharT, const std::locale&)
     isalpha(_CharT __c, const locale& __loc)
     ^~~~~~~
/usr/include/c++/6/bits/locale_facets.h:2595:5: note:   template argument deduction/substitution failed:
prog.cpp:40:23: note:   candidate expects 2 arguments, 1 provided
  }while(!isalpha(input));
                       ^
stdout
Standard output is empty