我是 C++ 编程的新手,显然遗漏了一些东西。在以下代码中,编译器似乎无法识别这一行:lengths[counter] = findDups(crtLine);
我收到一个错误:设置了变量“长度”但未使用。我无法弄清楚为什么它不能识别这条线,当names[counter] = getNameOnly(crtLine)
它完美地工作并且它基本上是相同的格式时。对此问题的任何见解表示赞赏!
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
string getNameOnly (string line) {
return line.substr(0, line.find(' '));
}
int findDups (string line) {
string lnCpy = line;
sort(lnCpy.begin(), lnCpy.end());
int i = 0;
int dups = 0;
int j = 1;
int len = lnCpy.length();
while (j < len) {
if (lnCpy.at(i) == lnCpy.at(j))
dups++;
i++;
j++;
}
if (dups != 0)
return 0;
else
return lnCpy.length();
}
int main() {
string names[1219];
int lengths[1219];
string crtLine;
int counter = 0;
ifstream myfile ("dist.male.first");
if (myfile.is_open()) {
while (!myfile.eof()) {
getline(myfile,crtLine);
lengths[counter] = findDups(crtLine);
names[counter] = getNameOnly(crtLine);
counter++;
}
myfile.close();
}
else cout << "Unable to open file";
return (0);
}