473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

char* string?

How can I make one of these? I'm trying to get my program to store a string
into a variable, but it only stores one line.

--
"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
Apr 18 '06 #1
33 3620
Jordan Tiona wrote:
How can I make one of these? I'm trying to get my program to store a string
into a variable, but it only stores one line.


Please elaborate (preferably with source code; cf.
http://parashift.com/c++-faq-lite/ho....html#faq-5.8). You should
prefer std::string to char* strings anyway.

Cheers! --M

Apr 18 '06 #2
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Jordan Tiona wrote:
How can I make one of these? I'm trying to get my program to store a
string
into a variable, but it only stores one line.


Please elaborate (preferably with source code; cf.
http://parashift.com/c++-faq-lite/ho....html#faq-5.8). You should
prefer std::string to char* strings anyway.

Cheers! --M


Sorry. I've decided to go with std::string. However, how do I print one of
these? If I have a string called "tempStr", and have this statement: "cout
<< tempStr", I get an error.
Apr 18 '06 #3
Jordan Tiona wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Jordan Tiona wrote:
How can I make one of these? I'm trying to get my program to store a
string
into a variable, but it only stores one line.


Please elaborate (preferably with source code; cf.
http://parashift.com/c++-faq-lite/ho....html#faq-5.8). You should
prefer std::string to char* strings anyway.

Cheers! --M


Sorry. I've decided to go with std::string. However, how do I print one of
these? If I have a string called "tempStr", and have this statement: "cout
<< tempStr", I get an error.


Do you really think that's an adequate description of your problem? Show us
a minimal, but complete program that has the observed error, as well as the
exact error message you got from the compiler. I'm not really in the mood
of solving riddles today.

Apr 18 '06 #4
Jordan Tiona wrote:
Sorry. I've decided to go with std::string. However, how do I print one of
these? If I have a string called "tempStr", and have this statement: "cout
<< tempStr", I get an error.


You shouldn't (unless your statement is also missing the semicolon).
Show us the offending code (cf. the FAQ mentioned above).

Cheers! --M

Apr 18 '06 #5

"Jordan Tiona" <to*******@comcast.net> wrote in message
news:Fe********************@comcast.com...
| "mlimber" <ml*****@gmail.com> wrote in message
| news:11**********************@e56g2000cwe.googlegr oups.com...
| > Jordan Tiona wrote:
| >> How can I make one of these? I'm trying to get my program to store a
| >> string
| >> into a variable, but it only stores one line.
| >
| > Please elaborate (preferably with source code; cf.
| > http://parashift.com/c++-faq-lite/ho....html#faq-5.8). You should
| > prefer std::string to char* strings anyway.
| >
| > Cheers! --M
| >
|
| Sorry. I've decided to go with std::string. However, how do I print one of
| these? If I have a string called "tempStr", and have this statement: "cout
| << tempStr", I get an error.
I have tried "cout << tempStr" and it works for me. Anyway you can try
"cout << tempStr.c_str()" intead.
Apr 18 '06 #6
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
Jordan Tiona wrote:
Sorry. I've decided to go with std::string. However, how do I print one
of
these? If I have a string called "tempStr", and have this statement:
"cout
<< tempStr", I get an error.


You shouldn't (unless your statement is also missing the semicolon).
Show us the offending code (cf. the FAQ mentioned above).

Cheers! --M


I'm sorry again. I didn't think that code was truly required to answer this
question. Here is my code, without strings. It is kind of long, I apologize.
(BTW, this IS homework, but you helping me with problems like this shouldn't
be a problem right?)

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(){
cd = new cdData();
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(){

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

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

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

void EnterData(){

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

tempStr = new char();

//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");
cin.ignore();
//Add a new node
curNode->SetNext(new cdData());
curNode = curNode->GetNext();

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

tempStr = new char();

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' || yn == 'y')
stop = false;
else
stop = true;
}

}

void SaveData(){
}

void LoadData(){
}

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;
artist = NULL;
cdName = 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;}
};

I can't put more than one character in the "cdName" variable. How do I get
more than one character (or more than one word for that matter)?
Apr 18 '06 #7

Jordan Tiona wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
Jordan Tiona wrote:
Sorry. I've decided to go with std::string. However, how do I print one
of
these? If I have a string called "tempStr", and have this statement:
"cout
<< tempStr", I get an error.


You shouldn't (unless your statement is also missing the semicolon).
Show us the offending code (cf. the FAQ mentioned above).

Cheers! --M


I'm sorry again. I didn't think that code was truly required to answer this
question. Here is my code, without strings. It is kind of long, I apologize.
(BTW, this IS homework, but you helping me with problems like this shouldn't
be a problem right?)


Please review
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Your code is neither minimal nor compilable. Compilable means that I
can copy and paste from your message directly into my editor and
compile it. If your question is about compile errors, I will see those
errors. If your question is about behaviour of the program, I will be
able to build the program and run it to see the behaviour. With the
code you posted, I can do neither. I just get an error about not being
able to find "W7Graded.h". Not surprising since I don't have it on my
computer.

But before you go ahead and follow the advice in point 4 in that FAQ,
think about minimal. Minimal means the *smallest possible* program you
can create that exhibits your problem. Take what you've got at the
moment and start stripping bits out, one at a time. Each time you
remove something, recompile and see if you get the same error. When the
error goes away, whatever you removed last was causing the problem.
Often, during this process, you will find the problem yourself anyway.

And finally, if you don't manage to fix it yourself by stripping the
code down, please make it clear what your question is. See point 7 in
that FAQ. In a previous message you said you had switched from char* to
std::string, but were having trouble printing one with cout. When asked
for code, you posted a far from minimal example without a single
std::string in it. Instead, you asked how to fit more than one
character into a char*. The answer to that is that you should prefer
std::string. But we've already been there. Can you see how it's quite
difficult to help you?

Gavin Deane

Apr 18 '06 #8
Jordan Tiona wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
Jordan Tiona wrote:
Sorry. I've decided to go with std::string. However, how do I print one
of
these? If I have a string called "tempStr", and have this statement:
"cout
<< tempStr", I get an error. You shouldn't (unless your statement is also missing the semicolon).
Show us the offending code (cf. the FAQ mentioned above).

Cheers! --M


I'm sorry again. I didn't think that code was truly required to answer this
question. Here is my code, without strings. It is kind of long, I apologize.
(BTW, this IS homework, but you helping me with problems like this shouldn't
be a problem right?)


Specific C++ questions generally aren't an issue. It's when people come
in an expect us to basically write their entire homework assignment,
then we get ornery.

Having said that, you just said that you've "decided to get with
std::string", but I don't see a std::string _anywhere_ in this code...
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(){
cd = new cdData();
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;
Warning... see what happens if a user doesn't enter a numeric value....
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(){

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

system("cls");
do{
tempStr = curNode->GetName();
if(tempStr != NULL)
cout << "CD Name: " << *tempStr << endl;
*tempStr is a character. You probably don't want to dereference tempStr
here. (at least it appears that you're using it in the context of a
C-style string...)
cout << "-------------------------------------\n";
tempStr = curNode->GetArtist();
if(tempStr != NULL)
cout << "Artist: " << *tempStr << endl;
Same as above.
tempInt = curNode->GetYear();
cout << "Year Released: " << tempInt << endl;

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

void EnterData(){

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

tempStr = new char();
tempStr is now pointing at enough memory to hold 1 char.... (Looking a
little further forward, are you sure you don't want:

tempStr = new char[MAX_LENGTH + 1];

? That is a "string" long enough to hold MAX_LENGTH, plus 1 character
for the terminating 0-char (that is the char with the value 0, not the
'0' character).
//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");
cin.ignore();
//Add a new node
curNode->SetNext(new cdData());
curNode = curNode->GetNext();

cout <<"\nName of CD: ";
cin.getline(tempStr, MAX_LENGTH);
Under your original code, this results in a "buffer overflow", and is
likely to crash your program (but not guaranteed... this is what's
called Undefined Behaviour).
curNode->SetName(tempStr);

tempStr = new char();
See above.
cout <<"\n\nName of Artist: ";
cin.getline(tempStr, MAX_LENGTH);
See above.
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' || yn == 'y')
stop = false;
else
stop = true;
}

}

void SaveData(){
}

void LoadData(){
}

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;
artist = NULL;
cdName = NULL;}
Prefer initialization lists to initialization within the body. That is,
write this instead:

cdData() : next(NULL), artist(NULL), cdName(NULL) {};

The advantage here (which doesn't mean much for a simple pointer, but
more of an issue with more complex classes) is that in your form, the
variables next, artist and cdName need to the default-initialized first,
then assigned a value. With an initialization list, the variables would
be constructed with the right values to begin with.
~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;}
};

I can't put more than one character in the "cdName" variable. How do I get
more than one character (or more than one word for that matter)?


Other items to note:

1) Your program leaks memory like a sieve. I see many instances of "new
char" (which probably should be "new char[MAX_LENGTH + 1]"), but I
don't ever see a call to "delete" (or delete[] if you fix the allocations).
2) This gets a whole lot easier if you change to std::strings. All of
the dynamic memory stuff related to "strings" goes away.
3) If you're trying to read an entire line into a std::string, look up
std::getline() (and not the getline() that is a member of a stream).
4) I'm assuming it's part of the assignment that you can't use the rest
of the standard C++ library (like std::list, or some other container)?
Apr 18 '06 #9
"Andre Kostur" <nn******@kostur.net> wrote in message
news:NV******************@news20.bellglobal.com...
Jordan Tiona wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
Jordan Tiona wrote:
Sorry. I've decided to go with std::string. However, how do I print one
of
these? If I have a string called "tempStr", and have this statement:
"cout
<< tempStr", I get an error.
You shouldn't (unless your statement is also missing the semicolon).
Show us the offending code (cf. the FAQ mentioned above).

