字符数组和字数统计

时间:2013-11-19 16:11:05

标签: c++ cstring arrays

我花了最后两个小时尝试不同的方法来防止我在运行此程序时遇到的APPCRASH错误,但我没有任何运气。基本上,这一切都是一个简单的单词计数程序,用于作业。它工作正常,直到它显示程序运行时找到的字数。此时,它只会冻结一两秒钟,然后弹出一个APPCRASH错误,然后关闭。我不知道为什么会这样,有人想告诉我哪里出错了?

    int main()
{
    //word counting programs
    char *userInput = new char;
    input(userInput);
    displayResults(wordCount(userInput), userInput);    
    return 0;
}

/***************************************
Definition of function - input         *
prompts the user to input a sentence   *
and stores the sentence into a char    *
array.                                 *
Parameter: char []                     *
****************************************/
void input(char userInput[])
{
    cout << "Enter a sentence (no more than 100 characters) and I will count the words:" << endl;
    cin.getline(userInput, 101);
}

/***************************************
Definition of function - wordCount     *
Accepts the input char array and counts*
the words in the sentence. Returns an  *
int value with the word count.         *
Parameter: char []                     *
Returns: an int with the word count    *
****************************************/
int wordCount(char* userInput)
{
    int count = 0;
    int words = 1;
    if(userInput[0] == '\0')
    {
        words = 0;
    }
    else if(userInput[0] == ' ' || userInput[0] == '\t')
    {
        cout << "Error: can not use a whitespace as the first character!" << endl;
        words = -1;
    }
    else
    {
        while(userInput[count] != '\0')
        {
            if(userInput[count] == ' ')
            {
                words++;
            }
            count++;
        }
    }
    return words;
}

/***************************************
Definition of function - displayResults*
Displays the word count for the user   *
entered input.                         *
****************************************/
void displayResults(int wordCountResult, char userInput[])
{
    if(wordCountResult == -1)
        cout << "Error reading input!" << endl;
    else if(wordCountResult == 0)
        cout << "Nothing was entered." << endl;
    else
    {
        cout << "You entered: " << userInput << endl;
        cout << "That contains " << wordCountResult << " word(s)!" << endl;
    }
}

4 个答案:

答案 0 :(得分:1)

您只分配了一个字符而不是数组

char *userInput = new char;

尝试将上面的行至少更改为

char *userInput = new char[101];

答案 1 :(得分:1)

你正在分配1个字节,并希望在那里适合100个字节:

char *userInput = new char;

你应该写:

char *userInput = new char[101];

更好的是,避免使用原始指针,C字符串和new。在C ++中使用std::string

答案 2 :(得分:0)

语句char *userInput = new char只是创建一个指向char的指针,而不是字符串 如果你不喜欢std::string,你必须在这里使用一个字符数组 将char *userInput = new char更改为char userInput[101]

此外,您必须在main()之前声明您的功能,以便可以从main()调用它们。

答案 3 :(得分:0)

using namespace std;

void input(char userInput[]);
int wordCount(char* userInput);
void displayResults(int wordCountResult, char userInput[]);



 int main()
{
    //word counting programs
    char *userInput = new char;
    input(userInput);
    displayResults(wordCount(userInput), userInput);    
    return 0;
}

/***************************************
Definition of function - input         *
prompts the user to input a sentence   *
and stores the sentence into a char    *
array.                                 *
Parameter: char []                     *
****************************************/
void input(char userInput[])
{
    cout << "Enter a sentence (no more than 100 characters) and I will count the words:" << endl;
    cin.getline(userInput, 101);
}

/***************************************
Definition of function - wordCount     *
Accepts the input char array and counts*
the words in the sentence. Returns an  *
int value with the word count.         *
Parameter: char []                     *
Returns: an int with the word count    *
****************************************/
int wordCount(char* userInput)
{
    int count = 0;
    int words = 1;
    if(userInput[0] == '\0')
    {
        words = 0;
    }
    else if(userInput[0] == ' ' || userInput[0] == '\t')
    {
        cout << "Error: can not use a whitespace as the first character!" << endl;
        words = -1;
    }
    else
    {
        while(userInput[count] != '\0')
        {
            if(userInput[count] == ' ')
            {
                words++;
            }
            count++;
        }
    }
    return words;
}

/***************************************
Definition of function - displayResults*
Displays the word count for the user   *
entered input.                         *
****************************************/
void displayResults(int wordCountResult, char userInput[])
{
    if(wordCountResult == -1)
        cout << "Error reading input!" << endl;
    else if(wordCountResult == 0)
        cout << "Nothing was entered." << endl;
    else
    {
        cout << "You entered: " << userInput << endl;
        cout << "That contains " << wordCountResult << " word(s)!" << endl;
    }
}