Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

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] != ''; 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] != ''; 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;
}
question from:https://stackoverflow.com/questions/65895758/c-switch-menu-will-not-let-me-add-new-input

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem with your code is that, when you press 'd',the program not only runs case d, however it also runs the code to enter choice as it is in the same do while loop.Therefore, you need to skip the choice input. For that what if have done is that initialized variable m as a moderator. When 'd' is pressed, m is incremented and the if statement becomes false, therefore taking input only the string

     if(m%2==0)
    {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);}

for the switch case 'd'

case 'd':
        cout << "Please enter a sentence. (100 characters max.)" << endl;
         m++;
         cin.getline(input,SIZE);
         cout << endl;
        break;

Replacing getline with ignore doesn't solve the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...