菜单驱动逻辑错误导致死循环
Menu Driven Logic Error Causing Infinite Loop
所有。
这是我的第一个 post。我很感激我能得到的所有帮助。
我已经测试了功能。他们工作正常。程序的目的如下:
1) 用户输入一个字符串
2) 显示菜单
3) 用户选择
a) 计算元音
b) 数辅音
c) 计算字符串字母
d) 输入另一个字符串
e) 退出
4) 菜单一直循环直到用户选择E
我知道我在使用指针时遇到循环问题很尴尬,但事实就是如此。
感谢您对我简单的头脑的耐心等待!
代码如下:
#include<iostream>
#include<cctype>//Case conversion
using namespace std;
//FUNCTION PROTOTYPES
int countVowels(char *);
int countCons(char *);
int countAlpha(char *);
//FUNCTION MAIN
int main()
{
const int SIZE = 50;
char inputString[SIZE];
char a,b,c,d,e,f;
int choice,
totalVowels,
totalConsonants,
totalLetters;
//Get the string from the user
cout << "Please enter a string consisting of 49 characters or less. ";
cin.getline(inputString, SIZE);
do{
cout << "\nPlease make a selection from the menu:\n"
<< "a) Counts the vowels\n"
<< "b) Counts the connsonants\n"
<< "c) Counts the string\n"
<< "d) Enter another string\n"
<< "e) Quit" << endl;
cout << "\nChoice is: ";
cin >> choice;
while (choice < 'a' || choice > 'e')
{
cout << "Invalid choice. Try again ";
cin >> choice;
}
//Call a function to count the vowels
if (choice == tolower('a'))
{
totalVowels = countVowels(inputString);
cout << "\n" << totalVowels << " Vowels" << endl;
}
//Call a function to count the vowels
else if (choice == tolower('b'))
{
totalConsonants = countCons(inputString);
cout << "\n" << totalConsonants << " Consonants" << endl;
}
//Call a function to count all letters
else if (choice == tolower('c'))
{
totalLetters = countAlpha(inputString);
cout << "\n" << totalLetters << " Letters" << endl;
}
//Write a new string
else if (choice == tolower('d'))
{
cout << "Please enter a string consisting of 49 characters or less.
";
cin.getline(inputString, SIZE);
}
} while (choice != tolower('e'));
return 0;
}
//FUNCTION DEFINITIONS
////Count the Vowels
int countVowels(char* strPtr)
{
int vowelCount = 0; //Each time a vowel is counted
while (*strPtr != '[=10=]')
{
if (tolower(*strPtr) == 'a'
||tolower(*strPtr) == 'e'
||tolower(*strPtr)== 'i'
||tolower(*strPtr) == 'o'
||tolower(*strPtr)== 'u')
{
vowelCount++;
}
strPtr++;
}
return vowelCount;
}
//Count the Consonants
int countCons(char* strPtr)
{
int conCount = 0; //Each time a consonant is counted
while (*strPtr != '[=10=]')
{
if (tolower(*strPtr) != 'a'
&& tolower(*strPtr) != 'e'
&& tolower(*strPtr) != 'i'
&& tolower(*strPtr) != 'o' //Clean this up
&& tolower(*strPtr) != 'u' // See section 10.1 for more
&& tolower(*strPtr) != ' '
&& tolower(*strPtr) != ','
&& tolower(*strPtr) != '?'
&& tolower(*strPtr) != '.'
&& tolower(*strPtr) != '!')
{
conCount++;
}
strPtr++;
}
return conCount;
}
//Count the Letters in the string
int countAlpha(char* strPtr)
{
int alphaCount = 0;
while (*strPtr != '[=10=]')
{
if (isalpha (tolower(*strPtr)))
{
alphaCount++;
}
strPtr++;
}
return alphaCount;
}
问题是变量 choice
被声明为 int
类型。因此,当您输入字符时,会发生输入流错误。在这种情况下,任何其他输入都将被忽略。
像这样声明变量
char choice;
考虑到这些变量
char a,b,c,d,e,f;
没有在程序中使用。
还有这样的条件
choice == tolower('a')
没有什么意义。
随便写
choice == 'a'
所有。
这是我的第一个 post。我很感激我能得到的所有帮助。
我已经测试了功能。他们工作正常。程序的目的如下:
1) 用户输入一个字符串
2) 显示菜单
3) 用户选择
a) 计算元音 b) 数辅音 c) 计算字符串字母 d) 输入另一个字符串 e) 退出
4) 菜单一直循环直到用户选择E
我知道我在使用指针时遇到循环问题很尴尬,但事实就是如此。
感谢您对我简单的头脑的耐心等待!
代码如下:
#include<iostream>
#include<cctype>//Case conversion
using namespace std;
//FUNCTION PROTOTYPES
int countVowels(char *);
int countCons(char *);
int countAlpha(char *);
//FUNCTION MAIN
int main()
{
const int SIZE = 50;
char inputString[SIZE];
char a,b,c,d,e,f;
int choice,
totalVowels,
totalConsonants,
totalLetters;
//Get the string from the user
cout << "Please enter a string consisting of 49 characters or less. ";
cin.getline(inputString, SIZE);
do{
cout << "\nPlease make a selection from the menu:\n"
<< "a) Counts the vowels\n"
<< "b) Counts the connsonants\n"
<< "c) Counts the string\n"
<< "d) Enter another string\n"
<< "e) Quit" << endl;
cout << "\nChoice is: ";
cin >> choice;
while (choice < 'a' || choice > 'e')
{
cout << "Invalid choice. Try again ";
cin >> choice;
}
//Call a function to count the vowels
if (choice == tolower('a'))
{
totalVowels = countVowels(inputString);
cout << "\n" << totalVowels << " Vowels" << endl;
}
//Call a function to count the vowels
else if (choice == tolower('b'))
{
totalConsonants = countCons(inputString);
cout << "\n" << totalConsonants << " Consonants" << endl;
}
//Call a function to count all letters
else if (choice == tolower('c'))
{
totalLetters = countAlpha(inputString);
cout << "\n" << totalLetters << " Letters" << endl;
}
//Write a new string
else if (choice == tolower('d'))
{
cout << "Please enter a string consisting of 49 characters or less.
";
cin.getline(inputString, SIZE);
}
} while (choice != tolower('e'));
return 0;
}
//FUNCTION DEFINITIONS
////Count the Vowels
int countVowels(char* strPtr)
{
int vowelCount = 0; //Each time a vowel is counted
while (*strPtr != '[=10=]')
{
if (tolower(*strPtr) == 'a'
||tolower(*strPtr) == 'e'
||tolower(*strPtr)== 'i'
||tolower(*strPtr) == 'o'
||tolower(*strPtr)== 'u')
{
vowelCount++;
}
strPtr++;
}
return vowelCount;
}
//Count the Consonants
int countCons(char* strPtr)
{
int conCount = 0; //Each time a consonant is counted
while (*strPtr != '[=10=]')
{
if (tolower(*strPtr) != 'a'
&& tolower(*strPtr) != 'e'
&& tolower(*strPtr) != 'i'
&& tolower(*strPtr) != 'o' //Clean this up
&& tolower(*strPtr) != 'u' // See section 10.1 for more
&& tolower(*strPtr) != ' '
&& tolower(*strPtr) != ','
&& tolower(*strPtr) != '?'
&& tolower(*strPtr) != '.'
&& tolower(*strPtr) != '!')
{
conCount++;
}
strPtr++;
}
return conCount;
}
//Count the Letters in the string
int countAlpha(char* strPtr)
{
int alphaCount = 0;
while (*strPtr != '[=10=]')
{
if (isalpha (tolower(*strPtr)))
{
alphaCount++;
}
strPtr++;
}
return alphaCount;
}
问题是变量 choice
被声明为 int
类型。因此,当您输入字符时,会发生输入流错误。在这种情况下,任何其他输入都将被忽略。
像这样声明变量
char choice;
考虑到这些变量
char a,b,c,d,e,f;
没有在程序中使用。
还有这样的条件
choice == tolower('a')
没有什么意义。
随便写
choice == 'a'