fork download
  1. // Module Ten Assignment.cpp
  2.  
  3. #include <iostream>
  4. #include <cctype>//for tolower
  5. #include <cstring>//for strcpy
  6.  
  7.  
  8. void displayMenu(){
  9. std::cout << "\na. Count the number of vowels in the string"
  10. << "\nb. Count the number of consonants in the string"
  11. << "\nc. Count both the vowels and consonants in the string"
  12. << "\nd. Enter another String"
  13. << "\ne. Exit the program" << std::endl;
  14. }
  15.  
  16.  
  17. int vowelCount(char* enteredString, const int SIZE){
  18. char vowelList[] = {'a', 'e', 'i', 'o', 'u'};
  19. int vowelsFound{0};
  20. for (int count = 0;count < SIZE;++count){
  21. for (auto vowelElement : vowelList){
  22. if(std::tolower(*(enteredString + count)) == vowelElement)
  23. ++vowelsFound;
  24. }
  25. }
  26. return vowelsFound;
  27. }
  28.  
  29.  
  30.  
  31. int consonantCount(char* enteredString, const int SIZE){
  32. char consonantList[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'};
  33. int consonantsFound{0};
  34. for (int count = 0;count < SIZE;++count){
  35. for (auto consonantElement : consonantList){
  36. if(std::tolower(*(enteredString + count)) == consonantElement)
  37. ++consonantsFound;
  38. }
  39. }
  40. return consonantsFound;
  41. }
  42.  
  43.  
  44.  
  45. char* getString(){
  46. std::cout << "Please type whatever you want: " <<std::endl;
  47. char *rawString = new char[1000];//1000 chosen arbitrarily as length constraint
  48. std::cin.getline(rawString, 999);
  49. char *fixedString = new char[std::strlen(rawString) + 1];//dynamically allocate a fixed amount of space based on input string. + 1 is for newline character
  50. strcpy_s(fixedString, std::strlen(rawString) + 1, rawString);//copies the input from the arbitrarily large string to the fixed one
  51. return fixedString;
  52. }
  53.  
  54. char getMenuChoice(){
  55. char choice{'\0'};
  56. std::cin >> choice;
  57. return std::tolower(choice);
  58. }
  59.  
  60. int main()
  61. {
  62. char *enteredString = getString();
  63. size_t size{std::strlen(enteredString)};
  64. displayMenu();
  65.  
  66.  
  67. char menuSelection{getMenuChoice()};
  68. do{
  69. switch (menuSelection){
  70. case 'a':
  71. std::cout << "\nThe vowel count of your string is " << vowelCount(enteredString, size) << std::endl;
  72. break;
  73. case 'b':
  74. std::cout << "\nThe consonant count of your string is " << consonantCount(enteredString, size) << std::endl;
  75. break;
  76. case 'c':
  77. std::cout << "\nThe count of both the vowels and consonants in your string is " << vowelCount(enteredString, size) + consonantCount(enteredString, size) << std::endl;
  78. break;
  79. case 'd':
  80. std::cin.ignore();//clear newline character from previous c-string
  81. enteredString = getString();
  82. size = std::strlen(enteredString);
  83. displayMenu();
  84. break;
  85. default:
  86. std::cout << "\nInvalid Input. Please Try again." << std::endl;
  87. }
  88. menuSelection = getMenuChoice();
  89. }while(menuSelection != 'e');
  90. }
  91.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘char* getString()’:
prog.cpp:50:61: error: ‘strcpy_s’ was not declared in this scope
  strcpy_s(fixedString, std::strlen(rawString) + 1, rawString);//copies the input from the arbitrarily large string to the fixed one
                                                             ^
stdout
Standard output is empty