所以有两个问题: 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;
}
答案 0 :(得分:0)
首先,numOccurence是int*
类型,而最高和最低类型是int
,默认情况下不具有可比性。这也适用于count
。
其次,除了收到用户输入的inputfile
之外,您的字符串为空,这不仅是因为您没有从文件中提取任何数据,还因为您的第一个for
循环计数器i
与currentLoc
进行比较,0<0
已初始化为0并且从未更改,并且由于get_posts()
为false,因此无法输入。
答案 1 :(得分:0)
您永远不会从ifstream文件中获取任何内容。你所做的只是在程序和文件之间打开一个流。所有流操作符(&lt;&lt;&lt;&lt;&gt;,&gt;,=,+ =等)将与您的&#34;文件一起使用&#34;现在变量和一个示例方法是。
字符串字; 而(文件&gt;&gt;字) {/ 已经在这里,你可以做所有的检查。你需要的字符串 / }