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

please help (novice)

P: n/a
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>

class string{
public:
string(void){characters = 0; pt = NULL;}
string(char *s);
private:
int characters;
char *pt;
};

string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);
}

void main()
{
string s1("My first string");
}
If you compile it, you'll see that the compiler doesn't report any
error.. but when running it, you'll see that you suddenly get a
windows error report..

Does someone know what the source of this fault might be and what to
change about the code above?

Thanks!!
Jul 22 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
"ranger" <sg****@msn.com> wrote in message
news:ab**************************@posting.google.c om...
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>

class string{
public:
string(void){characters = 0; pt = NULL;}
string(char *s);
private:
int characters;
char *pt;
};

string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);
}

void main()
{
string s1("My first string");
}
If you compile it, you'll see that the compiler doesn't report any
error.. but when running it, you'll see that you suddenly get a
windows error report..

Does someone know what the source of this fault might be and what to
change about the code above?

Thanks!!


I'm really sorry to tell you this, but you need a newer/better book. From
the code you show, there are many things wrong. Too many to deal with here
without basically rewriting everything you have. As a beginner you shouldn't
be using most of what you are using. Basics come first, you know.
I'd advise you to forget what you think you know, get a better book (what
are you using, BTW?) and start fresh.

On the other hand, if this is some kind of troll --- Well Done! and go away.
--
Gary
Jul 22 '05 #2

P: n/a
ranger wrote:

[...]
string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){ ^^^
This is an assignment, not a comparison.
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);
}
[...]
If you compile it, you'll see that the compiler doesn't report any
error.. but when running it, you'll see that you suddenly get a
windows error report..


Increase the warning level of your compiler. Then you'll probably see a
warning about doing an assignment in a comparison context.

--
Dirk

(PGP keyID: 0x448BC5DD - http://www.gnupg.org - http://www.pgp.com)

..oO° Death is a non-maskable interrupt. °Oo.
Jul 22 '05 #3

P: n/a
* ranger:
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>
#include <iostream>

<iostream.h> is not a standard C++ header.
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>

is the preferred style for these three headers.
class string{
public:
string(void){characters = 0; pt = NULL;}
'void' as argument list a C'ism; don't use it.

Use an initializer list instead of assignment,

string(): characters(0), pt(NULL) {}

string(char *s);
Argument should be const:

string( char const* s );

You also need a destructor to deallocate the string.

And you need an assignment operator to copy the string, and
a copy constructor.

private:
int characters;
char *pt;
};

string::string(char *s){
string::string( char const* s )
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){
In standard C++ this test will always fail. Operator 'new' reports
failure by means of std::bad_alloc exception, not by returning NULL,
so the test would always fail even if it correctly reflected what
you intended to write. But it doesn't even that: it's an assignment,
not a comparision.

cerr << "Failed to allocate string.\n";
Here you need to qualify using the 'std' namespace:

std::cerr << "Failed to allocate string.\n";

But it's not a good idea to do i/o to report failure to the user;
instead use exceptions to report the failure up to the caller.

exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);
This is OK if, as seems likely, you don't intend to have a terminating
zero byte in your string representation. However, this is where you
get a crash. That's due to the earlier error noted above.
}

