File Problem

This is a discussion on File Problem within the C++ Programming forums, part of the General Programming Boards category; I have this program... Code: #include <iostream> #include <fstream> using namespace std; int main (void) { float temps_degc[2][6]; int i ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool File Problem

    I have this program...

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main (void)
    {
    	float temps_degc[2][6];
    	int i = 0;
    	int j = 0;
    
    	// Initialise multid array
    	for(i = 0; i < 3; i++)
    		for(j = 0; j < 7; j++)
    			temps_degc[i][j] = 0.0;
    
    	ifstream filestream;	// Create a file called "filestream" which I will be able to access through my program
    	filestream.open("temps.dat");	//\\mwsug05\\u05\\EE0U61A9\\Documents\\Visual Studio 2008\\Projects\\temps.dat
    
    	if(!filestream)
    		cout << "Cannot open file!" << endl;
    
    	for(i = 0; i < 3; i++)
    		for(j = 0; j < 7; j++)
    		{
    				filestream >> temps_degc[i][j];
    				cout << temps_degc[i][j] << endl;
    		}
    
    	filestream.close();
    	return 0;
    }
    No errors, no warnings!

    But when I run it I get a run time error right before the program finishes. It displays all the value from the file to the screen and then an error appears.

    Can anyone help???

    thanks

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What error?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Run time check failure #2 - Stack around the variable 'temps_degc' was corrupted

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    float temps_degc[2][6];
    int i = 0;
    int j = 0;
    
    // Initialise multid array
    for(i = 0; i < 3; i++)
        for(j = 0; j < 7; j++)
            temps_degc[i][j] = 0.0;
    Your valid array dimensions are 0-1 and 0-5 but you are looping from 0-2 and 0-6.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    float temps_degc[2][6];
    int i = 0;
    int j = 0;
    
    // Initialise multid array
    for(i = 0; i < 3; i++)
        for(j = 0; j < 7; j++)
            temps_degc[i][j] = 0.0;
    Your valid array dimensions are 0-1 and 0-5 but you are looping from 0-2 and 0-6.
    Thanks friend for your reply

    I thought my array dimensions were 0-2 and 0-6? Why is this not so?

    sorry if this seems obvious to you

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    When you define an array, that is the # of elements in the array.
    When you access array elements, you start with zero.

    So, myarray[6] is 6 elements, numbered 0-5.

    Different critters.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by strokebow View Post
    Thanks friend for your reply

    I thought my array dimensions were 0-2 and 0-6? Why is this not so?

    sorry if this seems obvious to you
    When you specify the DIMENSION, you tell HOW MANY elements (n) you want. The indices to those values are 0..n-1, because for example 0, 1, 2 are THREE values, and we ALWAYS (in C and C++) start indices at 0.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Sweet

    So simple but yet so beautiful

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21