RUSES
Would you like to react to this message? Create an account in a few clicks or log in to continue.
April 2024
MonTueWedThuFriSatSun
1234567
891011121314
15161718192021
22232425262728
2930     

Calendar Calendar


Saving into a structure problem

2 posters

Go down

Saving into a structure problem Empty Saving into a structure problem

Post  Danielmckenna Sun Mar 04, 2012 11:07 pm

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

Back to top Go down

Saving into a structure problem Empty Re: Saving into a structure problem

Post  Alex Tue Mar 06, 2012 3:18 am

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)
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Saving into a structure problem Empty Re: Saving into a structure problem

Post  Alex Tue Mar 06, 2012 3:22 am

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
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Saving into a structure problem Empty Re: Saving into a structure problem

Post  Danielmckenna Wed Mar 07, 2012 6:20 am

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

Back to top Go down

Saving into a structure problem Empty Re: Saving into a structure problem

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum