c++ small linked list game keeps crashing -



c++ small linked list game keeps crashing -

i have started learning linked lists , trying create little game few nodes. have created 7 nodes , trying create little game can go node node until user escapes castle..the start of game works , sec level 1 time 3rd level no matter take null.this first time creating little programme tips fixing issue appreciated.here code far...

#include <iostream> #include <string> using namespace std; struct listnode { string text1; string text2; string text3; string text4; listnode *right; listnode *left; listnode *up; listnode *down; int choice; listnode() { text1 = "null"; text2 = "null"; text3= "null"; text4= "null"; right = null; left = null; = null; downwards = null; selection = 1; } }; int main() { char choice; listnode *start = new listnode(); listnode *two = null; 2 = new listnode(); start->right = two; two->left = start; listnode *three = null; 3 = new listnode(); two->down = three; three->up = two; listnode *four = null; 4 = new listnode(); four->right = four; three->left = three; listnode *five = null; 5 = new listnode(); five->right = five; three->left = four; listnode *six = null; 6 = new listnode(); six->up = six; five->down = five; listnode *seven = null; 7 = new listnode(); seven->up = seven; three->down = six; start->text1 = "welcome castle."; start->text2 = "you cannot leave!!"; two->text1 = "yay i'm in sec level!"; two->text2 = "oh no, i'm @ sec level again?"; three->text1= "yay, im on 3rd level"; three->text2= "oh no, i'm @ 3rd level again?"; four->text1= "yay, im on 4th level"; four->text2= "oh no, i'm @ 4th level again?"; five->text1= "yay, im on 5th level"; five->text2= "oh no, i'm @ 5th level again?"; six->text1= "yay, im on 6th level"; six->text2= "oh no, i'm @ 6th level again?"; seven->text1= "yay, im on 7th level , have escaped castle"; seven->text2= "oh no, i'm @ 7th level again?"; listnode *ptr = start; while (true) { if (ptr->choice == 1) { cout<<ptr->text1<<endl; ptr->choice = 2; } else cout<<ptr->text2<<endl; if (ptr->up != null) { cout<<"press w go up"<<endl; } if (ptr->down != null) { cout<<"press s go down"<<endl; } if (ptr->left != null) { cout<<"press go left"<<endl; } if (ptr->right != null) { cout<<"press d go right"<<endl; } cin>>choice; if (choice == 'w') ptr = ptr->up; else if (choice == 's') ptr = ptr->down; else if (choice == 'a') ptr = ptr->left; else if (choice == 'd') ptr = ptr->right; else { cout<<"wrong input.. "<<choice<<" not option"<<endl; } } homecoming 0; }

but 1 time 3rd level , go left programme displays word null , crashes on me

nowhere in code text3 or text4 ever printed. hence this...

three->text3= "yay, im on 3rd level"; three->text4= "oh no, i'm @ sec level again?";

should this...

three->text1= "yay, im on 3rd level"; three->text2= "oh no, i'm @ sec level again?";

c++ linked-list

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -