Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 86,374 C++ Programmers. There are 1,421 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Reading file into multi dimensional array

 
Reply to this topicStart new topic

Reading file into multi dimensional array, not sure what the difference is between the code for a standard array

warchilo
post 9 May, 2008 - 02:51 AM
Post #1


New D.I.C Head

*
Joined: 15 Jan, 2007
Posts: 12



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.

CODE

#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.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


gabehabe
post 9 May, 2008 - 03:33 AM
Post #2


D.I.C Regular

***
Joined: 6 Feb, 2008
Posts: 390

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:
cpp
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 smile.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

warchilo
post 9 May, 2008 - 03:46 AM
Post #3


New D.I.C Head

*
Joined: 15 Jan, 2007
Posts: 12

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?

CODE

#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: 9 May, 2008 - 03:52 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

gabehabe
post 9 May, 2008 - 03:50 AM
Post #4


D.I.C Regular

***
Joined: 6 Feb, 2008
Posts: 390

huh? Do you still have a question?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

warchilo
post 9 May, 2008 - 04:02 AM
Post #5


New D.I.C Head

*
Joined: 15 Jan, 2007
Posts: 12

Yeah sorry to be a pain mate, I just edited my post with the question I have.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

gabehabe
post 9 May, 2008 - 04:13 AM
Post #6


D.I.C Regular

***
Joined: 6 Feb, 2008
Posts: 390

Sorry, my bad sad.gif there was a typo in the if statement, it should be == not =
Here it is, corrected:
cpp
#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 smile.gif

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 smile.gif

This post has been edited by gabehabe: 9 May, 2008 - 04:20 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

warchilo
post 9 May, 2008 - 04:21 AM
Post #7


New D.I.C Head

*
Joined: 15 Jan, 2007
Posts: 12

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.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

gabehabe
post 9 May, 2008 - 04:27 AM
Post #8


D.I.C Regular

***
Joined: 6 Feb, 2008
Posts: 390

Glad it works. Good luck with your exam smile.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
Time is now: 5/17/08 02:50AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month