Unhandled Exception. Undefined Pointer?

J

Jordan Tiona

Alright, so here is some code. This is for a school project, and my teacher
is unreachable, so I'm posting this here. There are a few unfinished
functions, but I'll get to those soon. I compiled and ran this code, and I'm
getting an exception error on the "GetNext" function. I'm assuming it is
because I am forgetting to define a pointer before using it, but I can't
figure out where.

W7Graded.cpp

/*------------------------------------------------------/*
/* Workshop 7 -- Graded Project /*
/* Jordan Tiona /*
/* Last Updated April 17, 2006 /*
/*------------------------------------------------------*/

//Included Files

#include <iostream>
#include <stdio.h>
#include "W7Graded.h"
using namespace std;

//Function Prototypes

void PrintData();
void EnterData();
void SaveData();
void LoadData();

//Global Variables

cdData* cd; //Linked List

int main(){
int choice;
bool quit = false;
while(quit == false){
system("cls");
cout << "Welcome to CD Tracker \n\n";

cout << "What would you like to do?\n";
cout << "\t(1)Print Current Data\n";
cout << "\t(2)Enter New Data\n";
cout << "\t(3)Save Current Data\n";
cout << "\t(4)Load Data\n";
cout << "\t(5)Exit\n";
cin >> choice;

switch(choice){
case 1: PrintData();
break;
case 2: EnterData();
break;
case 3: SaveData();
break;
case 4: LoadData();
break;
case 5: quit = true;
break;
default: cout << "\n Invalid choice. Please try again\n";
}
}
return 1;
}

void PrintData(){
}

void EnterData(){

bool stop = false;
char* tempStr;
int tempInt;
cdData* curNode = cd;
char yn;

//Get to the end of the linked list first
while(curNode->GetNext() == NULL){ //If there isn't anything after this
node, then this is the end of the list
curNode = curNode->GetNext(); //Go to the next Node
}

while(stop == false){
system("cls");
//Add a new node
curNode->SetNext(new cdData());
curNode = curNode->GetNext();

cout <<"\nName of CD: ";
cin.getline(tempStr, MAX_LENGTH);
curNode->SetName(tempStr);

cout <<"\n\nName of Artist: ";
cin.getline(tempStr, MAX_LENGTH);
curNode->SetArtist(tempStr);

cout <<"\n\nYear Released: ";
cin >> tempInt;
curNode->SetYear(tempInt);

system("cls");
cout <<"Enter another CD? (Y/N)\n";
cin >> yn;

if(yn == 'Y' || 'y')
stop = false;
else
stop = true;
}

}

void SaveData(){
}

void LoadData(){
}

And now, W7Graded.h

//CD Data
const int MAX_LENGTH = 21;

class cdData {
private:
//CD Data
char* cdName;
char* artist;
int year;
//Linked List Data
cdData* next;
public:
//Constructor/Destructor
cdData(){next = NULL;}
~cdData(){}
//Accessors
char* GetName(){return cdName;}
char* GetArtist(){return artist;}
int GetYear(){return year;}

void SetName(char* newName){cdName = newName;}
void SetArtist(char* newName){artist = newName;}
void SetYear(int newYear){year = newYear;}

cdData* GetNext(){return next;}
void SetNext(cdData* newNext){next = newNext;}
};

Thanks in advance for your help. I really need to get this done.
 
G

Gianni Mariani

Jordan said:
Alright, so here is some code. This is for a school project, and my teacher
is unreachable, so I'm posting this here. There are a few unfinished
functions, but I'll get to those soon. I compiled and ran this code, and I'm
getting an exception error on the "GetNext" function. I'm assuming it is
because I am forgetting to define a pointer before using it, but I can't
figure out where.

W7Graded.cpp
....
cdData* cd; //Linked List ....
cdData* curNode = cd;
char yn;

//Get to the end of the linked list first
while(curNode->GetNext() == NULL){ //If there isn't anything after this

cd is the head of your linked list and is default initialized to 0
(NULL). The first thing you do is call cd->GetNext() which dereferences
a null pointer and a good computer will stop you right there.
 
J

Jordan Tiona

Gianni Mariani said:
cd is the head of your linked list and is default initialized to 0 (NULL).
The first thing you do is call cd->GetNext() which dereferences a null
pointer and a good computer will stop you right there.

So you're saying that cd is a null pointer. Ok, so I put a "new cdData();"
statement in main, but it still isn't working. Sorry, I guess I'm more of a
noob to pointers than I though ;-).
 
M

Mark P

Jordan said:
Alright, so here is some code. This is for a school project, and my teacher
is unreachable, so I'm posting this here. There are a few unfinished
functions, but I'll get to those soon. I compiled and ran this code, and I'm
getting an exception error on the "GetNext" function. I'm assuming it is
because I am forgetting to define a pointer before using it, but I can't
figure out where.

W7Graded.cpp

/*------------------------------------------------------/*
/* Workshop 7 -- Graded Project /*
/* Jordan Tiona /*
/* Last Updated April 17, 2006 /*
/*------------------------------------------------------*/

//Included Files

#include <iostream>
#include <stdio.h>
#include "W7Graded.h"
using namespace std;

//Function Prototypes

void PrintData();
void EnterData();
void SaveData();
void LoadData();

//Global Variables

cdData* cd; //Linked List