void main()
'main' must have return type 'int'.
{
string s1("My first string");
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4

P: n/a

The reason your code fails is the following line....
if (pt = NULL){
In C and C++ you should use "==" not "=" to compare two
items, what you have done is assign NULL to pt and loose the
value of the address of the memory you alloaced.

dave
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net... * ranger:
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>


#include <iostream>

<iostream.h> is not a standard C++ header.
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>


#include <cstdio>
#include <cstdlib>
#include <cstring>

is the preferred style for these three headers.
class string{
public:
string(void){characters = 0; pt = NULL;}


'void' as argument list a C'ism; don't use it.

Use an initializer list instead of assignment,

string(): characters(0), pt(NULL) {}

string(char *s);


Argument should be const:

string( char const* s );

You also need a destructor to deallocate the string.

And you need an assignment operator to copy the string, and
a copy constructor.

private:
int characters;
char *pt;
};

string::string(char *s){


string::string( char const* s )
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){


In standard C++ this test will always fail. Operator 'new' reports
failure by means of std::bad_alloc exception, not by returning NULL,
so the test would always fail even if it correctly reflected what
you intended to write. But it doesn't even that: it's an assignment,
not a comparision.

cerr << "Failed to allocate string.\n";


Here you need to qualify using the 'std' namespace:

std::cerr << "Failed to allocate string.\n";

But it's not a good idea to do i/o to report failure to the user;
instead use exceptions to report the failure up to the caller.

exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);


This is OK if, as seems likely, you don't intend to have a terminating
zero byte in your string representation. However, this is where you
get a crash. That's due to the earlier error noted above.
}

void main()


'main' must have return type 'int'.
{
string s1("My first string");
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Jul 22 '05 #5

P: n/a
* Dave Townsend:
[top-posting]
[quoting signature]
Do not top post (see the FAQ); corrected.

Do not quote signatures; corrected.

* Alf P. Steinbach:

if (pt = NULL){
In standard C++ this test will always fail. Operator 'new' reports
failure by means of std::bad_alloc exception, not by returning NULL,
so the test would always fail even if it correctly reflected what
you intended to write. But it doesn't even that: it's an assignment,
not a comparision.
memcpy(pt, s, characters);


This is OK if, as seems likely, you don't intend to have a terminating
zero byte in your string representation. However, this is where you
get a crash. That's due to the earlier error noted above.

* Dave Townsend: The reason your code fails is the following line....
if (pt = NULL){


In C and C++ you should use "==" not "=" to compare two
items, what you have done is assign NULL to pt and loose the
value of the address of the memory you alloaced.


Why are you adding this comment to my explanation?

Dirk Feytons and I have already pointed out what you're pointing out,
and we have done that less incorrectly (it's not "the" reason, but "a").

What you're writing is redundant to the n'th degree, and as a comment on
my reply it is strongly misleading.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #6

P: n/a
I'm using "introducing C++ for scientists, engineers and
mathematicians", from 1997. The book is meant for numerical
applications..
As an engineer in automatics I'm working on an application and I
needed to get the basics of C++ as quick as possible.. most of the
time I work on MATLAB.

Still, I regret your answer ..but I'm pleased with the help of all
those who took the time to help me out.. Thanks guys!

"Gary Labowitz" <gl*******@comcast.net> wrote in message news:<6t********************@comcast.com>...
"ranger" <sg****@msn.com> wrote in message
news:ab**************************@posting.google.c om...
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>

class string{
public:
string(void){characters = 0; pt = NULL;}
string(char *s);
private:
int characters;
char *pt;
};

string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}

memcpy(pt, s, characters);
}

void main()
{
string s1("My first string");
}
If you compile it, you'll see that the compiler doesn't report any
error.. but when running it, you'll see that you suddenly get a
windows error report..

Does someone know what the source of this fault might be and what to
change about the code above?

Thanks!!


I'm really sorry to tell you this, but you need a newer/better book. From
the code you show, there are many things wrong. Too many to deal with here
without basically rewriting everything you have. As a beginner you shouldn't
be using most of what you are using. Basics come first, you know.
I'd advise you to forget what you think you know, get a better book (what
are you using, BTW?) and start fresh.

On the other hand, if this is some kind of troll --- Well Done! and go away.

Jul 22 '05 #7

P: n/a
Thanks!

Dirk Feytons <Di**********@NORUBBISH.easynet.be> wrote in message news:<11***************@seven.kulnet.kuleuven.ac.b e>...
ranger wrote:

[...]
string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){

^^^
This is an assignment, not a comparison.
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}

memcpy(pt, s, characters);
}


[...]
If you compile it, you'll see that the compiler doesn't report any
error.. but when running it, you'll see that you suddenly get a
windows error report..


Increase the warning level of your compiler. Then you'll probably see a
warning about doing an assignment in a comparison context.

Jul 22 '05 #8

P: n/a
Thanks! ..I really appreciate your reaction!

