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

Eric after dark
  • 1,768
  • 4
  • 31
  • 79
  • 1
    Your code is a bit bizarre. Why the reference parameters to your function? But it's not wrong. The bug is somewhere in the code you didn't post. Perhaps you could show the code where you call your function, and the code where you print the values out. – john Nov 23 '12 at 22:03
  • How do you call `arrayInput`? – Cameron Nov 23 '12 at 22:04
  • Do you still have this: `int INDEXES = 0; string *names_Array = new string[INDEXES];` from [your previous question](http://stackoverflow.com/questions/13535382/c-how-to-let-the-indexes-of-an-dynamic-array-be-determined-by-lines-in-a-file) in your code? – jrok Nov 23 '12 at 22:07
  • Yes I do, give me a second and I'll just put all of it up – Eric after dark Nov 23 '12 at 22:11
  • Got to agree with jrok, that previous code had some serious misunderstandings. I think you've still got them. The problems are a lot more serious that this function. It looks like array indexing is one of the things you do understand. Unfortunately I/O and dynamic allocation are things you don't understand. – john Nov 23 '12 at 22:11
  • Would be good to see the input file as well, because I think the way you are reading that is wrong too. – john Nov 23 '12 at 22:15
  • Has subscript changed since that last post? – john Nov 23 '12 at 22:18
  • I don't think so, but it's up now – Eric after dark Nov 23 '12 at 22:19

2 Answers2

1

It clear what you are trying to do, but there's so many mistakes it's hard to know where to start. Unfortunately code has to be exactly right, not roughly right.

Here's a program that works. I think you should just start again.

#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;
    }
}

To aid understanding I've kept the same variable and function names (even though I think some of them are a bit weird).

Maybe studying the differences between this code and yours will help. I don't know.

john
  • 85,011
  • 4
  • 57
  • 81
  • I've been struggling with this all day, thank you. I will definitely be looking at the differences between the two. – Eric after dark Nov 23 '12 at 22:31
  • In case it wasn't clear the main mistake was what billz pointed out, you read through the file once, then you didn't reset the file so your second read in arrayInput always fails. That's why you had garbage values in your arrays. I don't think billz solution works however. I've solved that particular problem by opening the file twice, once in each function. It's the cleanest way of doing this. – john Nov 23 '12 at 22:37
  • I think you're right about that. The simplicity of your program compared to my original one is crazy. – Eric after dark Nov 23 '12 at 22:39
  • I've often noticed this. Beginners code is sometimes way too complicated. – john Nov 23 '12 at 22:40
1

In your subscript function, the employeeInfo has reached the end of the file and you tried to read file content again in arrayInput function. you need to tell employeeInfo to read from start again.

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++;
    }
}
billz
  • 44,644
  • 9
  • 83
  • 100