Cheers! --M


I'm sorry again. I didn't think that code was truly required to answer
this question. Here is my code, without strings. It is kind of long, I
apologize. (BTW, this IS homework, but you helping me with problems like
this shouldn't be a problem right?)


Specific C++ questions generally aren't an issue. It's when people come
in an expect us to basically write their entire homework assignment, then
we get ornery.

Having said that, you just said that you've "decided to get with
std::string", but I don't see a std::string _anywhere_ in this code...
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(){
cd = new cdData();
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;


Warning... see what happens if a user doesn't enter a numeric value....
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(){

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

system("cls");
do{
tempStr = curNode->GetName();
if(tempStr != NULL)
cout << "CD Name: " << *tempStr << endl;


*tempStr is a character. You probably don't want to dereference tempStr
here. (at least it appears that you're using it in the context of a
C-style string...)
cout << "-------------------------------------\n";
tempStr = curNode->GetArtist();
if(tempStr != NULL)
cout << "Artist: " << *tempStr << endl;


Same as above.
tempInt = curNode->GetYear();
cout << "Year Released: " << tempInt << endl;

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

void EnterData(){

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

tempStr = new char();


tempStr is now pointing at enough memory to hold 1 char.... (Looking a
little further forward, are you sure you don't want:

tempStr = new char[MAX_LENGTH + 1];

? That is a "string" long enough to hold MAX_LENGTH, plus 1 character for
the terminating 0-char (that is the char with the value 0, not the '0'
character).
//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");
cin.ignore();
//Add a new node
curNode->SetNext(new cdData());
curNode = curNode->GetNext();

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


Under your original code, this results in a "buffer overflow", and is
likely to crash your program (but not guaranteed... this is what's called
Undefined Behaviour).
curNode->SetName(tempStr);

tempStr = new char();


See above.
cout <<"\n\nName of Artist: ";
cin.getline(tempStr, MAX_LENGTH);


See above.
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' || yn == 'y')
stop = false;
else
stop = true;
}

}

void SaveData(){
}

void LoadData(){
}

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;
artist = NULL;
cdName = NULL;}


Prefer initialization lists to initialization within the body. That is,
write this instead:

cdData() : next(NULL), artist(NULL), cdName(NULL) {};

The advantage here (which doesn't mean much for a simple pointer, but more
of an issue with more complex classes) is that in your form, the variables
next, artist and cdName need to the default-initialized first, then
assigned a value. With an initialization list, the variables would be
constructed with the right values to begin with.
~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;}
};

I can't put more than one character in the "cdName" variable. How do I
get more than one character (or more than one word for that matter)?


Other items to note:

1) Your program leaks memory like a sieve. I see many instances of "new
char" (which probably should be "new char[MAX_LENGTH + 1]"), but I don't
ever see a call to "delete" (or delete[] if you fix the allocations).
2) This gets a whole lot easier if you change to std::strings. All of the
dynamic memory stuff related to "strings" goes away.
3) If you're trying to read an entire line into a std::string, look up
std::getline() (and not the getline() that is a member of a stream).
4) I'm assuming it's part of the assignment that you can't use the rest of
the standard C++ library (like std::list, or some other container)?