al***@start.no (Alf P. Steinbach) wrote in message news:<41*****************@news.individual.net>...
* ranger:
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>


#include <iostream>

<iostream.h> is not a standard C++ header.
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>


#include <cstdio>
#include <cstdlib>
#include <cstring>

is the preferred style for these three headers.
class string{
public:
string(void){characters = 0; pt = NULL;}


'void' as argument list a C'ism; don't use it.

Use an initializer list instead of assignment,

string(): characters(0), pt(NULL) {}

string(char *s);


Argument should be const:

string( char const* s );

You also need a destructor to deallocate the string.

And you need an assignment operator to copy the string, and
a copy constructor.

private:
int characters;
char *pt;
};

string::string(char *s){


string::string( char const* s )
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){


In standard C++ this test will always fail. Operator 'new' reports
failure by means of std::bad_alloc exception, not by returning NULL,
so the test would always fail even if it correctly reflected what
you intended to write. But it doesn't even that: it's an assignment,
not a comparision.

cerr << "Failed to allocate string.\n";


Here you need to qualify using the 'std' namespace:

std::cerr << "Failed to allocate string.\n";

But it's not a good idea to do i/o to report failure to the user;
instead use exceptions to report the failure up to the caller.

exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);


This is OK if, as seems likely, you don't intend to have a terminating
zero byte in your string representation. However, this is where you
get a crash. That's due to the earlier error noted above.
}

void main()


'main' must have return type 'int'.
{
string s1("My first string");
}

Jul 22 '05 #9

P: n/a
Thanks! ..I really appreciate your reaction!

"Dave Townsend" <da********@comcast.net> wrote in message news:<dL********************@comcast.com>...
The reason your code fails is the following line....
if (pt = NULL){


In C and C++ you should use "==" not "=" to compare two
items, what you have done is assign NULL to pt and loose the
value of the address of the memory you alloaced.

dave
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* ranger:
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>


#include <iostream>

<iostream.h> is not a standard C++ header.
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>


#include <cstdio>
#include <cstdlib>
#include <cstring>

is the preferred style for these three headers.
class string{
public:
string(void){characters = 0; pt = NULL;}


'void' as argument list a C'ism; don't use it.

Use an initializer list instead of assignment,

string(): characters(0), pt(NULL) {}

string(char *s);


Argument should be const:

string( char const* s );

You also need a destructor to deallocate the string.

And you need an assignment operator to copy the string, and
a copy constructor.

private:
int characters;
char *pt;
};

string::string(char *s){


string::string( char const* s )
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){


In standard C++ this test will always fail. Operator 'new' reports
failure by means of std::bad_alloc exception, not by returning NULL,
so the test would always fail even if it correctly reflected what
you intended to write. But it doesn't even that: it's an assignment,
not a comparision.

cerr << "Failed to allocate string.\n";


Here you need to qualify using the 'std' namespace:

std::cerr << "Failed to allocate string.\n";

But it's not a good idea to do i/o to report failure to the user;
instead use exceptions to report the failure up to the caller.

exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);


This is OK if, as seems likely, you don't intend to have a terminating
zero byte in your string representation. However, this is where you
get a crash. That's due to the earlier error noted above.
}

void main()


'main' must have return type 'int'.
{
string s1("My first string");
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Jul 22 '05 #10

P: n/a
I will follow your advice.. perhaps you can advice me on, what you
think is a good book for C++ (numerical app.)

Greetz

"Gary Labowitz" <gl*******@comcast.net> wrote in message news:<6t********************@comcast.com>...
"ranger" <sg****@msn.com> wrote in message
news:ab**************************@posting.google.c om...
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>

class string{
public:
string(void){characters = 0; pt = NULL;}
string(char *s);
private:
int characters;
char *pt;
};

string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}

memcpy(pt, s, characters);
}

void main()
{
string s1("My first string");
}
If you compile it, you'll see that the compiler doesn't report any
error.. but when running it, you'll see that you suddenly get a
windows error report..

Does someone know what the source of this fault might be and what to
change about the code above?

Thanks!!


