用户输入字符串后,程序不计算函数

Program is not calculating functions after user enters string

本文关键字:计算 函数 程序 输入 字符串 用户      更新时间:2023-10-16

我的程序遇到了一个问题。这里的程序应该接受一个指向C字符串的指针作为参数,并计算字符串中包含的单词数以及字符串中的字母数。这两个值都应该传递回主函数,但不使用全局变量。在这个函数之后,我应该编写另一个函数,它接受字母数和单词数,并将每个单词的平均字母数(或平均单词大小)发送回主函数。我写的函数应该完成上述所有功能,并且从计数中排除标点符号和空格。不过,我遇到的问题是,当我以用户身份输入字符串并按enter键时,不会发生计算。我可以一直按回车键,直到没有尽头,但我不明白为什么会发生这种情况。任何见解都将不胜感激,我是指针和C字符串的新手。

这是我的代码:

#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 != '')
    {
        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;
}

您从未推进过strptr,因此所有的比较都将永远针对相同的charCount_All循环。

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

此外,您的最后一个strptr++应该在它上面的if块内,以避免意外地越过字符串的末尾而进入未知。

更好的方法是使用std::string,并消除使用原始char[] s时出现的所有这些问题。

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 != ''; ++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;
}

首先,这里似乎有无限循环

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

这里是

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

您总是在检查同一位置的内容,而不进行任何指针运算来推进您正在检查的内存位置。