![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Mar 2005
Location: Lubbock, TX
Posts: 30
Rep Power: 0
![]() |
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];
}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);
};Any ideas? Thanks, ---David |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
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]; After the line; ifs >> file.type >> file.col >> file.row >> file.max; file.bulk = new int[file.col*file.row]; |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2005
Location: Lubbock, TX
Posts: 30
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#5 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|