Ok, well. I tried switching to strings, but I was getting a TON of errors
that made no sense. Another thing about using strings, is that they're not
mentioned once in my entire course (this is my last assignment). Char arrays
are all that are used. But it appeared that using a pointer was the only way
to use getline. Idk, I just never got it I guess. I think Ill try rewriting
the entire program (didn't take me /that/ long).
Apr 18 '06 #10
Jordan Tiona wrote:
1) Your program leaks memory like a sieve. I see many instances of "new
char" (which probably should be "new char[MAX_LENGTH + 1]"), but I don't
ever see a call to "delete" (or delete[] if you fix the allocations).
2) This gets a whole lot easier if you change to std::strings. All of
the dynamic memory stuff related to "strings" goes away.
3) If you're trying to read an entire line into a std::string, look up
std::getline() (and not the getline() that is a member of a stream).
4) I'm assuming it's part of the assignment that you can't use the rest
of the standard C++ library (like std::list, or some other container)?
Ok, well. I tried switching to strings, but I was getting a TON of errors
that made no sense.


I doubt that (not that you got a ton of errors, but that they made no
sense). I rather assume that you just didn't understand them.
Another thing about using strings, is that they're not mentioned once in
my entire course (this is my last assignment).


Then that course is useless. std::string is a standard C++ class, and it
makes string handling a _lot_ simpler and safer. It should be explained
before raw arrays of char and manual memory handling.
Apr 18 '06 #11
Jordan Tiona wrote:
Ok, well. I tried switching to strings, but I was getting a TON of errors
that made no sense. Another thing about using strings, is that they're not
mentioned once in my entire course (this is my last assignment). Char arrays
are all that are used. But it appeared that using a pointer was the only way
to use getline. Idk, I just never got it I guess. I think Ill try rewriting
the entire program (didn't take me /that/ long).


The fact that you never used std::strings implies that you probably
took a C-style course, even if you used a few features of C++. For a
good introduction to C++ from the ground up as C++ rather than C, see
Koenig and Moo's _Accelerated C++_. For an example of how to use
std::string and std::getline(), see this post:

http://groups.google.com/group/comp....9afd6f122340c8

Cheers! --M

Apr 18 '06 #12
Ok, so I started to rewrite the program with a better linked list class, and
with strings. But it doesn't seem to recognize strings. I don't get why I'm
getting the errors I'm getting. Could you compile this and see what is
wrong? (BTW, I don't know if it is ok for me to give you two different
files, but I have two files here.)

W7Graded.cpp

#include <iostream>
#include <string>
#include "W7Graded.h"

using namespace std;

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

int main(){
LinkedList *cdList = NULL;
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 EnterData(){
}

void PrintData(){
}

void SaveData(){
}

void LoadData(){
}

LinkedList::LinkedList(){
hn = new cdData();
cn = hn;
}

LinkedList::~LinkedList(){
this->RemoveAll();
}

void LinkedList::Insert(cdData* newCD){
cdData* temp = hn;

//Run to end of list
while(temp->GetNext() != NULL)
temp = temp->GetNext();

temp->SetNext(newCD);
}

void LinkedList::RemoveAll(){
cdData* temp = hn;
this->Reset();

while(temp->GetNext() != NULL){
cn = temp;
temp = temp->GetNext();
delete cn;
}
delete temp;
hn->SetNext(NULL);
}

-------------------------------------------------------
W7Graded.h

class cdData{
private:
string name;
string artist;
string year;

cdData* next;
public:
cdData(){ next = NULL;}
~cdData(){}

string GetName(){ return name;}
string GetArtist(){ return artist;}
string GetYear(){ return year;}
cdData* GetNext(){ return next;}

void SetName(string newName){ name = newName;}
void SetArtist(string newName){ artist = newName;}
void SetYear(string newYear){ year = newYear;}
void SetNext(cdData* newNext){ next = newNext;}
};

class LinkedList{
private:
cdData* hn; //Head Node
cdData* cn; //Current Node
public:
LinkedList();
~LinkedList();

void Insert(cdData* newCD);
void RemoveAll();
void Reset(){ cn = hn;}
void GetNext(){ cn = cn->GetNext();
return cn;}
};
--------------------------------------------------

I'm trying not to break any rules, but if I am, than sorry in advance.

Apr 18 '06 #13

Jordan Tiona wrote:
Ok, so I started to rewrite the program with a better linked list class, and
with strings. But it doesn't seem to recognize strings. I don't get why I'm
getting the errors I'm getting. Could you compile this and see what is
wrong? (BTW, I don't know if it is ok for me to give you two different
files, but I have two files here.) [snip] W7Graded.h

class cdData{
private:
string name;
string artist;
string year;

cdData* next;
public:
cdData(){ next = NULL;}
~cdData(){}

string GetName(){ return name;}
string GetArtist(){ return artist;}
string GetYear(){ return year;}
cdData* GetNext(){ return next;}

void SetName(string newName){ name = newName;}
void SetArtist(string newName){ artist = newName;}
void SetYear(string newYear){ year = newYear;}
void SetNext(cdData* newNext){ next = newNext;}
};

class LinkedList{
private:
cdData* hn; //Head Node
cdData* cn; //Current Node
public:
LinkedList();
~LinkedList();

void Insert(cdData* newCD);
void RemoveAll();
void Reset(){ cn = hn;}
void GetNext(){ cn = cn->GetNext();
return cn;}
};


The problem is that string isn't visible in your header file. If you
append "std::" before each instance of "string", that should get rid of
the errors. Also, it's generally best to include all headers that any
particular file needs directly. In this case, that would mean including
<string> in your .h file instead of the .cpp file. That way, you don't
have to include <string> manually in each file where you use that
header. Finally, "using namespace std;" is fine for .cpp files, but
don't be tempted to use it in your header. That will pollute the global
namespace for anyone who includes your file, which defeats the purpose
of namespaces.

Cheers! --M

Apr 18 '06 #14
class cdData{
private:
string name;
string artist;
string year;

class cdData {
private:
typedef std::string string;

string name;
string artist;
string year;
-Tomás
Apr 18 '06 #15
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Jordan Tiona wrote:
Ok, well. I tried switching to strings, but I was getting a TON of errors
that made no sense. Another thing about using strings, is that they're
not
mentioned once in my entire course (this is my last assignment). Char
arrays
are all that are used. But it appeared that using a pointer was the only
way
to use getline. Idk, I just never got it I guess. I think Ill try
rewriting
the entire program (didn't take me /that/ long).


The fact that you never used std::strings implies that you probably
took a C-style course, even if you used a few features of C++. For a
good introduction to C++ from the ground up as C++ rather than C, see
Koenig and Moo's _Accelerated C++_. For an example of how to use
std::string and std::getline(), see this post:

http://groups.google.com/group/comp....9afd6f122340c8

Cheers! --M


Thanks again limber. I'm going to get yelled at for posting so much, but
here goes ;-).

I have now made my "EnterData" function. But it appears that in the Insert
function (in the LinkedList class), I can't check to see if the head node is
created yet. I'm getting an error on this line:

if(hn == NULL)
hn = newCD;

Now when the LinkedList is made, hn is set to NULL. If the list is new, I
wanted to go ahead and make hn the new node, rather than make a new one. So
why can't I check to see if hn is NULL? Let me know if you want me to post
the entire code. Here is EnterData, and Insert:

void EnterData(){
bool stop = false;
string tempStr;
char yn;
cdData* temp;

if(cdList = NULL)
cdList = new LinkedList();

while(stop == false){
temp = new cdData();

cin.ignore();

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

cout <<"\nArtist: ";
getline(cin, tempStr);
temp->SetArtist(tempStr);

cout <<"\nYear Released: ";
getline(cin, tempStr);
temp->SetYear(tempStr);

cdList->Insert(temp);

system("cls");
cout << "Would you like to add another CD? (Y/N) ";
cin >> yn;

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

void LinkedList::Insert(cdData* newCD){
cdData* temp;

if(hn == NULL)
hn = newCD;
else {

//Run to end of list
while(temp->GetNext() != NULL)
temp = temp->GetNext();

temp->SetNext(newCD);
}
}
Apr 18 '06 #16
Jordan Tiona wrote:
"mlimber" <ml*****@gmail.com> wrote in message

Thanks again limber. I'm going to get yelled at for posting so much, but
here goes ;-).
No... but we will "yell" at you for not telling us what the error
message was....
I have now made my "EnterData" function. But it appears that in the Insert
function (in the LinkedList class), I can't check to see if the head node is
created yet. I'm getting an error on this line:

if(hn == NULL)
hn = newCD;

Now when the LinkedList is made, hn is set to NULL. If the list is new, I
wanted to go ahead and make hn the new node, rather than make a new one. So
why can't I check to see if hn is NULL? Let me know if you want me to post
the entire code. Here is EnterData, and Insert:

void EnterData(){
bool stop = false;
string tempStr;
char yn;
cdData* temp;

if(cdList = NULL)
cdList = new LinkedList();

while(stop == false){
temp = new cdData();

cin.ignore();

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

cout <<"\nArtist: ";
getline(cin, tempStr);
temp->SetArtist(tempStr);

cout <<"\nYear Released: ";
getline(cin, tempStr);
temp->SetYear(tempStr);

cdList->Insert(temp);

system("cls");
cout << "Would you like to add another CD? (Y/N) ";
cin >> yn;

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

void LinkedList::Insert(cdData* newCD){
cdData* temp;
temp is only used in the else clause below... you should move its
declaration there too.

if(hn == NULL)
hn = newCD;
else {

//Run to end of list
while(temp->GetNext() != NULL)
Umm... temp hasn't been assigned anything yet.... thus you are
attempting to dereference an uninitialized pointer. Undefined Behaviour.
temp = temp->GetNext();

temp->SetNext(newCD);
}
}

Apr 18 '06 #17
Jordan Tiona wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Jordan Tiona wrote:
Ok, well. I tried switching to strings, but I was getting a TON of errors
that made no sense. Another thing about using strings, is that they're
not
mentioned once in my entire course (this is my last assignment). Char
arrays
are all that are used. But it appeared that using a pointer was the only
way
to use getline. Idk, I just never got it I guess. I think Ill try
rewriting
the entire program (didn't take me /that/ long). The fact that you never used std::strings implies that you probably
took a C-style course, even if you used a few features of C++. For a
good introduction to C++ from the ground up as C++ rather than C, see
Koenig and Moo's _Accelerated C++_. For an example of how to use
std::string and std::getline(), see this post:

http://groups.google.com/group/comp....9afd6f122340c8

Cheers! --M


Thanks again limber. I'm going to get yelled at for posting so much, but
here goes ;-).

I have now made my "EnterData" function. But it appears that in the Insert
function (in the LinkedList class), I can't check to see if the head node is
created yet. I'm getting an error on this line:

if(hn == NULL)
hn = newCD;

Now when the LinkedList is made, hn is set to NULL. If the list is new, I
wanted to go ahead and make hn the new node, rather than make a new one. So
why can't I check to see if hn is NULL? Let me know if you want me to post
the entire code. Here is EnterData, and Insert:

void EnterData(){
bool stop = false;
string tempStr;
char yn;
cdData* temp;

if(cdList = NULL)

The above line is an error. It assigns NULL to cdList.
It should be:

if (NULL == cdList)

Notice how the 'constant' is on the left side of the '=='.
That approach helps find such errors at compile time.
The line:

if (NULL = cdList)

Would have produced a compile time error.

cdList = new LinkedList();

while(stop == false){
temp = new cdData();

cin.ignore();

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

cout <<"\nArtist: ";
getline(cin, tempStr);
temp->SetArtist(tempStr);

cout <<"\nYear Released: ";
getline(cin, tempStr);
temp->SetYear(tempStr);

cdList->Insert(temp);

system("cls");
cout << "Would you like to add another CD? (Y/N) ";
cin >> yn;

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

void LinkedList::Insert(cdData* newCD){
cdData* temp;

if(hn == NULL)
hn = newCD;
else {

//Run to end of list
while(temp->GetNext() != NULL)
temp = temp->GetNext();

temp->SetNext(newCD);
}
}

Apr 18 '06 #18
Oh, doi. Thanks larry!

Now I've tried to make my PrintData function. Now I'm getting another Access
Violation. But, the wierd thing is that it is in a file that I don't have in
my program. It is the header file <xstring>. Here is my print function,
followed by the function from xstring that is causing the problem.

void PrintData(){
cdData* temp;

cdList->Reset();
temp = cdList->GetNext();

system("cls");
while(temp == NULL){
cout << "CD Name: " << temp->GetName() << endl;
cout << "------------------------------\n";
cout << "Artist: " << temp->GetArtist() << endl;
cout << "Year Released: " << temp->GetYear() << endl;

temp = temp->GetNext();
}

}

size_type size() const
{ // return length of sequence
return (_Mysize);
}
-------------------------------------------

As a side note, since I got "yelled" at for not showing the error, here it
is.

Unhandled exception at 0x00420936 in W7Graded.exe: 0xC0000005: Access
violation reading location 0x00000014.
Apr 19 '06 #19
Jordan Tiona wrote:
Oh, doi. Thanks larry!

Now I've tried to make my PrintData function. Now I'm getting another Access
Violation. But, the wierd thing is that it is in a file that I don't have in
my program. It is the header file <xstring>. Here is my print function,
followed by the function from xstring that is causing the problem.

void PrintData(){
cdData* temp;

cdList->Reset();
temp = cdList->GetNext();

system("cls");
while(temp == NULL){
Hmm, maybe you meant:

while (NULL != temp) {
or even:

while (temp) {

If temp IS NULL, the following code will get an Access Violation.
cout << "CD Name: " << temp->GetName() << endl;
cout << "------------------------------\n";
cout << "Artist: " << temp->GetArtist() << endl;
cout << "Year Released: " << temp->GetYear() << endl;

temp = temp->GetNext();
}

}

size_type size() const
{ // return length of sequence
return (_Mysize);
}
-------------------------------------------

As a side note, since I got "yelled" at for not showing the error, here it
is.

Unhandled exception at 0x00420936 in W7Graded.exe: 0xC0000005: Access
violation reading location 0x00000014.

Apr 19 '06 #20
Thanks for all of the help guys. But I'm not quite done yet. I am new to
fstream, and am using it to write my files. I've gotten the SaveData
function done, and it does write the correct data to the file. For instance,
here is a sample output from the program...

This is a Test CDBy a test Artist2000This is another cdBy another artist2000

It's bunched up, but it is working. My only problem, is how do I load this
again? I thought I had saved it in binary, so would I be able to just take
each string out one at a time? (i.e. "This is a Test CD" and "By a test
Artist" would be put in seperate strings).

Also, how do I check for EOF? I've looked into the eof() function, but I'm
not sure how to use it properly. Here is my SaveData function:

void SaveData(){
string tempStr;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to save as: ";
cin >> fileName;

ofstream file(fileName.c_str(), ios::binary);
if(!file)
cout << "Could not save file";
else{

cout << "\nSaving . . .";

cdList->Reset();
temp = cdList->GetNext();
while(temp != NULL){
tempStr = temp->GetName();
file << tempStr;
tempStr = temp->GetArtist();
file << tempStr;
tempStr = temp->GetYear();
file << tempStr;

temp = temp->GetNext();
}
}
}
---------------------------------------

Thanks again for any help.
Apr 19 '06 #21

"Jordan Tiona" <to*******@comcast.net> wrote in message
news:D9********************@comcast.com...
. . .


Never mind. I found a good tutorial on using it. However, I'm having a bit
of trouble with my loading function. (BTW, I fixed a couple of errors in my
SaveData function). Here is my load function, along with my save function.

void SaveData(){
string tempStr;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to save as: ";
cin >> fileName;

ofstream file(fileName.c_str(), ios::binary);
if(!file)
cout << "Could not save file";
else{

cout << "\nSaving . . .";

cdList->Reset();
temp = cdList->GetNext();
while(temp != NULL){
tempStr = temp->GetName();
file.write((char*)&tempStr, sizeof(tempStr));
tempStr = temp->GetArtist();
file.write((char*)&tempStr, sizeof(tempStr));
tempStr = temp->GetYear();
file.write((char*)&tempStr, sizeof(tempStr));

temp = temp->GetNext();
}
}
file.close();
}

void LoadData(){
string tempStr;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to load from: ";
cin >> fileName;

ifstream file(fileName.c_str(), ios::binary);
if(!file)
cout << "Could not load file";
else{

cout << "\nLoading . . .";

if(cdList == NULL)
cdList = new LinkedList();

cdList->RemoveAll();
cdList->Reset();

while(!file.eof()){
temp = new cdData();

file.read((char*)&tempStr, sizeof(tempStr));
temp->SetName(tempStr);

file.read((char*)&tempStr, sizeof(tempStr));
temp->SetArtist(tempStr);

file.read((char*)&tempStr, sizeof(tempStr));
temp->SetYear(tempStr);

cdList->Insert(temp);
}
}
file.close();
}

LinkedList::LinkedList(){
hn = new cdData(); //Blank node for head
cn = hn;
}

I used my debugger, and found that it is loading correctly (all of the
variables are what they should be), but I'm getting that same Unhandled
exception/Access violation I got earlier in xstring. This time it is in this
function:

void _Tidy(bool _Built = false,
size_type _Newsize = 0)
{ // initialize buffer, deallocating any storage
if (!_Built)
;
else if (_BUF_SIZE <= _Myres)
{ // copy any leftovers to small buffer and deallocate
_Elem *_Ptr = _Bx._Ptr;
if (0 < _Newsize)
_Traits::copy(_Bx._Buf, _Ptr, _Newsize);
_Mybase::_Alval.deallocate(_Ptr, _Myres + 1);
}
_Myres = _BUF_SIZE - 1;
_Eos(_Newsize);
}

Here is the error I'm getting:

Unhandled exception at 0x7c901230 in W7Graded.exe: User breakpoint.

It says User Breakpoint, but I don't have any breakpoints there anymore. So
there you go. I don't know what to make of this.
Apr 19 '06 #22
Jordan Tiona wrote:
"Jordan Tiona" <to*******@comcast.net> wrote in message
news:D9********************@comcast.com...
. . .
Never mind. I found a good tutorial on using it. However, I'm having a bit
of trouble with my loading function. (BTW, I fixed a couple of errors in my
SaveData function). Here is my load function, along with my save function.

void SaveData(){
string tempStr;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to save as: ";
cin >> fileName;

ofstream file(fileName.c_str(), ios::binary);
if(!file)
cout << "Could not save file";
else{

cout << "\nSaving . . .";

cdList->Reset();
temp = cdList->GetNext();
while(temp != NULL){
tempStr = temp->GetName();
file.write((char*)&tempStr, sizeof(tempStr));


I'm surprised that this writes anything useful. Actually, maybe not
(possibly a small string optimization). Try your program with much
larger strings. And the first hint that you're doing something really
bad.... you're forcing a cast. You cannot write out the binary
representation of a std::string and expect anything intelligent to come
back. The internal representation is not for you to play with. You
probably want something closer to:

file.write(tempStr.c_str(), tempStr.size() + 1);
tempStr = temp->GetArtist();
file.write((char*)&tempStr, sizeof(tempStr));
tempStr = temp->GetYear();
file.write((char*)&tempStr, sizeof(tempStr));

temp = temp->GetNext();
}
}
file.close();
}

void LoadData(){
string tempStr;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to load from: ";
cin >> fileName;

ifstream file(fileName.c_str(), ios::binary);
if(!file)
cout << "Could not load file";
else{

cout << "\nLoading . . .";

if(cdList == NULL)
cdList = new LinkedList();

cdList->RemoveAll();
cdList->Reset();

while(!file.eof()){
temp = new cdData();

file.read((char*)&tempStr, sizeof(tempStr));
Equally bad as above (actually worse... above you're only writing out
some internal representation, here you're overwriting some internal
representation). You're reading who-knows-what onto a std::string,
blowing away whatever internal representation the std::string happens to
be using. Again, the hint of a cast being required is a really large
red flag.
temp->SetName(tempStr);

file.read((char*)&tempStr, sizeof(tempStr));
temp->SetArtist(tempStr);

file.read((char*)&tempStr, sizeof(tempStr));
temp->SetYear(tempStr);

cdList->Insert(temp);
}
}
file.close();
}

LinkedList::LinkedList(){
hn = new cdData(); //Blank node for head
cn = hn;
}

I used my debugger, and found that it is loading correctly (all of the
variables are what they should be), but I'm getting that same Unhandled
exception/Access violation I got earlier in xstring. This time it is in this
function:

void _Tidy(bool _Built = false,
size_type _Newsize = 0)
{ // initialize buffer, deallocating any storage
if (!_Built)
;
else if (_BUF_SIZE <= _Myres)
{ // copy any leftovers to small buffer and deallocate
_Elem *_Ptr = _Bx._Ptr;
if (0 < _Newsize)
_Traits::copy(_Bx._Buf, _Ptr, _Newsize);
_Mybase::_Alval.deallocate(_Ptr, _Myres + 1);
}
_Myres = _BUF_SIZE - 1;
_Eos(_Newsize);
}

Here is the error I'm getting:

Unhandled exception at 0x7c901230 in W7Graded.exe: User breakpoint.

It says User Breakpoint, but I don't have any breakpoints there anymore. So
there you go. I don't know what to make of this.


You've FUBARed your strings. You've invoked Undefined Behaviour, which
happens to be manifesting as a User Breakpoint.

Even better question, why bother with a "binary" representation. Why
not simply stream out your data records as a series of text lines?
Apr 19 '06 #23
"Andre Kostur" <nn******@kostur.net> wrote in message
news:l4*******************@news20.bellglobal.com.. .
Jordan Tiona wrote:
"Jordan Tiona" <to*******@comcast.net> wrote in message
news:D9********************@comcast.com...
. . .


Never mind. I found a good tutorial on using it. However, I'm having a
bit of trouble with my loading function. (BTW, I fixed a couple of errors
in my SaveData function). Here is my load function, along with my save
function.

void SaveData(){
string tempStr;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to save as: ";
cin >> fileName;

ofstream file(fileName.c_str(), ios::binary);
if(!file)
cout << "Could not save file";
else{

cout << "\nSaving . . .";

cdList->Reset();
temp = cdList->GetNext();
while(temp != NULL){
tempStr = temp->GetName();
file.write((char*)&tempStr, sizeof(tempStr));


I'm surprised that this writes anything useful. Actually, maybe not
(possibly a small string optimization). Try your program with much larger
strings. And the first hint that you're doing something really bad....
you're forcing a cast. You cannot write out the binary representation of
a std::string and expect anything intelligent to come back. The internal
representation is not for you to play with. You probably want something
closer to:

file.write(tempStr.c_str(), tempStr.size() + 1);
tempStr = temp->GetArtist();
file.write((char*)&tempStr, sizeof(tempStr));
tempStr = temp->GetYear();
file.write((char*)&tempStr, sizeof(tempStr));

temp = temp->GetNext();
}
}
file.close();
}

void LoadData(){
string tempStr;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to load from: ";
cin >> fileName;

ifstream file(fileName.c_str(), ios::binary);
if(!file)
cout << "Could not load file";
else{

cout << "\nLoading . . .";

if(cdList == NULL)
cdList = new LinkedList();

cdList->RemoveAll();
cdList->Reset();

while(!file.eof()){
temp = new cdData();

file.read((char*)&tempStr, sizeof(tempStr));


Equally bad as above (actually worse... above you're only writing out some
internal representation, here you're overwriting some internal
representation). You're reading who-knows-what onto a std::string,
blowing away whatever internal representation the std::string happens to
be using. Again, the hint of a cast being required is a really large red
flag.
temp->SetName(tempStr);

file.read((char*)&tempStr, sizeof(tempStr));
temp->SetArtist(tempStr);

file.read((char*)&tempStr, sizeof(tempStr));
temp->SetYear(tempStr);

cdList->Insert(temp);
}
}
file.close();
}

LinkedList::LinkedList(){
hn = new cdData(); //Blank node for head
cn = hn;
}

I used my debugger, and found that it is loading correctly (all of the
variables are what they should be), but I'm getting that same Unhandled
exception/Access violation I got earlier in xstring. This time it is in
this function:

void _Tidy(bool _Built = false,
size_type _Newsize = 0)
{ // initialize buffer, deallocating any storage
if (!_Built)
;
else if (_BUF_SIZE <= _Myres)
{ // copy any leftovers to small buffer and deallocate
_Elem *_Ptr = _Bx._Ptr;
if (0 < _Newsize)
_Traits::copy(_Bx._Buf, _Ptr, _Newsize);
_Mybase::_Alval.deallocate(_Ptr, _Myres + 1);
}
_Myres = _BUF_SIZE - 1;
_Eos(_Newsize);
}

Here is the error I'm getting:

Unhandled exception at 0x7c901230 in W7Graded.exe: User breakpoint.

It says User Breakpoint, but I don't have any breakpoints there anymore.
So there you go. I don't know what to make of this.


You've FUBARed your strings. You've invoked Undefined Behaviour, which
happens to be manifesting as a User Breakpoint.

Even better question, why bother with a "binary" representation. Why not
simply stream out your data records as a series of text lines?


Ok, I guess I see your point. But if I do write it out as text, how can I
load it back in? Would I be able to write each data statement on a line
(which would cause each line to end with the "\n" character) then use this:

file.getline(tempStr, 255, '\n');

Or is there another way to do this?
Apr 19 '06 #24
Jordan Tiona wrote:
Ok, I guess I see your point. But if I do write it out as text, how can I
load it back in? Would I be able to write each data statement on a line
(which would cause each line to end with the "\n" character) then use this:

file.getline(tempStr, 255, '\n');

Or is there another way to do this?


Please see the post reference earlier in this thread for an example of
how to do it:

http://groups.google.com/group/comp....7f86cae6ee9fb8

Cheers! --M

Apr 19 '06 #25
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Jordan Tiona wrote:
Ok, I guess I see your point. But if I do write it out as text, how can I
load it back in? Would I be able to write each data statement on a line
(which would cause each line to end with the "\n" character) then use
this:

file.getline(tempStr, 255, '\n');

Or is there another way to do this?


Please see the post reference earlier in this thread for an example of
how to do it:

http://groups.google.com/group/comp....7f86cae6ee9fb8

Cheers! --M


But how would I get more than one line in at a time? I'm trying to have the
CD name on one line, the Artist on the next, then the year on the third.
Apr 19 '06 #26
Jordan Tiona wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Jordan Tiona wrote:
Ok, I guess I see your point. But if I do write it out as text, how can I
load it back in? Would I be able to write each data statement on a line
(which would cause each line to end with the "\n" character) then use
this:

file.getline(tempStr, 255, '\n');

Or is there another way to do this?

Please see the post reference earlier in this thread for an example of
how to do it:

http://groups.google.com/group/comp....7f86cae6ee9fb8

Cheers! --M


But how would I get more than one line in at a time? I'm trying to have the
CD name on one line, the Artist on the next, then the year on the third.


Umm.. the question doesn't make much sense... for your data format, why
do you need to read multiple lines at a time? You read 1 line, put it
in the CD Name field. Read the next line, put it in Artist. Read the
third line, put it in the Year. Is there something I'm missing in the
question?
Apr 19 '06 #27

Jordan Tiona wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Jordan Tiona wrote:
Ok, I guess I see your point. But if I do write it out as text, how can I
load it back in? Would I be able to write each data statement on a line
(which would cause each line to end with the "\n" character) then use
this:

file.getline(tempStr, 255, '\n');

Or is there another way to do this?


Please see the post reference earlier in this thread for an example of
how to do it:

http://groups.google.com/group/comp....7f86cae6ee9fb8

Cheers! --M


But how would I get more than one line in at a time? I'm trying to have the
CD name on one line, the Artist on the next, then the year on the third.


Use multiple getlines:

string name, artist, year;
while( getline(file, name) && getline(file, artist) && getline(file,
year) )
{
// Do something with name, artist and year
}

You might also be interested to know that there is a newsgroup
specifically for helping people learn C and C++
(alt.comp.lang.learn.c-c++).

Cheers! --M

Cheers! --M

Apr 19 '06 #28
"mlimber" <ml*****@gmail.com> wrote in message
news:11*********************@t31g2000cwb.googlegro ups.com...

Jordan Tiona wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
> Jordan Tiona wrote:
>
>> Ok, I guess I see your point. But if I do write it out as text, how
>> can I
>> load it back in? Would I be able to write each data statement on a
>> line
>> (which would cause each line to end with the "\n" character) then use
>> this:
>>
>> file.getline(tempStr, 255, '\n');
>>
>> Or is there another way to do this?
>
> Please see the post reference earlier in this thread for an example of
> how to do it:
>
> http://groups.google.com/group/comp....7f86cae6ee9fb8
>
> Cheers! --M
>


But how would I get more than one line in at a time? I'm trying to have
the
CD name on one line, the Artist on the next, then the year on the third.


Use multiple getlines:

string name, artist, year;
while( getline(file, name) && getline(file, artist) && getline(file,
year) )
{
// Do something with name, artist and year
}

You might also be interested to know that there is a newsgroup
specifically for helping people learn C and C++
(alt.comp.lang.learn.c-c++).

Cheers! --M

Cheers! --M


Sigh, almost done ;-). So I made my load function. But I'm still getting
that same access violation with the User Breakpoint. Here is my function:

void LoadData(){
string name, artist, year;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to load from: ";
cin >> fileName;

ifstream file(fileName.c_str());
if(!file)
cout << "Could not load file";
else{

cout << "\nLoading . . .";

if(cdList == NULL)
cdList = new LinkedList();

cdList->RemoveAll();
cdList->Reset();

while(getline(file, name) && getline(file, artist) && getline(file, year)){
temp = new cdData();

temp->SetName(name);
temp->SetArtist(artist);
temp->SetYear(year);

cdList->Insert(temp);
}
}
file.close();
system("pause");
}
-------------------------------------------

So what could be the problem this time?
Apr 19 '06 #29
Jordan Tiona wrote:
Sigh, almost done ;-). So I made my load function. But I'm still getting
that same access violation with the User Breakpoint. Here is my function:

void LoadData(){
string name, artist, year;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to load from: ";
cin >> fileName;

ifstream file(fileName.c_str());
if(!file)
cout << "Could not load file";
else{

cout << "\nLoading . . .";

if(cdList == NULL)
cdList = new LinkedList();

cdList->RemoveAll();
cdList->Reset();

while(getline(file, name) && getline(file, artist) && getline(file, year)){
temp = new cdData();

temp->SetName(name);
temp->SetArtist(artist);
temp->SetYear(year);

cdList->Insert(temp);
}
}
file.close();
system("pause");
}
-------------------------------------------

So what could be the problem this time?


Nothing in this piece of code. Which probably makes it somewhere in
either LinkedList or cdData.
Apr 19 '06 #30
"Andre Kostur" <nn******@kostur.net> wrote in message
news:OH*******************@news20.bellglobal.com.. .
Jordan Tiona wrote:
Sigh, almost done ;-). So I made my load function. But I'm still getting
that same access violation with the User Breakpoint. Here is my function:

void LoadData(){
string name, artist, year;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to load from: ";
cin >> fileName;

ifstream file(fileName.c_str());
if(!file)
cout << "Could not load file";
else{

cout << "\nLoading . . .";

if(cdList == NULL)
cdList = new LinkedList();

cdList->RemoveAll();
cdList->Reset();

while(getline(file, name) && getline(file, artist) && getline(file,
year)){
temp = new cdData();

temp->SetName(name);
temp->SetArtist(artist);
temp->SetYear(year);

cdList->Insert(temp);
}
}
file.close();
system("pause");
}
-------------------------------------------

So what could be the problem this time?


Nothing in this piece of code. Which probably makes it somewhere in
either LinkedList or cdData.


Well, it has to be happening on this line:

while(getline(file, newName) && getline(file, newArtist) && getline(file,
newYear)){

And the error is pointing to a line in "write.c". I haven't any idea what's
causing the problem.
Apr 19 '06 #31
Jordan Tiona wrote:
"Andre Kostur" <nn******@kostur.net> wrote in message
news:OH*******************@news20.bellglobal.com.. .
Jordan Tiona wrote:
Sigh, almost done ;-). So I made my load function. But I'm still getting
that same access violation with the User Breakpoint. Here is my function:

void LoadData(){
string name, artist, year;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to load from: ";
cin >> fileName;

ifstream file(fileName.c_str());
if(!file)
cout << "Could not load file";
else{

cout << "\nLoading . . .";

if(cdList == NULL)
cdList = new LinkedList();

cdList->RemoveAll();
cdList->Reset();

while(getline(file, name) && getline(file, artist) && getline(file,
year)){
temp = new cdData();

temp->SetName(name);
temp->SetArtist(artist);
temp->SetYear(year);

cdList->Insert(temp);
}
}
file.close();
system("pause");
}
-------------------------------------------

So what could be the problem this time?

Nothing in this piece of code. Which probably makes it somewhere in
either LinkedList or cdData.


Well, it has to be happening on this line:

while(getline(file, newName) && getline(file, newArtist) && getline(file,
newYear)){

And the error is pointing to a line in "write.c". I haven't any idea what's
causing the problem.


Which somewhat points to you scrambling the contents of one of your
strings. Although with Undefined Behaviour, anything can happen. I'd
still suspect something in cdData.
Apr 19 '06 #32
"Andre Kostur" <nn******@kostur.net> wrote in message
news:Vh*******************@news20.bellglobal.com.. .
Jordan Tiona wrote:
"Andre Kostur" <nn******@kostur.net> wrote in message
news:OH*******************@news20.bellglobal.com.. .
Jordan Tiona wrote:

Sigh, almost done ;-). So I made my load function. But I'm still
getting that same access violation with the User Breakpoint. Here is my
function:

void LoadData(){
string name, artist, year;
string fileName;
cdData* temp;

cout << "Please write the filename you wish to load from: ";
cin >> fileName;

ifstream file(fileName.c_str());
if(!file)
cout << "Could not load file";
else{

cout << "\nLoading . . .";

if(cdList == NULL)
cdList = new LinkedList();

cdList->RemoveAll();
cdList->Reset();

while(getline(file, name) && getline(file, artist) && getline(file,
year)){
temp = new cdData();

temp->SetName(name);
temp->SetArtist(artist);
temp->SetYear(year);

cdList->Insert(temp);
}
}
file.close();
system("pause");
}
-------------------------------------------

So what could be the problem this time?
Nothing in this piece of code. Which probably makes it somewhere in
either LinkedList or cdData.


Well, it has to be happening on this line:

while(getline(file, newName) && getline(file, newArtist) && getline(file,
newYear)){

And the error is pointing to a line in "write.c". I haven't any idea
what's causing the problem.


Which somewhat points to you scrambling the contents of one of your
strings. Although with Undefined Behaviour, anything can happen. I'd
still suspect something in cdData.


It can't be anything that has to do with my linked list, because I took out
everything inside the loop and it still gave the same error.
Apr 19 '06 #33

Jordan Tiona wrote:
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
Jordan Tiona wrote:
Sorry. I've decided to go with std::string. However, how do I print one
of
these? If I have a string called "tempStr", and have this statement:
"cout
<< tempStr", I get an error.


You shouldn't (unless your statement is also missing the semicolon).
Show us the offending code (cf. the FAQ mentioned above).

Cheers! --M


I'm sorry again. I didn't think that code was truly required to answer this
question. Here is my code, without strings. It is kind of long, I apologize.
(BTW, this IS homework, but you helping me with problems like this shouldn't
be a problem right?)

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(){
cd = new cdData();
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(){

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

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

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

void EnterData(){

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

tempStr = new char();

//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");
cin.ignore();
//Add a new node
curNode->SetNext(new cdData());
curNode = curNode->GetNext();

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

tempStr = new char();

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' || yn == 'y')
stop = false;
else
stop = true;
}

}

void SaveData(){
}

void LoadData(){
}

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;
artist = NULL;
cdName = 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;}
};

I can't put more than one character in the "cdName" variable. How do I get
more than one character (or more than one word for that matter)?


This code looks so familiar to me...Must be the same assignment that
was given out..

Apr 20 '06 #34

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Yang Song | last post by:
HI, I am a little confused about char * and char. How would I be able to return a char* created in a function? Is new() the only way? How would I be able to return a point and a value at the same...
24
by: Norm | last post by:
Could someone explain for me what is happening here? char *string; string = new char; string = "This is the string"; cout << string << std::endl; The output contains (This the string) even...
9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
4
by: Radde | last post by:
Hi, class String { public: String(char* str); void operator char*();
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.