int main(){
int choice;
bool quit = false;
while(quit == false){
system("cls");
cout << "Welcome to CD Tracker \n\n";

cout << "What would you like to do?\n";
cout << "\t(1)Print Current Data\n";
cout << "\t(2)Enter New Data\n";
cout << "\t(3)Save Current Data\n";
cout << "\t(4)Load Data\n";
cout << "\t(5)Exit\n";
cin >> choice;

switch(choice){
case 1: PrintData();
break;
case 2: EnterData();
break;
case 3: SaveData();
break;
case 4: LoadData();
break;
case 5: quit = true;
break;
default: cout << "\n Invalid choice. Please try again\n";
}
}
return 1;
}

void PrintData(){
}

void EnterData(){

bool stop = false;
char* tempStr;
int tempInt;
cdData* curNode = cd;
char yn;

//Get to the end of the linked list first
while(curNode->GetNext() == NULL){ //If there isn't anything after this
node, then this is the end of the list
curNode = curNode->GetNext(); //Go to the next Node
}

I think you probably mean to make your while condition:
while (curNode->getNext() != NULL)
or, more concisely,
while (curNode->getNext())

-Mark
 
J

Jordan Tiona

Thanks Mark. However, I am now making my print function, and it appears I
have done it again. But where?

void PrintData(){

cdData* curNode = cd;
char* tempStr;
int tempInt;

system("cls");
do{
tempStr = curNode->GetName();
cout << "CD Name: " << tempStr << endl;
cout << "-------------------------------------\n";
tempStr = curNode->GetArtist();
cout << "Artist: " << tempStr << endl;
tempInt = curNode->GetYear();
cout << "Year Released: " << tempInt << endl;

curNode = curNode->GetNext();
}while(curNode != NULL);

}
 
J

Jordan Tiona

BTW, I think it is when I'm trying to print tempStr. Can't you print a char
pointer?
 
J

Jordan Tiona

--
"No eye has seen, no ear has heard, no mind can
conceive what God has prepared for those who love him"
1 Cor 2:9
Jordan Tiona said:
Alright, so here is some code. This is for a school project, and my
teacher is unreachable, so I'm posting this here. There are a few
unfinished functions, but I'll get to those soon. I compiled and ran this
code, and I'm getting an exception error on the "GetNext" function. I'm
assuming it is because I am forgetting to define a pointer before using
it, but I can't figure out where.

W7Graded.cpp

/*------------------------------------------------------/*
/* Workshop 7 -- Graded Project /*
/* Jordan Tiona /*
/* Last Updated April 17, 2006 /*
/*------------------------------------------------------*/

//Included Files

#include <iostream>
#include <stdio.h>
#include "W7Graded.h"
using namespace std;

//Function Prototypes

void PrintData();
void EnterData();
void SaveData();
void LoadData();

//Global Variables

cdData* cd; //Linked List

int main(){
int choice;
bool quit = false;
while(quit == false){
system("cls");
cout << "Welcome to CD Tracker \n\n";

cout << "What would you like to do?\n";
cout << "\t(1)Print Current Data\n";
cout << "\t(2)Enter New Data\n";
cout << "\t(3)Save Current Data\n";
cout << "\t(4)Load Data\n";
cout << "\t(5)Exit\n";
cin >> choice;

switch(choice){
case 1: PrintData();
break;
case 2: EnterData();
break;
case 3: SaveData();
break;
case 4: LoadData();
break;
case 5: quit = true;
break;
default: cout << "\n Invalid choice. Please try again\n";
}
}
return 1;
}

void PrintData(){
}

void EnterData(){

bool stop = false;
char* tempStr;
int tempInt;
cdData* curNode = cd;
char yn;

//Get to the end of the linked list first
while(curNode->GetNext() == NULL){ //If there isn't anything after this
node, then this is the end of the list
curNode = curNode->GetNext(); //Go to the next Node
}

while(stop == false){
system("cls");
//Add a new node
curNode->SetNext(new cdData());
curNode = curNode->GetNext();

cout <<"\nName of CD: ";
cin.getline(tempStr, MAX_LENGTH);
curNode->SetName(tempStr);

cout <<"\n\nName of Artist: ";
cin.getline(tempStr, MAX_LENGTH);
curNode->SetArtist(tempStr);

cout <<"\n\nYear Released: ";
cin >> tempInt;
curNode->SetYear(tempInt);

system("cls");
cout <<"Enter another CD? (Y/N)\n";
cin >> yn;

if(yn == 'Y' || 'y')
stop = false;
else
stop = true;
}

}

void SaveData(){
}

void LoadData(){
}

And now, W7Graded.h

//CD Data
const int MAX_LENGTH = 21;

class cdData {
private:
//CD Data
char* cdName;
char* artist;
int year;
//Linked List Data
cdData* next;
public:
//Constructor/Destructor
cdData(){next = NULL;}
~cdData(){}
//Accessors
char* GetName(){return cdName;}
char* GetArtist(){return artist;}
int GetYear(){return year;}

void SetName(char* newName){cdName = newName;}
void SetArtist(char* newName){artist = newName;}
void SetYear(int newYear){year = newYear;}

cdData* GetNext(){return next;}
void SetNext(cdData* newNext){next = newNext;}
};

Thanks in advance for your help. I really need to get this done.

New problem. I can only store on letter in the cdName and artist variables.
What do I need to do to make it a string?
 
D

Default User

Jordan said:
BTW, I think it is when I'm trying to print tempStr. Can't you print
a char pointer?

Please don't top-post. See the newsgroup FAQ on that topic. This is
especially problematic with your broken newsreader, which places your
..sig on top of the quotes, thereby ramming all that extra stuff into
your .sig. Look into OE QuoteFix.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top