微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C++,切换菜单不会让我添加新的输入

如何解决C++,切换菜单不会让我添加新的输入

我正在处理课堂作业。我需要制作一个使用菜单来计算元音和辅音的程序。在菜单中,我需要有一个选项 D“请输入一个新字符串”。但是,当我输入 d 时,cout 字符串输出,然后是整个菜单,没有机会输入新字符串。除此之外,程序中的其他所有内容似乎都运行良好。

#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;
}

解决方法

您的代码的问题在于,当您按“d”时,程序不仅会运行 case d,而且还会运行代码以输入选择,因为它在同一个 do while 循环中。因此,您需要跳过选择输入。为此,如果已经完成的是将初始化变量 m 作为调节器。当按下 'd' 时,m 增加并且 if 语句变为 false,因此只输入字符串

     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);}

对于 switch case 'd'

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

将 getline 替换为 ignore 并不能解决问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。