I'm really sorry to tell you this, but you need a newer/better book. From
the code you show, there are many things wrong. Too many to deal with here
without basically rewriting everything you have. As a beginner you shouldn't
be using most of what you are using. Basics come first, you know.
I'd advise you to forget what you think you know, get a better book (what
are you using, BTW?) and start fresh.

On the other hand, if this is some kind of troll --- Well Done! and go away.

Jul 22 '05 #11

P: n/a
I really appreciate the fact you answered my question!

I'm very new in C++, I'm studying C++ wih numerical applications in my mind..

Still, thanks a lot!!

Dirk Feytons <Di**********@NORUBBISH.easynet.be> wrote in message news:<11***************@seven.kulnet.kuleuven.ac.b e>...
ranger wrote:

[...]
string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){

^^^
This is an assignment, not a comparison.
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}

memcpy(pt, s, characters);
}


[...]
If you compile it, you'll see that the compiler doesn't report any
error.. but when running it, you'll see that you suddenly get a
windows error report..


Increase the warning level of your compiler. Then you'll probably see a
warning about doing an assignment in a comparison context.

Jul 22 '05 #12

P: n/a
I really appreciate the fact you answered my question!

I'm very new in C++, I'm studying C++ wih numerical applications in my mind..

Still, thanks a lot!!

Dirk Feytons <Di**********@NORUBBISH.easynet.be> wrote in message news:<11***************@seven.kulnet.kuleuven.ac.b e>...
ranger wrote:

[...]
string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){

^^^
This is an assignment, not a comparison.
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}

memcpy(pt, s, characters);
}


[...]
If you compile it, you'll see that the compiler doesn't report any
error.. but when running it, you'll see that you suddenly get a
windows error report..


Increase the warning level of your compiler. Then you'll probably see a
warning about doing an assignment in a comparison context.

Jul 22 '05 #13

P: n/a
I really appreciate the fact you answered my question!

I'm very new in C++, I'm studying C++ wih numerical applications in my mind..

Still, thanks a lot!!

al***@start.no (Alf P. Steinbach) wrote in message news:<41*****************@news.individual.net>...
* ranger:
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>


#include <iostream>

<iostream.h> is not a standard C++ header.
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>


#include <cstdio>
#include <cstdlib>
#include <cstring>

is the preferred style for these three headers.
class string{
public:
string(void){characters = 0; pt = NULL;}


'void' as argument list a C'ism; don't use it.

Use an initializer list instead of assignment,

string(): characters(0), pt(NULL) {}

string(char *s);


Argument should be const:

string( char const* s );

You also need a destructor to deallocate the string.

And you need an assignment operator to copy the string, and
a copy constructor.

private:
int characters;
char *pt;
};

string::string(char *s){


string::string( char const* s )
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){


In standard C++ this test will always fail. Operator 'new' reports
failure by means of std::bad_alloc exception, not by returning NULL,
so the test would always fail even if it correctly reflected what
you intended to write. But it doesn't even that: it's an assignment,
not a comparision.

cerr << "Failed to allocate string.\n";


Here you need to qualify using the 'std' namespace:

std::cerr << "Failed to allocate string.\n";

But it's not a good idea to do i/o to report failure to the user;
instead use exceptions to report the failure up to the caller.

exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);


This is OK if, as seems likely, you don't intend to have a terminating
zero byte in your string representation. However, this is where you
get a crash. That's due to the earlier error noted above.
}

void main()


'main' must have return type 'int'.
{
string s1("My first string");
}

Jul 22 '05 #14

P: n/a
I really appreciate the fact you answered my question!

I'm very new in C++, I'm studying C++ wih numerical applications in my mind..

Still, thanks a lot!!

