junk characters generated with new operator

B

bintom

Hey, can anybody tell me what I'm doing wrong in the following
program?
The output generated is some junk characters followed by 5 (the size
of "Texas").
Thanks,
Bintom


#include <iostream.h>

class state
{ char *name;
int size;
public : state(char* s)
{ size = strlen(s);
name = new char[size + 1];
}

void display()
{ cout << name << " " << size << "\n"; }
};

int main()
{ state S1("Texas");

S1.display();}
}
 
P

Paul N

Hey, can anybody tell me what I'm doing wrong in the following
program?
The output generated is some junk characters followed by 5 (the size
of "Texas").
Thanks,
Bintom

#include <iostream.h>

class state
{ char *name;
  int size;
  public : state(char* s)
             { size = strlen(s);
               name = new char[size + 1];
             }

             void display()
             { cout << name << "  " << size << "\n";   }

};

int main()
{ state S1("Texas");

  S1.display();}



}

Your problem is that, while you are allocating some space for "name"
which is big enough to hold the name of the state, you aren't actually
copying the name into that space.

If I were writing the program I'd probably use strcpy(name, s) to copy
the name in.

Most people here would probably recommend that you use a string
instead of a char pointer. And they'd probably suggest changing
<iostream.h> to <iostream>, which will mean putting a "std" somewhere
in your code. It's up to you how much of this you go along with.

Hope that helps.
Paul.
 
R

red floyd

And they'd probably suggest changing
<iostream.h> to <iostream>, which will mean putting a "std" somewhere
in your code. It's up to you how much of this you go along with.

So you recommend using non-standard headers? Even MSVC doesn't have
<iostream.h> any more.
 
J

Juha Nieminen

Pete Becker said:
Um, no, the suggestion is just the opposite. The original code used
<iostream.h>, and the suggestion was to change that to <iostream>.

Read it again. It's not suggesting it, it's saying that "people
will suggest it, it's up to you to decide". Writing non-standard code
is not a good idea, so the "decision" is a no-brainer.
 
P

Paul N

  Read it again. It's not suggesting it, it's saying that "people
will suggest it, it's up to you to decide". Writing non-standard code
is not a good idea, so the "decision" is a no-brainer.

Perhaps I should have stuck to just answering the question, which the
OP seemed perfectly happy with...

I don't know the OP's circumstances. Possibly he is using an old
compiler which (like the older of my two compilers) simply doesn't
have <iostream>. Or perhaps he would be better off concentrating on
getting his code working on his system rather than worrying about how
it will port to other systems. Or, conversely, perhaps he would indeed
be better off learning about standard code at this stage. I was just
trying to give a pointer without being too prescriptive.

Incidentally, I hardly ever use either myself; for DOS programs I
either use stdio.h or access the screen memory directly, for Windows
programs I have to use other techniques anyway. I suspect I'm not
typical in this group, just as I suspect even more strongly that my
preference for C-type string handling over C++ strings is not typical
of the group.

Hope that clears up any doubt.
Paul.
 
S

Sprechen sie C++

"bintom" wrote in message

Thanks Paul,

I don't know how I goofed up on that.


Microsoft gives away the express editions of Visual Studio for free. The
2010 version supports modern programming constructs
 

Ask a Question

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

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

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top