私は C++ プログラミングが初めてで、明らかに何かが欠けています。次のコードでは、コンパイラはこの行を認識していないようです:lengths[counter] = findDups(crtLine);
エラーが発生しました: variable "lengths" set but not used. 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);
}