【发布时间】:2015-12-02 04:14:00
【问题描述】:
我收到了几个数组下标的错误,也没有匹配的函数来调用获取行...我不知道究竟是为什么。
错误:数组下标的类型“int[int]”无效
错误:数组下标的类型“double[int]”无效
错误:数组下标的类型“int[int]”无效
错误:没有匹配的函数调用“getline(bool)”
到目前为止,这是我的代码:
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
using namespace std;
struct CountiesFirst
{
int counties;
double poverty_rate;
double income;
};
const int MAX_COUNTIES = 10;
bool openFile(ifstream& input)
{
int counties;
double poverty_rate;
double income;
int i=0;
char filename[256];
cout << "Welcome to the Appalachian Poverty/Income Report\n";
cout << "Enter input file name: ";
cin >> filename;
input.open(filename);
getline(input, counties);
while (getline(!input.eof() && i < MAX_COUNTIES))
{
input>>counties[i];
input>> poverty_rate[i] >> income[i];
++i;
}
while (input.eof())
{
input.putback (i) ;
cout << "Input file is empty" << endl;
return i;
}
return !input.fail();
}
void printTable(string counties[],
double poverty_rate[],
double income[],
size_t size)
{
cout << "Counties Poverty Rate Salary\n";
cout << "---------------------------------------\n";
for (size_t i = 0; i < size; ++i)
{
cout.width(17);
cout << left << counties[i];
cout << poverty_rate[i] << "%";
cout.width(13);
cout << right << "$" << income[i] << endl;
}
}
int main()
{
CountiesFirst counties [MAX_COUNTIES];
int filename;
ifstream input;
ofstream output;
if (openFile(input))
{
//sort(counties, poverty_rate, income, size);
void printTable(string counties[], double poverty_rate[], double income[], int size);
}
else
{
cout << "Input file does not exist.\n";
exit(EXIT_FAILURE);
input.close();
output.close();
}
return 0;
}
【问题讨论】:
-
getline(!input.eof() && i < MAX_COUNTIES)这不是您使用此功能的方式。 cplusplus.com/reference/string/string/getline -
cin >> filename;这很危险。如果用户输入超过 255 个字符怎么办?使用std::string -
看来你的代码有无穷无尽的问题。如果我是你,我会从头重写代码。
-
几乎与c++ - invalid types 'int[int]' for array subscript - Stack Overflow 重复——除了在这种情况下维度是 0->1 而不是 3->4。