Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
虽然循环导致无限循环,但我可以';我不明白为什么 我正在做一个C++的作业,目的是教我们更多关于对象和面向对象的知识。下面是我的代码。其要点是,用户输入一些输入,程序计算元音或辅音(由用户选择)的数量、输入的字符总数以及行尾的总数_C++_Infinite Loop - Fatal编程技术网

虽然循环导致无限循环,但我可以';我不明白为什么 我正在做一个C++的作业,目的是教我们更多关于对象和面向对象的知识。下面是我的代码。其要点是,用户输入一些输入,程序计算元音或辅音(由用户选择)的数量、输入的字符总数以及行尾的总数

虽然循环导致无限循环,但我可以';我不明白为什么 我正在做一个C++的作业,目的是教我们更多关于对象和面向对象的知识。下面是我的代码。其要点是,用户输入一些输入,程序计算元音或辅音(由用户选择)的数量、输入的字符总数以及行尾的总数,c++,infinite-loop,C++,Infinite Loop,我有三个问题: 我注释掉的代码部分在保留时会导致无限循环。它会使countChars函数的输出无限打印,以及询问用户是否希望输入更多输入的输出 countChars函数未正确计算下线。我认为这很可能是因为我不熟悉EOL。我如何在我的条件语句中表示它?如果我想说,“如果它的值为'0',则递增”,我会说,If(variable==0)。如果某个东西是EOL,我如何告诉C++来增加?< /LI> countChars为计数输出随机负值。我确实注意到这些值根据我键入的内容(EOL除外)而变化,但我不确定

