用户输入字符串后,程序不计算功能

时间:2016-07-27 19:20:08

标签: c++ arrays string function pointers

我的程序遇到了问题。这里的程序应该接受一个指向C字符串作为参数的指针,并计算字符串中包含的字数以及字符串中的字母数。这两个值都应该传递回main函数,但不使用全局变量。在该函数之后,我应该编写另一个接受字母数和字数的函数,并将每个字的平均字母数(或平均字大小)发送回主函数。我写的函数应该完成以上所有操作,并且还要从计数中排除标点符号和空格。我遇到的问题是,当我输入字符串作为用户并按Enter键时,不会进行任何计算。我可以继续按Enter直到没有结束,我无法弄清楚为什么会发生这种情况。任何见解都会受到赞赏,我是指针和C-String的新手。

这是我的代码:

#include <iostream>
#include <cstring>
#include <iomanip>

using namespace std;

void Count_All(char*, int&, double&, int&); // Function prototype.
double Calc_Average (char*, int, int, double); // Function prototype.

int main()
{
    const int size = 500;
    char userString[size];
    int Word = 0;
    int Pun = 0;
    double Total_Characters = 0;
    double Average = 0.0;

    cout << "Please enter a string of 500 or less characters: ";
    cin.getline(userString, size);
    cout << "\n";

    Count_All (userString, Word, Total_Characters, Pun);
    cout << "Number of words in the string: " << Word << "\n";
    Average = Calc_Average (userString, Word, Pun, Total_Characters);
    cout <<"\nAverage number of letters per word: "<< fixed <<
    showpoint << setprecision(2) << Average << "\n" << endl;


    cin.ignore(1);
    return 0;
}

void Count_All (char*strptr, int &Word, double &Total_Characters, int &Pun) // Counts all characters and types.
{
    int index = 0;

    while (*strptr != '\0')
    {
        if ((isspace(*strptr)) || (ispunct(*strptr)))
        {
            while ((isspace(*strptr)) || (ispunct(*strptr)))
            {
                index++;
            }
        }

        if ((isalnum(*strptr)) || (ispunct(*strptr)))
        {
            Word++;
            while ((isalnum(*strptr))||(ispunct(*strptr)))
            {
                index++;
                Total_Characters++; // Counting the total printable characters (including digits and punctuation).

                if((ispunct(*strptr)))
                {
                    Pun++; // Counting punctuation.
                }

            }
        }
        index++;
    }
}

double Calc_Average(char*strptr, int Word, int Pun, double Total_Characters)  // Calculates the average number of characters per words.
{
    double Average = 0.0;
    Total_Characters = Total_Characters - Pun; // Subtracting punctuation from all of the characters in the string (not including spaces).
    Average = (Total_Characters / Word);
    return Average;
}

3 个答案:

答案 0 :(得分:0)

Count_All函数实现中有双无限循环。

我修复了你的两个功能并简化了它们。没有大的改变,对我来说很难。为什么total的类型为double?字符数如何可以是非整数?

void Count_All(const char* strptr, int& word, int& total, int& pun)
{
    bool last_isalnum = false;
    for (word = total = pun = 0; *strptr != '\0'; ++strptr) {
        bool cur_isalnum = isalnum(*strptr) != 0;
        bool cur_ispunct = ispunct(*strptr) != 0;
        if (last_isalnum != cur_isalnum)
            ++word;
        if (cur_ispunct)
            ++pun;
        if (cur_ispunct || cur_isalnum)
            ++total;
        last_isalnum = cur_isalnum;
    }
}

double Calc_Average(int word, int pun, int total)
{
    return static_cast<double>(total - pun) / word;
}

答案 1 :(得分:0)

您永远不会前进strptr,因此您的所有比较都会永远违反相同的charCount_All循环。

您应该将index++的所有实例替换为strptr++。这将导致strptr向下移动字符串并查看每个字符,而不是一遍又一遍地查看相同的字符。执行此操作后index未使用,可以删除。

此外,您的最终strptr++应位于其上方的if区域内,以避免意外地跨越字符串的末尾并进入未知区域。

更好的是使用std::string并摆脱因使用原始char[]而引起的所有这些问题。

答案 2 :(得分:0)

首先,你好像在这里有无限循环

while ((isspace(*strptr)) || (ispunct(*strptr))) (line 42)

在这里

while ((isalnum(*strptr)) || (ispunct(*strptr)))   (line 51)

您始终检查相同位置的内容,而不进行任何指针运算以提升您正在检查的内存位置。

相关问题