2021年1月25日星期一

C++, switch menu will not let me add new input

I am working on an assignment for class. I need to make a program that uses a menu to count vowels and consonants. In the menu I need to have an option D "Please enter a new string". However when I input d, the cout string outputs, then the whole menu, with no chance to input a new string. Everything else in the program seems to work fine besides this.

#include<iostream>  #include <iomanip>  #include <ctype.h>  using namespace std;    int countVowel(char*);  int countConst(char*);    int main()  {  const int SIZE = 101;  char input[SIZE];  int vow, con;  char choice;    cout << "Please enter a sentence. (100 characters max.)" << endl;  cin.getline(input, SIZE);      do  {      cout << "A) Count the number of vowels in the string" << endl;      cout << "B) Count the number of consonants in the string" << endl;      cout << "C) Count both the vowels and consonants in the string" << endl;      cout << "D) Enter another string" << endl;      cout << "E) Exit the program" << endl;        cout << "Please select an option" << endl;      cin >> choice;      choice = tolower(choice);        switch (choice)      {      case 'a':          vow = countVowel(input);          cout << "Number of vowels: " << vow << endl;          break;        case 'b':          con = countConst(input);          cout << "Number of consonants: " << con << endl;          break;        case 'c':          vow = countVowel(input);          con = countConst(input);          cout << "Number of vowels: " << vow << endl;          cout << "Number of consonants: " << con << endl;          break;        case 'd':          cout << "Please enter a sentence. (100 characters max.)" << endl;          cin.getline(input, SIZE);          cout << endl;          break;        case 'e':          break;        default:          cout << "Invalid input!" << endl;          break;      }  } while (choice != 'e');        return 0;  }    int countVowel(char *str)  {  int count = 0;    for (int i = 0; str[i] != '\0'; i++) {      if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||          str[i] == 'o' || str[i] == 'u' || str[i] == 'A' ||          str[i] == 'E' || str[i] == 'I' || str[i] == 'O' ||          str[i] == 'U')      {          count++;      }  }  return count;  }    int countConst(char *str)  {  int count = 0;    for (int i = 0; str[i] != '\0'; i++) {      if (str[i] == 'b' || str[i] == 'c' || str[i] == 'd' ||          str[i] == 'f' || str[i] == 'g' || str[i] == 'h' ||          str[i] == 'j' || str[i] == 'k' || str[i] == 'l' ||          str[i] == 'm' || str[i] == 'n' || str[i] == 'p' ||           str[i] == 'q' || str[i] == 'r' || str[i] == 's' ||          str[i] == 't' || str[i] == 'v' || str[i] == 'w' ||          str[i] == 'x' || str[i] == 'y' || str[i] == 'z' ||          str[i] == 'B' || str[i] == 'C' || str[i] == 'D' ||          str[i] == 'F' || str[i] == 'G' || str[i] == 'H' ||          str[i] == 'J' || str[i] == 'K' || str[i] == 'L' ||          str[i] == 'M' || str[i] == 'N' || str[i] == 'P' ||          str[i] == 'Q' || str[i] == 'R' || str[i] == 'S' ||          str[i] == 'T' || str[i] == 'V' || str[i] == 'W' ||          str[i] == 'X' || str[i] == 'Y' || str[i] == 'Z')      {          count++;      }  }  return count;  }  
https://stackoverflow.com/questions/65895758/c-switch-menu-will-not-let-me-add-new-input January 26, 2021 at 12:06PM

没有评论:

发表评论