C++ C++;,开关菜单不允许我添加新的输入
我正在做课堂作业。我需要做一个程序,使用菜单来计算元音和辅音。在菜单中,我需要一个选项D“请输入新字符串”。然而,当我输入d时,cout字符串输出,然后是整个菜单,没有机会输入新字符串。除此之外,程序中的其他一切似乎都很好C++ C++;,开关菜单不允许我添加新的输入,c++,menu,switch-statement,C++,Menu,Switch Statement,我正在做课堂作业。我需要做一个程序,使用菜单来计算元音和辅音。在菜单中,我需要一个选项D“请输入新字符串”。然而,当我输入d时,cout字符串输出,然后是整个菜单,没有机会输入新字符串。除此之外,程序中的其他一切似乎都很好 #include<iostream> #include <iomanip> #include <ctype.h> using namespace std; int countVowel(char*); int countConst(cha
#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;
}
#包括
#包括
#包括
使用名称空间std;
整数元音(字符*);
int countConst(字符*);
int main()
{
常数int SIZE=101;
字符输入[大小];
内誓,内骗;
字符选择;
您的代码的问题是,当您按“d”时,程序不仅运行case d,而且还运行代码来输入choice,因为它在同一个do-while循环中。因此,您需要跳过choice输入。为此,if-have所做的是初始化变量m作为调节器。当按下“d”时,m递增,if-statement变为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);}
if(m%2==0)
{在堆栈溢出问题上,这里肯定会有无数重复的问题。但是基本上,当你按d
时,你也要按Enter键。因为你正在将选项读入char
,所以回车键留在流中。你可以读你的“选项”改为使用std::getline
,或者您可以使用cin.ignore
放弃所有内容,直到换行,或者您可以使用std::ws
I/O操纵器删除前导空间。太棒了,这就成功了,谢谢。
case 'd':
cout << "Please enter a sentence. (100 characters max.)" << endl;
m++;
cin.getline(input,SIZE);
cout << endl;
break;