C ++:如何让动态数组的索引由文件中的行确定 [英] C++: How to let the indexes of an dynamic array be determined by lines in a file
问题描述
我正在尝试制作三个动态数组,其索引取决于文本文件中的行数。然后我需要使其索引值可修改。我在想全球阵列是我最好的选择。
由于某些原因我不断收到以下编译器错误:
secondLab.obj:error LNK2019:未解析的外部符号void __cdecl arrayInput(...)
问题,我如何解决这个问题,基本符合我的程序的目标。
这是我的代码: / p>
#include< iostream>
#include< fstream>
#include< iomanip>
#include< string>
#include< new>
使用namespace std;
int INDEXES = 0;
string * names_Array = new string [INDEXES];
double * rates_Array = new double [INDEXES];
double * hours_Array = new double [INDEXES];
void subscript (ifstream& int& string& double& double&);
void arrayInput(istream& string [],double [],double [],
string& double& ;);
int main()
{
string names;
double rates;
double hours;
string filename(employee sample file.txt);
ifstream employeeInfo(filename.c_str());
if(employeeInfo.fail())
{
cout<<< 对不起,文件没有成功打开。
<< 请确保您的文件存在,并且
< 重新运行程序。 << ENDL;
}
下标(employeeInfo,INDEXES,名称,费率,小时);
arrayInput(employeeInfo,names_Array,rates_Array,hours_Array,
名称,费率,小时);
cout<<< names_Array [0]<< endl
<< names_Array [1]<< endl
<< names_Array [2]<< endl
<< names_Array [3]<< endl
<< names_Array [4]<< ENDL;
delete [] names_Array;
delete [] rates_Array;
delete [] hours_Array;
system(pause);
return 0;
}
void下标(ifstream& employeeInfo,int& INDEXES,
string& names,double& rates,double& hours)
{
while (!employeeInfo.eof())
{
employeeInfo>>名称>>价格>>>小时;
INDEXES ++;
}
}
void arrayInput(ifstream& employeeInfo,string names_Array [],
double rates_Array [],double hours_Array [],string& names,double&费率,双倍和小时)
{
int i = 0;
while(!employeeInfo.eof())
{
employeeInfo>>名称>>价格>>>小时;
names_Array [i] = names;
rates_Array [i] = rate;
hours_Array [i] = hours;
i ++;
}
}
可以先计数行数,然后启动数组..并将数据填充到数组。
subscript(employeeInfo ,INDEXES,姓名,费率,小时);
names_Array = new string [INDEXES];
rates_Array = new double [INDEXES];
hours_Array = new double [INDEXES];
arrayInput(employeeInfo,names_Array,rates_Array,hours_Array,names,rates,hours);
尝试这样..
I'm trying to make three dynamic arrays whose indexes are determined by the number of lines in a text file. I then need to make their index values modifiable. I was thinking that global arrays would be my best bet.
For some reason I keep receiving the following compiler error: secondLab.obj : error LNK2019: unresolved external symbol "void __cdecl arrayInput(...)
Question, how do I fix this and basically meet the goal of my program.
Here's my code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <new>
using namespace std;
int INDEXES = 0;
string *names_Array = new string[INDEXES];
double *rates_Array = new double[INDEXES];
double *hours_Array = new double[INDEXES];
void subscript(ifstream&, int&, string&, double&, double&);
void arrayInput(istream&, string [], double [], double[],
string&, double&, double&);
int main ()
{
string names;
double rates;
double hours;
string filename("employee sample file.txt");
ifstream employeeInfo(filename.c_str());
if (employeeInfo.fail())
{
cout << "Sorry, file was not successfully opened. "
<< "Please make sure your file exists and\n"
<< "re-run the program." << endl;
}
subscript(employeeInfo, INDEXES, names, rates, hours);
arrayInput(employeeInfo, names_Array, rates_Array, hours_Array,
names, rates, hours);
cout << names_Array[0] << endl
<< names_Array[1] << endl
<< names_Array[2] << endl
<< names_Array[3] << endl
<< names_Array[4] << endl;
delete[] names_Array;
delete[] rates_Array;
delete[] hours_Array;
system("pause");
return 0;
}
void subscript(ifstream& employeeInfo, int& INDEXES,
string& names, double& rates, double& hours)
{
while(!employeeInfo.eof())
{
employeeInfo >> names >> rates >> hours;
INDEXES++;
}
}
void arrayInput(ifstream& employeeInfo, string names_Array[],
double rates_Array[], double hours_Array[], string& names, double& rates, double& hours)
{
int i = 0;
while(!employeeInfo.eof())
{
employeeInfo >> names >> rates >> hours;
names_Array[i] = names;
rates_Array[i] = rates;
hours_Array[i] = hours;
i++;
}
}
u can count the numbers of lines at first .. then initiate the arrays .. and filling data to arrays..
subscript(employeeInfo, INDEXES, names, rates, hours);
names_Array = new string[INDEXES];
rates_Array = new double[INDEXES];
hours_Array = new double[INDEXES];
arrayInput(employeeInfo, names_Array, rates_Array, hours_Array,names, rates, hours);
try like this ..
这篇关于C ++:如何让动态数组的索引由文件中的行确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!