我有三个问题:

  • 我注释掉的代码部分在保留时会导致无限循环。它会使
    countChars
    函数的输出无限打印,以及询问用户是否希望输入更多输入的输出
  • countChars
    函数未正确计算下线。我认为这很可能是因为我不熟悉EOL。我如何在我的条件语句中表示它?如果我想说,“如果它的值为'0',则递增”,我会说,
    If(variable==0)
    。如果某个东西是EOL,我如何告诉C++来增加?< /LI>
  • countChars
    为计数输出随机负值。我确实注意到这些值根据我键入的内容(EOL除外)而变化,但我不确定为什么会得到负值。除了使用
    无符号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
    
    /*
    +----------------------------------------+
    |伯爵|
    +----------------------------------------+
    |-countVorC:Integer|
    |-countEOL:整数|
    |-totalChars:Integer|
    |-元音计数:布尔值|
    +----------------------------------------+
    |                         |
    |CountChars()|
    |+输入字符():|
    |+元音检查(characterToCheck:Boolean)|
    |+设置元音计数(VorC:字符)|
    |+getCountVorC():整数|
    |+getCountEOL():整数|
    |+getTotalChars():整数|
    |+GetVouelCount():布尔值|
    +----------------------------------------+
    */
    使用名称空间std;
    #ifndef COUNTCHARS\u H
    #定义COUNTCHARS\u H
    类CountChars
    {
    私人:
    无符号整数;
    无符号整数计数;
    无符号整数totalChars;
    布尔元音计数;
    公众:
    CountChars();
    void inputChars();
    布尔元音检查(charcharactertocheck);
    无效设置元音计数(字符VorC);
    int getCountVorC();
    int getCountEOL();
    int getTotalChars();
    bool getvuelCount();
    };
    #恩迪夫
    
    实施文件:

    #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;
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括“CountChars.h”
    使用名称空间std;
    CountChars::CountChars()
    {
    无符号int countVorC=0;
    无符号整数计数=0;
    无符号整数totalChars=0;
    布尔元音计数=假;
    }
    void CountChars::inputChars()
    {
    整数字母;
    while((letter=cin.get())!=EOF&&letter!=EOF){
    if(元音计数==true&(元音检查(字母)==true)){
    countVorC++;
    }
    else if(元音计数==false&(元音检查(字母)==false)){
    countVorC++;
    }
    如果(伊萨尔法(信)){
    totalChars++;
    }
    如果(字母=='\n'){
    countEOL++;
    }
    }
    }
    boolcountchars::元音检查(characterToCheck)
    {
    characterToCheck=toupper(characterToCheck);
    if((isalpha(characterToCheck))&&
    (characterToCheck='A'| | characterToCheck=='E'||
    characterToCheck=='I'| | characterToCheck=='O'||
    characterToCheck=='U')){
    返回true;
    }
    否则{
    返回false;
    }
    }
    void CountChars::setvuelCount(char VorC)
    {
    VorC=toupper(VorC);
    如果(VorC='V'){
    元音计数=真;
    }
    否则{
    元音计数=假;
    }
    }
    int CountChars::getCountVorC()
    {
    返回计数器VORC;
    }
    int CountChars::getCountEOL()
    {
    返回计数;
    }
    int CountChars::getTotalChars()
    {
    返回totalChars;
    }
    boolcountchars::getvouelcount()
    {
    返回元音计数;
    }
    
    主要内容:

    #包括
    #包括
    #包括
    #包括
    #包括
    #包括“CountChars.h”
    使用名称空间std;
    无效打印计数(CountChars);
    int main()
    {
    char-VorC;
    字符重复='Y';
    CountChars charCounter;
    cout
    cin.get()
    返回一个
    int
    你有:

    char letter;
    
    while ((letter = cin.get()) != EOF)
    
    如果普通
    char
    是无符号类型,就像在某些机器上一样,那么这将永远不会计算为true,因为值
    -1
    (EOF的正常值)被分配给一个(无符号)
    char
    ,它被映射到0xFF,并且当
    0xFF
    int
    类似
    EOF
    (这仍然是
    -1
    ),答案为false,因此循环继续

    解决方法是使用
    int-letter
    而不是
    char-letter
    。(请注意,如果
    char
    是有符号类型,则编写的代码会出现不同的问题;然后,代码为0xFF的字符(通常为ÿ、y umlaut、U+00FF、带DIAERESIS的拉丁小写字母y)会被误解为EOF。修复方法相同;使用
    int-LETTER;

    不过,我怀疑这只是问题的一部分


    EOF不是EOL 在同一功能中,您还具有:

        if (letter == EOF) {
            countEOL++;
        }
    
    您知道
    字母
    不是EOF(因为循环检查了这一点)。此外,您希望计算EOL,而不是EOF(每个文件只有一个EOF,但如果您继续尝试读取EOF之外的内容,您将重复返回EOF)。您可能需要:

        if (letter == '\n') {
            countEOL++;
        }
    
    或者您想定义EOL并与之进行比较:

        const int EOL = '\n';
        if (letter == EOL) {
            countEOL++;
        }
    

    cin
    在输入中保留换行符 在守则中:

    cout << "Type 'V' for vowels and 'C' for consonants: ";
    cin >> char(VorC);
    cout << endl;
    
    while (toupper(VorC) != 'V' && toupper(VorC) != 'C') {
        cout << "\nSorry, that was an invalid choice. Please try again: ";
        cin >> char(VorC);
        cout << endl;
    }
    
    读过新行

    再说一遍,这不是wh
    cout << "Type 'V' for vowels and 'C' for consonants: ";
    cin >> char(VorC);
    cout << endl;
    
    while (toupper(VorC) != 'V' && toupper(VorC) != 'C') {
        cout << "\nSorry, that was an invalid choice. Please try again: ";
        cin >> char(VorC);
        cout << endl;
    }
    
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    /*do {
        cout << "You may being typing input below.\n" << endl;*/
    
        charCounter.setVowelCount(VorC);
        charCounter.inputChars();
    
        /*cout << "Would you like to enter new input?";
        cout << "Type 'Y' for yes or 'N' for no: " << endl;
        cin >> char(repeat);
        cout << endl;
            while (toupper(repeat) != 'Y' && toupper(repeat) != 'N') {
                cout << "\nSorry, that was an invalid choice. Please try again: ";
                cin >> char(repeat);
                cout << endl;
            }
    } while (toupper(repeat) == 'Y');*/
    
    int letter;
    
    while ((letter = cin.get()) != EOF && letter != '\n')  // Or EOL if you define EOL as before
    
    $ g++ -O3 -g -std=c++11 -Wall -Wextra -Werror -c CountChars.cpp
    CountChars.cpp: In constructor ‘CountChars::CountChars()’:
    CountChars.cpp:13:18: error: unused variable ‘countVorC’ [-Werror=unused-variable]
         unsigned int countVorC = 0;
                      ^
    CountChars.cpp:14:18: error: unused variable ‘countEOL’ [-Werror=unused-variable]
         unsigned int countEOL = 0;
                      ^
    CountChars.cpp:15:18: error: unused variable ‘totalChars’ [-Werror=unused-variable]
         unsigned int totalChars = 0;
                      ^
    CountChars.cpp:16:10: error: unused variable ‘vowelCount’ [-Werror=unused-variable]
         bool vowelCount = false;
              ^
    cc1plus: all warnings being treated as errors
    $
    
    $ g++ -O3 -g -std=c++11 -Wall -Wextra -Werror -c Main.cpp
    Main.cpp: In function ‘int main()’:
    Main.cpp:18:9: error: ambiguous overload for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘char’)
         cin >> char(VorC);
             ^
    Main.cpp:18:9: note: candidates are:
    In file included from /usr/gcc/v4.9.1/include/c++/4.9.1/iostream:40:0,
                     from Main.cpp:1:
    /usr/gcc/v4.9.1/include/c++/4.9.1/istream:120:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>] <near match>
           operator>>(__istream_type& (*__pf)(__istream_type&))
           ^
    /usr/gcc/v4.9.1/include/c++/4.9.1/istream:120:7: note:   no known conversion for argument 1 from ‘char’ to ‘std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}’
    /usr/gcc/v4.9.1/include/c++/4.9.1/istream:124:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>] <near match>
           operator>>(__ios_type& (*__pf)(__ios_type&))
           ^
    …
    $
    
    cin >> char(VorC);
    
    cin >> VorC;
    
    if (!(cin >> VorC)) …process EOF or error…