[英]Char arrays and word counting
我花了最后兩個小時嘗試不同的方法來防止我在運行這個程序時一直得到的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;
}
}
您只分配了一個字符而不是數組
char *userInput = new char;
嘗試將上面的行更改為至少
char *userInput = new char[101];
你正在分配1個字節,並期望在那里適合100個字節:
char *userInput = new char;
你應該寫:
char *userInput = new char[101];
更好的是,避免使用原始指針,C字符串和new
。 在C ++中使用std::string
。
語句char *userInput = new char
只是創建一個指向char
的指針,而不是字符串。
如果你不喜歡std::string
,你必須在這里使用一個字符數組。
將char *userInput = new char
更改為char userInput[101]
。
此外,您必須在main()
之前聲明您的函數,以便可以從main()
調用它們。
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.