At the moment I am trying to store double values into indexes of an array. As far as I can tell I'm doing this right, but I must be wrong because for some reason when I run the program I get some pretty insane numerical values.
I could use some help figuring this out, thanks in advance.
ps: I think that the values being displayed are basically the last values stored at that specific memory location...
Code (Let me know if you need to see more):
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <new>
using namespace std;
int INDEXES = 0;
string *names_Array;
double *rates_Array;
double *hours_Array;
void subscript(ifstream&, int&, string&, double&, double&);
void arrayInput(ifstream&, 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);
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);
cout << rates_Array[0] << endl
<< rates_Array[1] << endl
<< rates_Array[2] << endl
<< rates_Array[3] << endl
<< rates_Array[4] << endl;
employeeInfo.close();
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;
string line;
while(getline(employeeInfo, line))
{
employeeInfo >> names >> rates >> hours;
names_Array[i] = names;
rates_Array[i] = rates;
hours_Array[i] = hours;
i++;
}
}
INPUT FILE (please ignore double spacing):
Clinton 10.00 10
Lincoln 5.00 50
Washington 32.00 35
Kennedy 4.99 45
Nixon 10.00 25
OUTPUT:
-6.27744e+066
-6.27744e+066
-6.27744e+066
-6.27744e+066
-6.27744e+066
OUTPUT should be:
10.00
5.00
32.00
4.99
10.00