   Problem with linked list
  I am trying to program a Workout log, compiling it step by stet to seeif there is any problem. As always, there is one. When I build it, it givesme no errors, and I am able to compile it. But during runtime it stops executingand the system crashes I am not sure why. However I did debug it found the problem lies with the vector string I created. The program crashes when it reaches the for loop, more specifically when I call for the vector.size();here is the relavent code,although ask if you need to see more : void workOutRoutine::AddNode(){system("cls"); //Could not resistunsigned int i(0),j(0);//Used to add new nodeworkOutRoutine *Temp = new workOutRoutine;//Used to determine the end of the list to add new nodeworkOutRoutine *EndList; //Dummy variable //getline reads \nstring dummy;getline(cin,dummy);//Get Day Of workout;cout<<"Enter the Day : ";getline(cin,Temp->Day);cout<<endl;//Get body thats being worked outcout<<"Which Body Part is Worked out on "<<Temp->Day<<"\t";getline(cin,Temp->bodyPart);cout<<endl;//Add the type of workout//on the unique body part//and the number of sets//each with certain weightfor(i =0; i <= Exercise.size(); i++) //<--- PROBLEM LIES WITHIN "Exercise.size(). While degugging it crashes my program.{if(i == Exercise.size()-1){cout<<"\nWould You like to add more sets to your "<<Temp->bodyPart<<" workout (y/n) ";char opt;cin >> opt;//NOT SURE IF THIS PART WORKS THE WAY I WANT IT TO,//BUT I'LL DEAL WITH THAT AFTER THIS BUG IS FIXEDif(opt == 'y' || opt == 'Y') {cout<<"\nAdding another Workout Element...";Exercise.resize(Exercise.size()+1,vector<string>(Exercise.size()+1));cout<<"\nDone adding another Element"<<endl;}else break;}cout<<"What type of "<<Temp->bodyPart<<" workout did you do : ";getline(cin,Exercise[0]); // [0] is the Identifier or type of workoutcout<<"\n"<<endl;for(j = 1; i< Exercise.size(); j++){cout<<"Enter the weight lifted for set # "<<++j<<"  :  ";getline(cin,Exercise[j]);}}//Get Additional Infocout<<"If you did any extra 'workout' the enter keywords with time like basketball(30.00 minutes),running(10.00 minutes)\n";cout<<"else enter NONE :  ";getline(cin,Extra);cout<<"\nWhat do you estimate the total time you took to workout :  ";getline(cin,TotalTime);//Check to see if any node has been alocatedif(strt == NULL){strt = Temp;strt->NextNode = NULL;return;}else{//set endlist pointer to start of the listEndList = strt;//Move to the end of the listwhile(EndList->NextNode != NULL)EndList = EndList->NextNode;EndList = Temp; //Add new nodeEndList->NextNode = NULL;}}Regards,Tnutty   
  Are you sure that the pointer you're calling AddNode() on is valid?   ;  Yes, I think so, here is the full code#include<iostream>#include<string>#include<vector>#include<windows.h>using namespace std;//Min number of Exercie that I will do per Workoutconst unsigned int MIN_EXERCISE = 3;//The number of sets/exercise done //for a particular workoutconst unsigned int MIN_SETS = 4;//Class hold every info about the type of workout, which days it on// which body part, how many sets, how much weight lifted per set, and //the time it toolclass workOutRoutine{public: //The day of workoutstring Day;//The body part thats being worked outstring bodyPart;//The type of exercise begin used//And how many times being done//Exercise[x][0] is Exercise identifier//Exercise[x][y] is the weight liftedvector<vector<string> > Exercise;//Any extra exercise donestring Extra;//The time spent during a particular day of working outstring TotalTime;workOutRoutine *NextNode;public:workOutRoutine();void AddNode();void PrintInfo();bool DeleteFirstNode();bool DeleteLastNode();bool DeleteCurrentNode();void MoveRight();void ModeLeft();bool SaveToFile();} *strt,*curr;//The constructorworkOutRoutine::workOutRoutine(){//set strt so that its emptystrt = NULL;curr = strt;//Resize vector to the minimun number of exercise that //I will do and to the minimum number of setsExercise.resize(MIN_EXERCISE,vector<string>(MIN_SETS,"NONE"));}void workOutRoutine::AddNode(){system("cls");unsigned int i(0),j(0);//Used to add new nodeworkOutRoutine *Temp = new workOutRoutine;//Used to determine the end of the list to add new nodeworkOutRoutine *EndList; //Dummy variable //getline reads \nstring dummy;getline(cin,dummy);//Get Day Of workout;cout<<"Enter the Day : ";getline(cin,Temp->Day);cout<<endl;//Get body thats being worked outcout<<"Which Body Part is Worked out on "<<Temp->Day<<"\t";getline(cin,Temp->bodyPart);cout<<endl;//Add the type of workout//on the unique body part//and the number of sets//each with certain weightfor(i =0; i <=Exercise.size(); i++){if(i == Exercise.size()-1){cout<<"\nWould You like to add more sets to your "<<Temp->bodyPart<<" workout (y/n) ";char opt;cin >> opt;if(opt == 'y' || opt == 'Y'){cout<<"\nAdding another Workout Element...";Exercise.resize(Exercise.size()+1,vector<string>(Exercise.size()+1));cout<<"\nDone adding another Element"<<endl;}else break;}cout<<"What type of "<<Temp->bodyPart<<" workout did you do : ";getline(cin,Exercise[0]); // [0] is the Identifier or type of workoutcout<<"\n"<<endl;for(j = 1; i< Exercise.size(); j++){cout<<"Enter the weight lifted for set # "<<++j<<"  :  ";getline(cin,Exercise[j]);}}//Get Additional Infocout<<"If you did any extra 'workout' the enter keywords with time like basketball(30.00 minutes),running(10.00 minutes)\n";cout<<"else enter NONE :  ";getline(cin,Extra);cout<<"\nWhat do you estimate the total time you took to workout :  ";getline(cin,TotalTime);//Check to see if any node has been alocatedif(strt == NULL){strt = Temp;strt->NextNode = NULL;return;}else{//set endlist pointer to start of the listEndList = strt;//Move to the end of the listwhile(EndList->NextNode != NULL)EndList = EndList->NextNode;EndList = Temp; //Add new nodeEndList->NextNode = NULL;}}void workOutRoutine::PrintInfo(){workOutRoutine *Temp = strt;//Check if any node existif(Temp == NULL){cout<<"No routine at this point\n\n";return;}//Print all Infowhile(Temp != NULL){cout<<"Day of Week "<<Temp->Day<<endl;cout<<"Body Part worked out : "<<Temp->bodyPart<<endl;for(unsigned int i = 0; i<Exercise.size(); i++){//Exercise[0] represents Workout Type/IDcout<<"Type of Workout #"<<i<<"\t\t"<<Temp->Exercise[0]<<endl;for(unsigned int j = 1; j< Exercise.size(); j++){//Exercise weight liftedcout<<"\t  Set #"<<i<<" : "<<Exercise[j]<<endl;}}cout<<endl<<endl;}}int Options(int opt){cout<<"1 -- Add Day to workout"<<endl;cout<<"2 -- Move Node forward"<<endl;cout<<"3 -- Move Node Backward"<<endl;cout<<"4 -- Delete current Node"<<endl;cout<<"5 -- Delete First Node"<<endl;cout<<"6 -- Delete Last Node"<<endl;cout<<"7 -- Save Info to File"<<endl;cout<<"8 -- Load Info to File"<<endl<<endl;cin >> opt;return opt;}int main(){int opt = -1;//Call the constructor workOutRoutine();do{system("cls");strt->PrintInfo();cout<<"\n\n"<<endl;//Get uses inputopt = Options(opt);switch(opt){case 1:strt->AddNode();break;case 2:break;case 3:break;case 4:break;case 5:break;case 6:break;case 7:break;case 8:break;}}while(opt != 0);};  Where do you initialize strt?   ;  the CtorworkOutRoutine::workOutRoutine(){//set strt so that its emptystrt = NULL;curr = strt;//Resize vector to the minimun number of exercise that //I will do and to the minimum number of setsExercise.resize(MIN_EXERCISE,vector<string>(MIN_SETS,"NONE"));};  You may not realize this, but NULL is not a valid pointer value to call a member function on.   ;  I tried this before with another linked list program and it gave no problem.If set it to null in main, it gives the same problem.Here is the error: Unhandled exception at 0x008132e9 in ExerciseRoutine.exe: 0xC0000005: Access violation reading location 0x0000004c.;  The size function is  1..n   not 0..n   ( use  less-than  not less-than-equal)for(i =0; i <= Exercise.size(); i++)it tries to iterate one step further than the actual size ... that at least for a start.Its one of the most common errors  -- an 'off by one' error   ;Quote:Original post by wodinoneeyeThe size function is  1..n   not 0..n   ( use  less-than  not less-than-equal)for(i =0; i <= Exercise.size(); i++)it tries to iterate one step further than the actual size ... that at least for a start.Its one of the most common errors  -- an 'off by one' errorI do this to make push_back func workwhen (i) reaches vec.Size(), since it starts from 0, its 1 size passed it, so Icall push_back method if I want to add more sets. if(i == Exercise.size()){cout<<"\nWould You like to add more sets to your "<<Temp->bodyPart<<" workout (y/n) ";char opt;cin >> opt;if(opt == 'y' || opt == 'Y'){cout<<"\nAdding another Workout Element...";Exercise.resize(Exercise.size()+1,vector<string>(Exercise.size()+1));cout<<"\nDone adding another Element"<<endl;};Quote:Original post by tnuttyIf set it to null in main, it gives the same problem.Quote:Original post by SiCraneYou may not realize this, but NULL is not a valid pointer value to call a member function on.