2016-07-27 55 views
-4

我遇到了一個與我的程序有關的問題。這裏的程序應該接受一個指向C字符串的指針作爲參數,並計算字符串中包含的單詞數以及字符串中的字母數。這兩個值都應該傳回主函數,但不使用全局變量。在該函數之後,我應該編寫另一個接受字母數量和字數的函數,並將每個字的平均字母數(或平均字大小)發送回主函數。我寫的功能應該完成上述所有功能,並且還可以從計數中排除標點符號和空格。我遇到的問題是,當我以用戶身份輸入字符串並按下回車鍵時,不會進行任何計算。我可以繼續按下直到沒有結束,我無法弄清楚爲什麼會發生。任何洞察力將不勝感激,我是新的指針和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 != '\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; 
} 
+0

什麼打印? –

+0

輸入字符串後,沒有任何東西被打印出來,你按下回車鍵,它只是開始新的行。 – Zjm4192

回答

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; 
} 
+0

我應該在最後一個支架的上方放一個突破點嗎? – Zjm4192

+0

@ Zjm4192一個'strptr ++'並且對字符串結尾的測試應該足夠了。 – user4581301

+0

@ Zjm4192更新了我的答案。 – slavanap

0

你永遠前進strptr,所以所有的比較都是針對同一charCount_All循環下去。

您應該將index++的所有實例替換爲。這將導致strptr向下移動字符串並查看每個字符,而不是一遍又一遍地看同一個字符。這樣做後index未使用,可以刪除。

此外,您的最終應位於其上方的if區塊內,以避免意外跳過您的字符串末端並進入未知字符串。

更好的辦法是使用std::string並擺脫使用原始char[] s產生的所有這些問題。

+0

包括替換 int index = 0 還是保持不變? – Zjm4192

+0

已更新。你永遠不會使用'index',所以你根本不需要它。 –

+0

好吧,我可以刪除「int index = 0」,然後通過strptr ++替換所有的「index ++」? – Zjm4192

0

一開始,好像你有無限循環這裏

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

這裏

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

你總是檢查同一位置的內容沒有做任何指針運算推進你正在檢查的內存位置。