【发布时间】:2014-09-13 04:33:02
【问题描述】:
我正在做一个 C++ 作业,旨在教我们更多关于对象和 OOP 的知识。下面是我的代码。它的要点是,用户输入一些输入,程序会计算元音或辅音(由用户选择)的数量、输入的字符总数和行尾总数。
我遇到了三个问题:
- 我注释掉的代码部分在留下时会导致无限循环。它会导致
countChars函数的输出被无限打印,以及询问用户是否愿意输入的输出更多输入。 -
countChars函数未正确计算 EOL。我认为这很可能是由于我对 EOL 不熟悉。我如何在我的条件语句中表示它?如果我想说“如果它的值为'0'则增加”,我说if (variable == 0)。如果某事是 EOL,我如何告诉 C++ 递增? -
countChars输出计数的随机负值。我确实注意到值会根据我输入的内容而变化(EOL 除外),但我不确定为什么会得到负值。除了使用unsigned int和初始化值之外,我不确定如何解决它。
另外,我预见人们会告诉我使用 getline 函数,但我们有非常具体的使用说明来使用 cin.get(毕竟我们应该学习一点东西)所以请避免使用getline.
头文件:
/*
+----------------------------------------+
| CountChars |
+----------------------------------------+
| -countVorC : Integer |
| -countEOL : Integer |
| -totalChars : Integer |
| -vowelCount : Boolean |
+----------------------------------------+
| <<constructor>> |
| CountChars() |
| +inputChars() : |
| +vowelCheck(characterToCheck : Boolean)|
| +setVowelCount(VorC : Character) |
| +getCountVorC() : Integer |
| +getCountEOL() : Integer |
| +getTotalChars() : Integer |
| +getVowelCount() : Boolean |
+----------------------------------------+
*/
using namespace std;
#ifndef COUNTCHARS_H
#define COUNTCHARS_H
class CountChars
{
private:
unsigned int countVorC;
unsigned int countEOL;
unsigned int totalChars;
bool vowelCount;
public:
CountChars();
void inputChars();
bool vowelCheck(char characterToCheck);
void setVowelCount(char VorC);
int getCountVorC();
int getCountEOL();
int getTotalChars();
bool getVowelCount();
};
#endif
实现文件:
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdio>
#include "CountChars.h"
using namespace std;
CountChars::CountChars()
{
unsigned int countVorC = 0;
unsigned int countEOL = 0;
unsigned int totalChars = 0;
bool vowelCount = false;
}
void CountChars::inputChars()
{
int letter;
while ((letter = cin.get()) != EOF && letter != EOF){
if (vowelCount == true && (vowelCheck(letter) == true)) {
countVorC++;
}
else if (vowelCount == false && (vowelCheck(letter) == false)) {
countVorC++;
}
if (isalpha(letter)) {
totalChars++;
}
if (letter == '\n') {
countEOL++;
}
}
}
bool CountChars::vowelCheck(char characterToCheck)
{
characterToCheck = toupper(characterToCheck);
if ((isalpha(characterToCheck)) &&
(characterToCheck == 'A' || characterToCheck == 'E' ||
characterToCheck == 'I' || characterToCheck == 'O' ||
characterToCheck == 'U')) {
return true;
}
else {
return false;
}
}
void CountChars::setVowelCount(char VorC)
{
VorC = toupper(VorC);
if (VorC == 'V') {
vowelCount = true;
}
else {
vowelCount = false;
}
}
int CountChars::getCountVorC()
{
return countVorC;
}
int CountChars::getCountEOL()
{
return countEOL;
}
int CountChars::getTotalChars()
{
return totalChars;
}
bool CountChars::getVowelCount()
{
return vowelCount;
}
主要:
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cstdio>
#include "CountChars.h"
using namespace std;
void printCounts(CountChars);
int main()
{
char VorC;
char repeat = 'Y';
CountChars charCounter;
cout << "Welcome to the Character Counter Program!" << endl;
cout << "\nWould you want to count vowels or consonants?" << endl;
cout << "Type 'V' for vowels and 'C' for consonants: ";
cin >> VorC;
cout << endl;
while (toupper(VorC) != 'V' && toupper(VorC) != 'C') {
cout << "\nSorry, that was an invalid choice. Please try again: ";
cin >> VorC;
cout << endl;
}
do {
cout << "You may being typing input below.\n" << endl;
charCounter.setVowelCount(VorC);
charCounter.inputChars();
cin.clear();
printCounts(charCounter);
cout << "\nWould you like to enter new input?" << endl;
cout << "Type 'Y' for yes or 'N' for no: ";
cin >> repeat;
cout << endl;
while (toupper(repeat) != 'Y' && toupper(repeat) != 'N') {
cout << "\nSorry, that was an invalid choice. Please try again: ";
cin >> repeat;
cout << endl;
}
} while (toupper(repeat) == 'Y');
cout << "\nThank you for using the Character Counter Program!\n" << endl;
system("pause");
return 0;
}
void printCounts(CountChars charCounter)
{
cout << "\nTotal characters: " << charCounter.getTotalChars() << endl;
if (charCounter.getVowelCount() == true) {
cout << "Total vowels: " << charCounter.getCountVorC() << endl;
}
else {
cout << "Total consonants: " << charCounter.getCountVorC() << endl;
}
cout << "Total end-of-lines: " << charCounter.getCountEOL() << endl;
}
【问题讨论】:
-
为什么在做
cin >> char(VorC);时要投射到char?你为什么不直接做cin >> VorC;? -
我想你想要 OR 运算符
|| -
我已经养成了将所有内容转换为我想要的类型的习惯,因为它很容易导致错误。无论如何,我不明白为什么这很重要,因为它不会导致任何错误。我只是重试了我的程序,去掉了铸件。所有的错误都仍然存在,不管是演员还是不演员。
-
@JasonSperske 我的代码中有很多条件语句。你建议我在哪里使用 OR 运算符?
-
@AxelFinkel: casting isn't always a good idea...
标签: c++ infinite-loop