Reading file into multi dimensional array

not sure what the difference is between the code for a standard array

Page 1 of 1

7 Replies - 3868 Views - Last Post: 09 May 2008 - 04:27 AM Rate Topic: -----

#1 warchilo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-January 07

Reading file into multi dimensional array

Posted 09 May 2008 - 02:51 AM

Hello, again I have hit a brick wall. the code below is how I believe this should be done. It works when I read into a standadr array but not for a multi dimensional array. Any help would be greatly appreciated.

#include "stdafx.h"
#include "gwin.h"
#include <iostream>
#include <fstream>

using namespace std;
int main() // starting point of program - the main function is always called this name
{
	// Variable declarations

	double account [3][8];
	ifstream myfile("finances.txt");
	int counter = 0;


	
	if (!myfile)
	{
		Gwin.writeText("Oh dear, one of your files could not be opened");
		Keyboard.getch();
		exit(1); //an error code to denote what has gone wrong
	}

	while (counter<24)
	{
		myfile >> account[counter];
		counter++;
	}
	myfile.close();

	Gwin.writeDouble(100,100,account[1][1]);


	// Finally, wait for a key to be pressed
	Keyboard.getch();

	return 0;
}



the error I get is

error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'double [8]' (or there is no acceptable conversion)

Thanks again for your help.

Warchilo.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading file into multi dimensional array

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Reading file into multi dimensional array

Posted 09 May 2008 - 03:33 AM

OK, well the best way to explain this would be:
You have 3 arrays which contain 8 elements each (account[3][8])
But, when it comes to reading the file, you are just trying to read directly into the array of 3. The way that you should think about doing it is something like this:
int counter1 = 0; // this will go up to 3, for the first array
int counter2 = 0; // this will go up to 8 for each array, then reset
while (counter1 < 3)
{
    myfile >> account[counter1][counter2];
    counter2 ++;
    if (counter2 = 8) // reset the counter to read in the next set
    {
        counter2 = 0; // reset to read from the beginning
        counter1 ++; // increment counter1 to read the next set
    }
}

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 warchilo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-January 07

Re: Reading file into multi dimensional array

Posted 09 May 2008 - 03:46 AM

Thank you for your help. It now appears that using the code you gave me the file is being read into the array. however when I try to display anything to the screen from the array I get a figure that is not contained in the file "finances.txt". the figure I get is -107374176.0000, could this be an address?

#include "stdafx.h"
#include "gwin.h"
#include <iostream>
#include <fstream>

using namespace std;
int main() // starting point of program - the main function is always called this name
{
	// Variable declarations

	float account [3][8];
	ifstream myfile("finances.txt");
	int counter1 = 0;
	int counter2 = 0;


	
	if (!myfile)
	{
		Gwin.writeText("one of your files could not be opened");
		Keyboard.getch();
		exit(1); //an error code to denote what has gone wrong
	}

	while (counter1<3)
	{
		myfile >> account[counter1][counter2];
		counter2++;
		if (counter2=8)
		{
			counter2=0;
			counter1++;
		}
	}
	myfile.close();


	Gwin.writeDouble(150,150,account[1][2]);

	// Finally, wait for a key to be pressed
	Keyboard.getch();

	return 0;
}



again thank you.

Warchilo.

This post has been edited by warchilo: 09 May 2008 - 03:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Reading file into multi dimensional array

Posted 09 May 2008 - 03:50 AM

huh? Do you still have a question?
Was This Post Helpful? 0
  • +
  • -

#5 warchilo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-January 07

Re: Reading file into multi dimensional array

Posted 09 May 2008 - 04:02 AM

Yeah sorry to be a pain mate, I just edited my post with the question I have.
Was This Post Helpful? 0
  • +
  • -

#6 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Reading file into multi dimensional array

Posted 09 May 2008 - 04:13 AM

Sorry, my bad :( there was a typo in the if statement, it should be == not =
Here it is, corrected:
#include <iostream>
#include <fstream>
#include <conio.h>

using namespace std;
int main() // starting point of program - the main function is always called this name
{
    // Variable declarations

    float account [3][8];
    ifstream myfile("finances.txt");
    int counter1 = 0;
    int counter2 = 0;



    if (!myfile)
    {
        cout << "The file could not be opened.";
        getch();
        exit(1); //an error code to denote what has gone wrong
    }

    while (counter1<3)
    {
        myfile >> account[counter1][counter2];
        counter2++;
        if (counter2==8)
        {
            counter2=0;
            counter1++;
        }
    }
    myfile.close();

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 8; j++)
            cout << account[i][j] << " ";

        cout << endl;
    }


    // Finally, wait for a key to be pressed
    getch();

    return 0;
}

I hade to replace Gwin.command with the standard stuff, you shouldn't have a problem rewriting it, but if you do, let me know and we'll go through it :)

By the way, you have a line that looks like this: exit(1); //an error code to denote what has gone wrong
That parameter (1) isn't an error code, it is a timer - the exit function will exit the program after a number of milliseconds, determined within the brackets. So, to exit after 1 second, it would be exit(1000);

Hope this helps :)

This post has been edited by gabehabe: 09 May 2008 - 04:20 AM

Was This Post Helpful? 0
  • +
  • -

#7 warchilo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-January 07

Re: Reading file into multi dimensional array

Posted 09 May 2008 - 04:21 AM

Mate your a legend, probably just saved me hours of stress! got an exam on next week and I will be required to write a similar program undertest conditions. I'll let you know how it goes.

Warchilo.
Was This Post Helpful? 0
  • +
  • -

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Reading file into multi dimensional array

Posted 09 May 2008 - 04:27 AM

Glad it works. Good luck with your exam :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1