簡體   English   中英

字數統計程序

[英]Word count program

所以有兩個問題:1)當我到達我試圖計算最低和最高的部分時,所有 >、< 和 = 都在它們下面出現紅色波浪線,並顯示錯誤消息“操作數類型不兼容(” int*" 和 "int")" 然后當我設置斷點時,我發現我的文件中的單詞甚至沒有被放入字符串中,我所有的字符串都是空白的。 請幫忙!

#include <iostream>

#include <fstream>
#include <string>
#include <cmath>

using namespace std;

struct numStringOccurrences //this brings the words into the struct as a string form
{
string wordFromFile;
int numOccurrence;
};

int countOccurrences(numStringOccurrences strOcc[], int pos) //this counts how many times the words show up
{
int wordCounts = 0;

for (int i = 0; i < 1000; i++)
{
if (strOcc[i].wordFromFile == strOcc[pos].wordFromFile)
wordCounts++;
}
return wordCounts;
}

int main()
{
string inputFile, wordFromFile;
bool wordFound = false;
string wordArray[10000];
int numOccurrence[10000];
int currentLoc = 0;

cout << "Good day! What is the input file name?: " << endl;
cin >> inputFile;

// Open file.
ifstream file(inputFile.c_str());

how to do 

this counts the highest and lowest word count and outputs it.
for (int i = 0; i < currentLoc; i++)
{
if (wordArray[i] == wordFromFile)
{
numOccurrence[i]++;
wordFound = true;
break;
}


if (wordFound == false)
{
wordArray[currentLoc] = wordFromFile;
currentLoc++;
}
}

// counts the lowest and highest occurrences.
int highest = numOccurrence[0];
int lowest = numOccurrence[0];


for (int count = 0; count < numOccurrence; count++)

{

if (numOccurrence > highest);
{
highest = numOccurrence;
}
if (numOccurrence < lowest)
{
lowest = numOccurrence;
}
//Display the largest and smallest value of the array
cout << "The highest value in the array is: " << highest << "\nThe lowest value in the array is: " << lowest << endl;


system("Pause");
return 0;
}

首先, numOccurence 是int*類型,而最高和最低是int類型,默認情況下它們不可比較。 這也適用於count

其次,除了接收用戶輸入的inputfile之外,您的字符串是空的,不僅因為您沒有從文件中提取任何數據,還因為在您的第一個for循環中,計數器i與在 0 中初始化的currentLoc進行了比較並且永遠不會改變,因為0<0是假的,所以它不會進入。

您永遠不會從 ifstream 文件中獲取任何內容。 您所做的只是在程序和文件之間打開一個流。 所有流操作符(<<、>>、=、+= 等)現在都將與您的“文件”變量一起使用,一個示例方法是。

字符串詞; While (file >> word) { /已經在這里你可以做所有的檢查了。 你需要的字符串/ }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM