gpt4 book ai didi

c++ - 菜单驱动逻辑错误导致死循环

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:45 24 4
gpt4 key购买 nike

全部。

这是我的第一篇文章。我很感激我能得到的所有帮助。

我已经测试了这些功能。他们工作正常。该程序的目的如下:

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 != '\0')
{
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 != '\0')
{
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 != '\0')
{
if (isalpha (tolower(*strPtr)))
{
alphaCount++;
}
strPtr++;
}

return alphaCount;
}

最佳答案

问题是变量 choice 被声明为 int 类型。因此,当您输入字符时,会发生输入流错误。在这种情况下,任何其他输入都将被忽略。

像这样声明变量

char choice;

考虑到这些变量

char a,b,c,d,e,f;

没有在程序中使用。

像这样的条件

choice == tolower('a')

没有什么意义。

随便写

choice == 'a'

关于c++ - 菜单驱动逻辑错误导致死循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49590895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com