【发布时间】:2018-03-31 18:12:18
【问题描述】:
全部。
这是我的第一篇文章。感谢您在这方面获得的所有帮助。
我已经测试了这些功能。他们工作正常。该计划的目的如下:
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;
}
【问题讨论】:
-
欢迎来到 SO!您能否澄清一下究竟是什么问题(例如,观察到的行为与预期的不同,以及有何不同)和/或您在消息正文中明确提出的问题是什么? (点击“编辑”)
-
@greid 声明选择具有 char 类型。
标签: c++ loops menu infinite-loop