By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

Unhandled Exception. Undefined Pointer?

P: n/a
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.
Apr 17 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Jordan Tiona wrote:
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.
Apr 17 '06 #2

P: n/a
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:Ld******************************@speakeasy.ne t...
Jordan Tiona wrote:
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.


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 ;-).
Apr 17 '06 #3

P: n/a
Jordan Tiona wrote:
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

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.

Apr 17 '06 #4

P: n/a
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);

}

--
"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
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fm> wrote in message
news:P%*******************@newssvr21.news.prodigy. com...
Jordan Tiona wrote:
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

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.


Apr 17 '06 #5

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

--
"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
"Mark P" <us****@fall2005REMOVE.fastmailCAPS.fm> wrote in message
news:P%*******************@newssvr21.news.prodigy. com...
Jordan Tiona wrote:
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

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.


Apr 17 '06 #6

P: n/a


--
"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" <to*******@comcast.net> wrote in message
news:OJ********************@comcast.com...
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?
Apr 17 '06 #7

P: n/a
Jordan Tiona wrote:
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
Apr 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.