"Dave Townsend" <da********@comcast.net> wrote in message news:<dL********************@comcast.com>...
The reason your code fails is the following line....
if (pt = NULL){


In C and C++ you should use "==" not "=" to compare two
items, what you have done is assign NULL to pt and loose the
value of the address of the memory you alloaced.

dave
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* ranger:
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>


#include <iostream>

<iostream.h> is not a standard C++ header.
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>


#include <cstdio>
#include <cstdlib>
#include <cstring>

is the preferred style for these three headers.
class string{
public:
string(void){characters = 0; pt = NULL;}


'void' as argument list a C'ism; don't use it.

Use an initializer list instead of assignment,

string(): characters(0), pt(NULL) {}

string(char *s);


Argument should be const:

string( char const* s );

You also need a destructor to deallocate the string.

And you need an assignment operator to copy the string, and
a copy constructor.

private:
int characters;
char *pt;
};

string::string(char *s){


string::string( char const* s )
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){


In standard C++ this test will always fail. Operator 'new' reports
failure by means of std::bad_alloc exception, not by returning NULL,
so the test would always fail even if it correctly reflected what
you intended to write. But it doesn't even that: it's an assignment,
not a comparision.

cerr << "Failed to allocate string.\n";


Here you need to qualify using the 'std' namespace:

std::cerr << "Failed to allocate string.\n";

But it's not a good idea to do i/o to report failure to the user;
instead use exceptions to report the failure up to the caller.

exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);


This is OK if, as seems likely, you don't intend to have a terminating
zero byte in your string representation. However, this is where you
get a crash. That's due to the earlier error noted above.
}

void main()


'main' must have return type 'int'.
{
string s1("My first string");
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Jul 22 '05 #15

P: n/a
"ranger" <sg****@msn.com> wrote in message
news:ab**************************@posting.google.c om...
<<top posting moved to bottom>>
"Gary Labowitz" <gl*******@comcast.net> wrote in message news:<6t********************@comcast.com>...
I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h> Headers from C.

class string{
public:
string(void){characters = 0; pt = NULL;} Use of void for empty parameter list, very C.
string(char *s);
private:
int characters;
char *pt;
}; Class, hardly a basic, beginner topic.
string::string(char *s){
characters = strlen(s);
pt = new char[characters]; Allocation using new, probably not basic, beginner topic.
if (pt = NULL){ Incorrect use of assignment instead of comparison operator.
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}

memcpy(pt, s, characters); memcpy a little bit non-basic, beginner topic.
}

void main() Incorrect main signature.
{
string s1("My first string");
}

I'm using "introducing C++ for scientists, engineers and
mathematicians", from 1997. The book is meant for numerical
applications..
As an engineer in automatics I'm working on an application and I
needed to get the basics of C++ as quick as possible.. most of the
time I work on MATLAB.

Still, I regret your answer ..but I'm pleased with the help of all
those who took the time to help me out.. Thanks guys!


From what you say, it appears you are not a troll. Okay. But if you are the
beginner you claim to be, I would suggest learning in some beginner fashion.
It is the rare individual who can just dive into the full C++ language and
produce compilable, working code. Some of the suggestions given you are also
not basic nor beginner topics (like initialization lists on constructors),
and I think this is going to slow you down rather than speed up the learning
process.
Just my opinion, but I've been teaching for 44 years and I've seen a lot of
learners. In general, a more methodical approach works best.
I just wasn't going to get into basic details AND more advance details all
at the same time given some of the basic errors made.

The book you mention isn't reviewed on ACCU, but I suggest you get and look
at Accelerated C++ by Andrew Koenig & Barbara Moo.

But good luck in any case.
--
Gary
Jul 22 '05 #16

P: n/a
On Sat, 20 Nov 2004 14:42:16 -0500, "Gary Labowitz"
<gl*******@comcast.net> wrote in comp.lang.c++:
"ranger" <sg****@msn.com> wrote in message
news:ab**************************@posting.google.c om...
Hi,

I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>

class string{
public:
string(void){characters = 0; pt = NULL;}
string(char *s);
private:
int characters;
char *pt;
};


I'm really sorry to tell you this, but you need a newer/better book.


And I'm really sorry to tell you this, but you need a better
newsreader. The OP posted nicely indented code and you flattened it
out completely when you quoted it.

Consider Free Agent (www.forteinc.com) and Mozilla Thunderbird
(www.mozilla.org), among others. They cost no more than the what you
are using and work much better.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.