Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 6th, 2005, 11:28 PM   #1
davidguygc
Programmer
 
Join Date: Mar 2005
Location: Lubbock, TX
Posts: 30
Rep Power: 0 davidguygc is on a distinguished road
Code is changing a variable that isn't involved

Hey, I'm stuck on this code for a project of mine. I need to create a PGM struct that takes in a filename, reads the type, # of columns, # of rows, and max val, followed by the bulk of the data. It takes in the filename and opens it, and does fine until the loop that reads in the bulk of the data. It changes the pgm.filename variable for no reason I can see:
PGM open(bool &file_opened)
{
	PGM file;
	int x;
	
	cout << "File name? ";
	cin >>  file.filename;
	string filename = file.filename;
	if(file.filename == "") throw 2;

	ifstream ifs(filename.c_str());
	ifs >> file.type >> file.col >> file.row >> file.max;
	if(!ifs) throw 1;
	else
	{
		//getting the int's
		for(x = 0; x < (file.col * file.row); x++)
		{
			cout << x +1 << ". " << file.filename << endl;
                        system("pause");
			ifs >> file.bulk[x];
		}
I have it cout the filename every cycle so I can see when it starts to change the name, and after about 10 iterations, it starts to warp the filename.
Here is the code for the struct:
struct PGM
{
	int max, row, col;
	string filename, type;
	int* bulk;
	string* readable;
	PGM();
	PGM(string filename, int type, int maxval, int row, int col);
};
the code was changing the filename before I added in the cout. It crashes right after that when it tries to read the file, but can't find the file.
Any ideas?
Thanks,
---David
davidguygc is offline   Reply With Quote
Old Dec 7th, 2005, 2:55 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
You haven't provided enough information, but I'm guessing that you haven't initialised the pointer file.bulk to point at anything useful. A consequence of that is that the line (in the loop)
    ifs >> file.bulk[x];
will tromp on random memory.

After the line;
   ifs >> file.type >> file.col >> file.row >> file.max;
you need to add a line something like;
   file.bulk = new int[file.col*file.row];
That will stop the immediate crash, but won't prevent other problems. For example, you will also need to hand-roll a copy constructor (and operator=()) for the PBM type as well.
grumpy is offline   Reply With Quote
Old Dec 7th, 2005, 8:12 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
This is just another way of saying something Grumpy said, to emphasize the point. Those two thangys down ther are like your address book. There you may put directions on where to find someone, but you can't actually make those someones live in your address book. You need to go build a couple houses and put the ADDRESSES there.
	int* bulk;
	string* readable;
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 7th, 2005, 9:05 PM   #4
davidguygc
Programmer
 
Join Date: Mar 2005
Location: Lubbock, TX
Posts: 30
Rep Power: 0 davidguygc is on a distinguished road
Thanks,

I'm fairly sure that is what is wrong with the code, this project is about creating dynamic memory and pointers. My only problem now is that when I try to compile it, two functions I have in a header that I created, say that there are multiple definitions of it. I have the header (console.h) nested in another header (include.h), that is then in my main header, (header.h). In each header I put in
#ifndef filename_h_
#define filename_h_

blah blah

#endif

The really big thing that's making me scratch my head is the fact that it was compiling just fine without those errors appearing, then one time i compiled the project it gave me that error, and has been ever since. I am using Dev-C++, and I tried putting all the files in a new project, but same problem. Any suggestions?
davidguygc is offline   Reply With Quote
Old Dec 7th, 2005, 9:45 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
two functions I have in a header
This has been covered about a half-dozen times in the last two weeks. Don't put code in a header file. The reasons are plentiful, and so are the recent threads explaining them. I recommend a search and some research.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC