Saving into a structure problem
2 posters
RUSES :: Part 1 :: Programming :: Coursework
Page 1 of 1
Saving into a structure problem
- Code:
struct vertex
{
char data, c1Val, c2Val, pVal;
struct vertex *parent;
struct vertex *child1, *child2;
};
int main()
{
vertex *tree=NULL;
int v=10;
if(v>0)
{
struct vertex *tree=new vertex[v];
tree[0].data='s';
cout<<tree[0].data<<endl;
char test='d';
tree[1].data=test;
cout<<tree[1].data<<endl;
}
cout<<tree[0].data<<endl;
return 0;
}
Okay so the issue is I can write to a value in structure tree above the comment and it will print it out fine. However when I start getting characters from my file verfile it screws up. The variable is correctly saved from verfile into blank (for my file the value is an 's') however when I try and save that into tree[k].data it says:
"Unhandled exception at 0x00942e4c in programming coursework.exe: 0xC0000005: Access violation writing location 0x00000000."
This is puzzling me quite a bit as I can write a variable into tree[1] earlier on however writing another variable in later on doesn't work.
N.B I haven't cut out any of the code between those two bits just the stuff before setting up variables etc and the stuff afterwards.
Any help is greatly appreciated.
EDIT:it appears to be a scoping issue
Last edited by Danielmckenna on Mon Mar 05, 2012 1:44 am; edited 2 times in total (Reason for editing : Clarity of reading)
Danielmckenna- Posts : 3
Join date : 2012-03-03
Re: Saving into a structure problem
Try: if (v>1)
Since it's accessing tree[1] and if v only equals 1, then accessing tree[1] will cause an exception
EDIT: Ah, I just read the whole code, need to take a proper look (though the above still applies)
Since it's accessing tree[1] and if v only equals 1, then accessing tree[1] will cause an exception
EDIT: Ah, I just read the whole code, need to take a proper look (though the above still applies)
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Saving into a structure problem
- Code:
#include <iostream>
using namespace std;
struct vertex
{
char data, c1Val, c2Val, pVal;
vertex *parent;
vertex *child1, *child2;
};
int main()
{
vertex *tree=NULL;
int v=10;
tree=new vertex[v];
if(v>1)
{
tree[0].data='s';
cout<<tree[0].data<<endl;
char test='d';
tree[1].data=test;
cout<<tree[1].data<<endl;
}
cout<<tree[0].data<<endl;
return 0;
}
Fixed!
Also you are using a mix of C and C++, in C++ when defining a struct object instead of the C code i.e.
- Code:
struct a
{
int val;
};
struct a OBJECT;
You do:
- Code:
struct a
{
int val;
};
a OBJECT;
i.e. don't call 'struct' when creating the object
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Saving into a structure problem
That's not the problem it was a scoping problem and I've solved it, so if the mods want to delete this then they can.
Danielmckenna- Posts : 3
Join date : 2012-03-03
Similar topics
» All topics must identify which language the code problem is in
» All topics must identify which language the code problem is in
» All topics must identify which language the code problem is in
RUSES :: Part 1 :: Programming :: Coursework
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum