0

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

4

2 回答 2

1

很清楚您要做什么,但是错误太多,很难知道从哪里开始。不幸的是,代码必须完全正确,而不是大致正确。

这是一个有效的程序。我认为你应该重新开始。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

static int subscript();
static void arrayInput(string* names_Array, double* rates_Array, double*hours_Array);

const string filename("employee sample file.txt");

int main()
{
    int INDEXES = subscript();
    string* names_Array = new string[INDEXES];
    double* rates_Array = new double[INDEXES];
    double* hours_Array = new double[INDEXES];
    arrayInput(names_Array, rates_Array, hours_Array);
    cout << rates_Array[0] << endl
         << rates_Array[1] << endl
         << rates_Array[2] << endl
         << rates_Array[3] << endl
         << rates_Array[4] << endl;
}

static int subscript()
{
    ifstream employeeInfo(filename.c_str());
    string name;
    double rate, hour;
    int count = 0;
    while (employeeInfo >> name >> rate >> hour)
        ++count;
    return count;
}

static void arrayInput(string* names_Array, double* rates_Array, double* hours_Array)
{
    ifstream employeeInfo(filename.c_str());
    string name;
    double rate, hour;
    int count = 0;
    while (employeeInfo >> name >> rate >> hour)
    {
        names_Array[count] = name;
        rates_Array[count] = rate;
        hours_Array[count] = hour;
        ++count;
    }
}

为了帮助理解,我保留了相同的变量和函数名称(尽管我认为其中一些有点奇怪)。

也许研究这段代码和你的代码之间的差异会有所帮助。我不知道。

于 2012-11-23T22:26:03.363 回答
1

在您的subscript函数中,已到达文件末尾,您尝试在函数中employeeInfo再次读取文件内容。arrayInput您需要告诉employeeInfo 从头开始​​读取。

void arrayInput(ifstream& employeeInfo, string names_Array[], double rates_Array[], 
            double hours_Array[], string& names, double& rates, double& hours)
{
    int i = 0;
    string line;

    employeeInfo.seekg (0, ios::beg);  // <-- add this line to point to the start of the file again

    while(getline(employeeInfo, line))
    {
        employeeInfo >> names >> rates >> hours;

        names_Array[i] = names;
        rates_Array[i] = rates;
        hours_Array[i] = hours;

        i++;
    }
}
于 2012-11-23T22:26:52.970 回答