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){ch aracters = 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_FAILU RE);
}
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!! 16 1850
"ranger" <sg****@msn.com > wrote in message
news:ab******** *************** ***@posting.goo gle.com... 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){ch aracters = 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_FAILU RE); } 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
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_FAILU RE); } 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.
* 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){ch aracters = 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_FAILU RE); } 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?
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){ch aracters = 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_FAILU RE); } 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?
* 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?
I'm using "introducin g 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*******@comc ast.net> wrote in message news:<6t******* *************@c omcast.com>... "ranger" <sg****@msn.com > wrote in message news:ab******** *************** ***@posting.goo gle.com... 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){ch aracters = 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_FAILU RE); } 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.
Thanks!
Dirk Feytons <Di**********@N ORUBBISH.easyne t.be> wrote in message news:<11******* ********@seven. kulnet.kuleuven .ac.be>... 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_FAILU RE); } 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.
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){ch aracters = 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_FAILU RE); } 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"); }
Thanks! ..I really appreciate your reaction!
"Dave Townsend" <da********@com cast.net> wrote in message news:<dL******* *************@c omcast.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){ch aracters = 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_FAILU RE); } 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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tim Bird |
last post by:
Hi all.
I have recently installed VB2005 so teach myself programming, could anyone
suggest any links to useful websites, or help sites, Ideally I am looking
for tutorials, written for the complete novice
TIA
|
by: Susan Bricker |
last post by:
Greetings!
Still the same application (as previous posts). I worked on the app
while at work (don't tell my boss ... cause this is just for fun and not
work related) and the form was working, but now when I try to repeat the
code at home (for real) it won't work. I suspect that I haven't
replicated the logic and forms at home from what I remember that I did
while at work.
My subform is based on a table called "tblTrialClass" and it...
|
by: gretchen.ogrady |
last post by:
I admit - I'm a simple user but looking to improve skills.
Instructions aren't helping and have searched this group but am getting
bogged down by some of the programming-speak.
I have a query called PAL_PA Summary and in that query I calculate
several fields, all %s: Fed, ITS, SALT which are 3 of the 6 columns for
example. Basically its a query by person that shows the % of their time
spent in these diff fields (they add up to 100%).
...
|
by: trond |
last post by:
Hello all,
Before I start I'd like to point out that I am a complete novice when
it comes to asp.net - My background is in network and operating
systems, and although I have been doing a bit of hobby programming in
vb.net and web programming in asp/vbscript in the past, I am pretty
much a beginner at asp.net.
What I'm trying to do, is to take the "PersonalHomePage" starter kit
that MS supplies with VS2005 and tweak it a bit, to make it...
|
by: GJP |
last post by:
Hello.
I'm doing a computing course and thinking of designing a network management
program.
Basically, it would ping an IP address at a certain time and then wait for a
response, an action would then depend on if a repose is returned or not.
I have very limited knowledge of VB.net at the moment, but am being taught
it.
| |
by: Tarun Mistry |
last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies
to both (apologies if it breaks some group rules).
I am making a web app in asp.net using c#. This is the first fully OO
application I will be making, also my first .NET application, so im looking
for any help and guidance.
Ok, my problems are todo with object and database abstraction, what should i
do.
|
by: weidongtom |
last post by:
Hi,
I tried to implement the Universal Machine as described in
http://www.boundvariable.org/task.shtml, and I managed to get one
implemented (After looking at what other's have done.) But when I use
to run a UM program, I kept on getting error messages. I have used
someone else's implementation and it runs fine. I have compared my
code with other's and I still can't figure it out what's wrong with
mine. So please help me out, after 3...
|
by: loveshack |
last post by:
Can anyone help me please (i am quite a novice, but having fun learning). Im not sure if this is an ASP problem, a javascript problem or a browser problem.
Firstly, everything i have written works fine in IE7 and beta IE8. My pages do not work however in Safari or Firefox, and please dont beat me, but i use Frontpage to write my site, and i use iframes as ive not discovered how to do this any differently!
So the issue is this, i have a...
|
by: =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?= |
last post by:
Mo <Mehile.Orloff@gmail.comwrote:
may be the iteration should be inside your function qryFunc ?
or, return $fQuery (when no error)
and then :
$reponse=qryFunc($select, $from);
|
by: zumin |
last post by:
Hi guys. I'm having a pain with this.
What I want to do is be able to click a link. from a picture and have it open up a table cell.
My dilema is it is for ebay. So I cannot use java, Iframes, meta, cookie, base href
I have already tried using a table name and setting target to table name but this does not work.
I am using dreamweaver, and a txt editor, but i am still a bit of a novice with html.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |