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

I need some basic C++ help

P: n/a
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
thanks for any help you can give me.
Jul 22 '05 #1
Share this Question
Share on Google+
41 Replies


P: n/a

"Psykarrd" <ps******@hotmail.com> wrote in message
news:19**************************@posting.google.c om...
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
thanks for any help you can give me.


strcpy(name, "modem");

An expression such as "modem" is viewed by the compiler as a const char *
(which points to an address in memory where the string literal "modem" is
stored).

So, the expression you gave was trying to assign an address to array. As
such an operation does not make sense to do, your compiler yelped!
Jul 22 '05 #2

P: n/a
Psykarrd wrote:
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
...


Both 'name' and "modems" are _arrays_. Arrays in C++ are not assignable.
In order to copy one array to another in general case you have to copy
them "manually" - element by element. The good news is that in most
cases there's a library function that can do it for you.

In this particular case you can use 'strcpy':

strcpy(name, modem);

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #3

P: n/a

"Dave" <be***********@yahoo.com> wrote in message
news:vr************@news.supernews.com...

"Psykarrd" <ps******@hotmail.com> wrote in message
news:19**************************@posting.google.c om...
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
thanks for any help you can give me.


strcpy(name, "modem");

An expression such as "modem" is viewed by the compiler as a const char *
(which points to an address in memory where the string literal "modem" is
stored).

So, the expression you gave was trying to assign an address to array. As
such an operation does not make sense to do, your compiler yelped!


Oops, sorry, I should have said "array of const char" instead of "const char
*". So, it's really an array, and *then* that array undergoes a conversion
to a pointer when the compiler encounters "modem". Finally then, as I said
before, a pointer cannot be assigned to an array...
Jul 22 '05 #4

P: n/a

"Psykarrd" <ps******@hotmail.com> wrote in message
news:19**************************@posting.google.c om...
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";


Arrays are not assignable.

strcpy(name, "modem"); /* 'strcpy()' is declared by <cstring>
or <string.h>

Better yet, use the std::string type, which *is* assignable,
and also has the advantage of not being restricted to a fixed
size like an array:

std::string name; /* 'std::string' declared by <string> */
name = "modem";

Or if "modem" is to be the inital value:

std::string name("modem");

-Mike
Jul 22 '05 #5

P: n/a
"Dave" <be***********@yahoo.com> wrote in message
news:vr************@news.supernews.com

Oops, sorry, I should have said "array of const char" instead of
"const char
*". So, it's really an array, and *then* that array undergoes a
conversion to a pointer when the compiler encounters "modem".
Finally then, as I said before, a pointer cannot be assigned to an
array...


It is not the chars that are const --- if they were you couldn't change the
char values. Rather, it is the location in memory of the chars that cannot
be changed. In pointer terms, it is like a const pointer to char, i.e.,
char * const
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #6

P: n/a

"John Carson" <do***********@datafast.net.au> wrote in message
news:3f******@usenet.per.paradox.net.au...
"Dave" <be***********@yahoo.com> wrote in message
news:vr************@news.supernews.com

Oops, sorry, I should have said "array of const char" instead of
"const char
*". So, it's really an array, and *then* that array undergoes a
conversion to a pointer when the compiler encounters "modem".
Finally then, as I said before, a pointer cannot be assigned to an
array...
It is not the chars that are const --- if they were you couldn't change

the char values. Rather, it is the location in memory of the chars that cannot
be changed. In pointer terms, it is like a const pointer to char, i.e.,
char * const
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


2.13.4-1: An ordinary string literal has type "array of n const char"

And indeed, you may not change the characters of a string literal.
Jul 22 '05 #7

P: n/a
Looking at the way you're wanting to use the variable, perhaps you'll be better
off using a string variable instead of char. i.e.
#include <string>. You'll them be able to do what you seem to have in mind.

string name;

name = "modem";

and you can still access the individual components of name:

for(int count = 0; count < strlen(name); count++)
cout << name[count] << "," ;
cout << endl;

outputs: m, o, d, e, m,

Jul 22 '05 #8

P: n/a
ps******@hotmail.com (Psykarrd) wrote in message news:<19**************************@posting.google. com>...
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
thanks for any help you can give me.


hi!

It is better to use the string class

#include<string>

string s = "modem";
cout << s << "\n";

Best,

Raj
Jul 22 '05 #9

P: n/a
John Carson wrote:
"Dave" <be***********@yahoo.com> wrote in message
news:vr************@news.supernews.com

Oops, sorry, I should have said "array of const char" instead of
"const char
*". So, it's really an array, and *then* that array undergoes a
conversion to a pointer when the compiler encounters "modem".
Finally then, as I said before, a pointer cannot be assigned to an
array...


It is not the chars that are const --- if they were you couldn't change the
char values. Rather, it is the location in memory of the chars that cannot
be changed. In pointer terms, it is like a const pointer to char, i.e.,
char * const


I think Dave was talking about the literal. String literal is an "array
of const char" and its 'char' values cannot be changed.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #10

P: n/a
"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:vr************@news.supernews.com
John Carson wrote:
"Dave" <be***********@yahoo.com> wrote in message
news:vr************@news.supernews.com

Oops, sorry, I should have said "array of const char" instead of
"const char
*". So, it's really an array, and *then* that array undergoes a
conversion to a pointer when the compiler encounters "modem".
Finally then, as I said before, a pointer cannot be assigned to an
array...


It is not the chars that are const --- if they were you couldn't
change the char values. Rather, it is the location in memory of the
chars that cannot be changed. In pointer terms, it is like a const
pointer to char, i.e., char * const


I think Dave was talking about the literal. String literal is an
"array
of const char" and its 'char' values cannot be changed.

You are right. I didn't read the post carefully enough.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #11

P: n/a
"Dave" <be***********@yahoo.com> wrote in message
news:vr************@news.supernews.com
"John Carson" <do***********@datafast.net.au> wrote in message
news:3f******@usenet.per.paradox.net.au...
"Dave" <be***********@yahoo.com> wrote in message
news:vr************@news.supernews.com

Oops, sorry, I should have said "array of const char" instead of
"const char
*". So, it's really an array, and *then* that array undergoes a
conversion to a pointer when the compiler encounters "modem".
Finally then, as I said before, a pointer cannot be assigned to an
array...


It is not the chars that are const --- if they were you couldn't
change the char values. Rather, it is the location in memory of the
chars that cannot be changed. In pointer terms, it is like a const
pointer to char, i.e., char * const
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)


2.13.4-1: An ordinary string literal has type "array of n const char"

And indeed, you may not change the characters of a string literal.

My mistake. I misread your post as referring to the array

char name[80];

rather than to the string literal.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #12

P: n/a

"John Carson" <do***********@datafast.net.au> wrote in message
news:3f********@usenet.per.paradox.net.au...
"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:vr************@news.supernews.com
John Carson wrote:
"Dave" <be***********@yahoo.com> wrote in message
news:vr************@news.supernews.com
>
> Oops, sorry, I should have said "array of const char" instead of
> "const char
> *". So, it's really an array, and *then* that array undergoes a
> conversion to a pointer when the compiler encounters "modem".
> Finally then, as I said before, a pointer cannot be assigned to an
> array...

It is not the chars that are const --- if they were you couldn't
change the char values. Rather, it is the location in memory of the
chars that cannot be changed. In pointer terms, it is like a const
pointer to char, i.e., char * const


I think Dave was talking about the literal. String literal is an
"array
of const char" and its 'char' values cannot be changed.

You are right. I didn't read the post carefully enough.


Although I think we've long since answered the OPs question, I'm now
starting to wonder about the accuracy of another part of my post.
Specifically, I'm wondering about where I stated the array is converted to a
pointer. Though such a conversion is one of the implicit conversions
described by the Standard, *does* the conversion happen here? Would there
be any reason for it to? After all, it's not as if we're trying to match
the array to a pointer parameter of a function, or some other such case
where a conversion is *needed*. Here, the assignment of an array (a string
literal) to another array is illegal to start with, so why would a
conversion happen only to come up with another construct (the assignment of
a pointer to an array) that is still just as illegal? So, I may have been
incorrect in that portion of my post. Can anybody confirm or deny this
authoritatively? I'd like to know for sure now that I've thought about it a
little longer...
Jul 22 '05 #13

P: n/a
main returns an 'int', not void

"Psykarrd" <ps******@hotmail.com> wrote in message news:19**************************@posting.google.c om...
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];

then when i try to use the variable it dosn't work, however i am not
sure you can use it the way i am trying to. Could some one please tell
me what i am doing wrong, or another way of doing the same thing. i am
trying to use the variable like this.

name = "modem";
thanks for any help you can give me.

Jul 22 '05 #14

P: n/a
strcpy(name, "modem");
Jul 22 '05 #15

P: n/a
Hi there,

a better idea would almost certainly be for you to use std::string, as
it takes care of expanding the array for you (how can you be sure that
name[] will always be <79 chars?):

#include <string>
using std::string;

string name;
name = "modem"; // string expands automatically
name = "sfasfasdfasdf;asdf;kasdjf;aslkdfa;ksldjfas;dlfkj" ; // ditto

with std::string, you can also easily do string copying:
std::string mycopy = name;
If you were using raw char arrays, you'd have to do strcpy(), etc ...
std::string is just an easier (clearer and better) way to do it.

PS if you need a C-style char* of your string, you can just use the
..c_str() member function.
Jul 22 '05 #16

P: n/a
> I am trying to declare a string variable as an array of char's. the
code looks like this. char name[80];


If you want a string, use a string:

#include <string>

...

std::string name;

Whoever suggested using arrays of characters as strings gave you bad advice.
If the advice came from a book from which you are learning C++, I suggest
you try a different book.
Jul 22 '05 #17

P: n/a

"Andrew Koenig" <ar*@acm.org> wrote in message
news:ZY*********************@bgtnsc05-news.ops.worldnet.att.net...
I am trying to declare a string variable as an array of char's. the
code looks like this.

char name[80];


If you want a string, use a string:


Amen, brother.
Jul 22 '05 #18

P: n/a

"Andrew Koenig" <ar*@acm.org> wrote in message
news:ZY*********************@bgtnsc05-news.ops.worldnet.att.net...
I am trying to declare a string variable as an array of char's. the
code looks like this.
char name[80];


If you want a string, use a string:

#include <string>

...

std::string name;

Whoever suggested using arrays of characters as strings gave you bad

advice. If the advice came from a book from which you are learning C++, I suggest
you try a different book.


I should think using char [ ] for strings must still be taught. Firstly, it
is an intuitive way to "see" arrays and learn to use them. Secondly, there
is lots of code extant that uses them and a beginner better learn to
understand it. Thirdly, it still seems easier to me to teach it first and
then switch to standard string class. The relief or not having to deal with
the problems of char [ ] shows the benefit of the standard libraries
clearly. But making the switch also involves discussion of namespaces which
I tend to defer until the little coders are reasonably confident using the
scope resolution operator to access globals, statics, and classes.

Without knowing what book, I'd say it isn't the best advice to abandon it
just yet. I'm aware you prefer to teach standard C++ from the outset and
fill in the retrograde topics later. Besides, I can't get the school to use
your books -- damned committees!
--
Gary
Jul 22 '05 #19

P: n/a
> I should think using char [ ] for strings must still be taught. Firstly,
it
is an intuitive way to "see" arrays and learn to use them. Secondly, there
is lots of code extant that uses them and a beginner better learn to
understand it. Thirdly, it still seems easier to me to teach it first and
then switch to standard string class. The relief or not having to deal with the problems of char [ ] shows the benefit of the standard libraries
clearly. But making the switch also involves discussion of namespaces which I tend to defer until the little coders are reasonably confident using the
scope resolution operator to access globals, statics, and classes.
I agree with you that every professional C++ programmer must eventually
learn how to use arrays. However, I don't think that teaching them before
teaching strings is the most useful way to go. The trouble is that
beginners have enough information to absorb that there's a very good chance
that they will be unable to write programs that work correctly for quite a
while--long enough for them to become frustrated.

I have taught C++ both ways, and my experience is that the students learn a
lot faster when they start with strings, then move to arrays of characters.
One reason is that they spend much less time debugging.
Without knowing what book, I'd say it isn't the best advice to abandon it
just yet. I'm aware you prefer to teach standard C++ from the outset and
fill in the retrograde topics later. Besides, I can't get the school to use your books -- damned committees!


Interesting. I'd like to know what the committees' objections are. Perhaps
we can discuss this offline?
Jul 22 '05 #20

P: n/a

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:Ob********************@comcast.com...

I should think using char [ ] for strings must still be taught. Firstly, it is an intuitive way to "see" arrays and learn to use them.
Disagree. I think there are better examples for arrays. In fact, I think a
string is a poor example. Strings as objects come very naturally to most
people, and making an array out of it just obfuscates it.
Secondly, there
is lots of code extant that uses them and a beginner better learn to
understand it.
Partially disagree - a "beginner" is rarely going to be doing code
maintenance. I agree that a programmer needs to learn this eventually if he
wants to be a professional.
Thirdly, it still seems easier to me to teach it first and
then switch to standard string class. The relief or not having to deal with the problems of char [ ] shows the benefit of the standard libraries
clearly.


Disagree again. The standard library isn't there to make people realize how
good they have it. How about if we drill some cavities for you without
novacaine just so the benefit of it will really be made clear to you? Or
use a screwdriver to pound in a hammer, so that first hammer purchase will
be oh-so-sweet? The point of the standard library is to use it -
immediately.
Jul 22 '05 #21

P: n/a

"Andrew Koenig" <ar*@acm.org> wrote in message
news:3p***********************@bgtnsc04-news.ops.worldnet.att.net...
Besides, I can't get the school to

use
your books -- damned committees!


Interesting. I'd like to know what the committees' objections are.


First guess: the teachers there already wrote books, or who have friends who
have. Do I win?
Jul 22 '05 #22

P: n/a
Andrew Koenig wrote:

I have taught C++ both ways, and my experience is that the students learn a
lot faster when they start with strings, then move to arrays of characters.
One reason is that they spend much less time debugging.


I once had the pleasure of an hour comp sci lecture on "for
loops and arrays in Modula2". 40 minutes was spent on how to
use a for loop. How I stayed awake I don't know, but after
the 40 minutes came the statement:

"Now lets turn to arrays. I don't want to complicate
matters, because we only have 20 minutes left, so we'll
only be looking a one dimensional arrays."

then up on the OHP went a slide showing an array of String
and indexing into the characters. Damn funny.

Jul 22 '05 #23

P: n/a
"Andrew Koenig" <ar*@acm.org> wrote in message
news:3p***********************@bgtnsc04-news.ops.worldnet.att.net...
Interesting. I'd like to know what the committees' objections are. Perhaps we can discuss this offline?


No need to go offline. Either the rep has done a better job indoctrinating
them ("Want some more free books?") or "We have always used this book."
Some of the teachers I've seen there don't know the languages they are
trusted with teaching very well, either.
--
Gary
Jul 22 '05 #24

P: n/a
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:Ob********************@comcast.com...
Partially disagree - a "beginner" is rarely going to be doing code
maintenance. I agree that a programmer needs to learn this eventually if

he wants to be a professional.
I'm not so sure about that. It seems I recall that new addition to staff was
put on "maintenance." I kept telling management that they should put their
best people on maintaining code, largely because it's critical. No hope. The
experienced guys all seem to want the thrill of working on something new.
Thirdly, it still seems easier to me to teach it first and
then switch to standard string class. The relief or not having to deal with
the problems of char [ ] shows the benefit of the standard libraries
clearly.


Disagree again. The standard library isn't there to make people realize

how good they have it. How about if we drill some cavities for you without
novacaine just so the benefit of it will really be made clear to you? Or
use a screwdriver to pound in a hammer, so that first hammer purchase will
be oh-so-sweet? The point of the standard library is to use it -
immediately.


You're right. I guess I could just teach what I know and throw the book
away.
I did it once. I got bad reviews from the students (did you know they ask
they students what they think of the teacher to make the judgment?) that
said I contradicted the book, kept telling them it was wrong in places, and
why should they have to buy the book if it's no good? Good question.
Jul 22 '05 #25

P: n/a

"jbruno4000" <jb********@aol.com> wrote in message
news:20***************************@mb-m18.aol.com...
Looking at the way you're wanting to use the variable, perhaps you'll be better off using a string variable instead of char. i.e.
#include <string>. You'll them be able to do what you seem to have in mind.
string name;

name = "modem";

and you can still access the individual components of name:

for(int count = 0; count < strlen(name); count++)
cout << name[count] << "," ;
cout << endl;

outputs: m, o, d, e, m,

I thought the above code didn't look right. I have needed to do something
like this on more than one occasion and it just does not work.

strlen(name) gives an error of the following type:
cannot convert parameter 1 from 'class std ????' to const char *

What is the work around to this to do the same thing?
Thanks

Jul 22 '05 #26

P: n/a
hi!
strlen(name) gives an error of the following type:
cannot convert parameter 1 from 'class std ????' to const char *

sure,... strlen is not defined to take a std::string as parameter...
use strlen(name.c_str()) if you really want to use strlen

or better name.length()

(instead of position-increment, you could use iterators in the for-loop)

regards,
sev
Jul 22 '05 #27

P: n/a
guess I should have tested the code but you need to take on some of the
responsibility as well. If you type search 'strlen' in your help files it
should give examples on it's usage.
Jul 22 '05 #28

P: n/a
"Gary Labowitz" <gl*******@comcast.net> wrote in message news:<Ob********************@comcast.com>...
I should think using char [ ] for strings must still be taught. Firstly, it
is an intuitive way to "see" arrays and learn to use them.


Is it? char arrays for strings are more than just arrays. They have a
special terminating element. All the C library string functions expect
and utilise it. You have to take steps to cope with that (eg add 1 to
the array size, remember that strlen doesn't count the terminator).

Nothing very taxing[*] when you get familiar with it, but it's a
whole extra feature that isn't part of the concept of arrays. Does
that get in the way of teaching about arrays?

GJD
[*] Though more taxing than using a std::string.
Jul 22 '05 #29

P: n/a
In article <cw********************@news1.epix.net>,
kazack <ka****@talon.net> wrote:

"jbruno4000" <jb********@aol.com> wrote in message
news:20***************************@mb-m18.aol.com...

for(int count = 0; count < strlen(name); count++)
cout << name[count] << "," ;
cout << endl;


strlen(name) gives an error of the following type:
cannot convert parameter 1 from 'class std ????' to const char *

What is the work around to this to do the same thing?


Change strlen(name) to name.length()

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #30

P: n/a

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:5J********************@comcast.com...
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:Ob********************@comcast.com...
Partially disagree - a "beginner" is rarely going to be doing code
maintenance. I agree that a programmer needs to learn this eventually

if he
wants to be a professional.
I'm not so sure about that. It seems I recall that new addition to staff

was put on "maintenance." I kept telling management that they should put their
best people on maintaining code, largely because it's critical. No hope. The experienced guys all seem to want the thrill of working on something new.
I'm not sure what you mean. You're talking about "staff". I assume that
means they're getting paid. Are you saying that since they couldn't get the
paid staff to do it, they brought in students to maintain the code for free?
I understand that many programmers prefer new development over maintenance,
but that by itself is a red herring to the discussion.
You're right. I guess I could just teach what I know and throw the book
away.
I did it once. I got bad reviews from the students (did you know they ask
they students what they think of the teacher to make the judgment?) that
said I contradicted the book, kept telling them it was wrong in places, and why should they have to buy the book if it's no good? Good question.


Yeah, that's a tough one. There is a common perception among people who are
learning that if it's in print, it has some special authority. Here's one
possible suggestion: I had a teacher once who used a book for a algorithm
course that she thought was especially good in many respects, despite its
numerous typos and little flaws. She acknowledged them right up front,
before we even got our hands on the book, so we never had a chance to assume
it was gospel. Then she had a little "contest", where every flaw we found
in the book got us a little extra "point" toward our grade for the course.
That made her look like the one in charge, not the book. Boy, some of those
flaws were hard to find (we couldn't understand half the algorithms to begin
with, how could we find flaws?) But we never doubted her word over the
book's.
Jul 22 '05 #31

P: n/a

"Gavin Deane" <de*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...

Is it? char arrays for strings are more than just arrays. They have a
special terminating element. All the C library string functions expect
and utilise it. You have to take steps to cope with that (eg add 1 to
the array size, remember that strlen doesn't count the terminator).

Nothing very taxing[*] when you get familiar with it...


At least tedious if not taxing.....
Jul 22 '05 #32

P: n/a
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:5J********************@comcast.com...
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
<<snip>> I'm not sure what you mean. You're talking about "staff". I assume that
means they're getting paid. Are you saying that since they couldn't get the paid staff to do it, they brought in students to maintain the code for free? I understand that many programmers prefer new development over maintenance, but that by itself is a red herring to the discussion.


Yes, they brought in students to do it. Of course, the students had
graduated and were then in the working force pool. They certainly didn't
maintain code for free.

When I was in grad school, on the first day a professor in a finite math
class pointed out an error in the book we were to use on page 1! He made the
comment that the author was an authority on the subject, but there was an
elementary mistake. He then called on one student to show the correction on
the board.
The student (a high school teacher taking the course in order to "qualify"
for a raise) put the following on the board (among other equations)
(a + b) ^2 = a^2 + b^2 [but using superscripts for the squaring]
The instructor looked at it and said, "Okay, is that right?" The student
said yes. The instructor said, "Okay, for what conditions of a and b is this
correct?"
No answer. The instructor said, "Pick up your book, go directly to the
registrar's office, and drop this course." And he waited until the guy left.

I have often wished I could do that, but schools now-a-days push what they
call "retention." You are measured on it, and it means: can you keep as many
students as possible enrolled, regardless of how well they do?
--
Gary
Jul 22 '05 #33

P: n/a
I think the idea of using arrays for strings (one that comes entirely
from C) was a quick, effective and intelligent solution for creating
strings using other data type elements in the language. For that
matter "char"-s and "string"-s are types derived from "int"-s and
arrays. I feel, the way we construct char arrays with terminating
zeros is so that we can write functions that process such arrays as
strings.

If there was no C, only C++, I would presume we would never speak
about "char*"-s ... only "std::string"-s ... or in a multi-lingual
world ... of std::basic_string, leaving "char*"-s as an implementation
detail perhaps.

Having said that ... I wonder how judgmental can we get regarding the
appropriateness of teaching "char*"-s before "string"-s. If it's a
quick C++ course where the students are expected to get up to speed in
using strings, I guess it's a good idea to jump to std::string. If
however, the language elements are to be understood thoroughly, then
the students need to see how strings can be constructed using arrays
.... how well they serve specific needs and where they fall short ...
whereby the need for better alternatives arise and what those
alternatives are. The reason I say this is because students need to
have a motivation for learning something. And the motivation can come
from the understanding the need to use one method over the other.
std::string is not a small isolated topic ... it is merely a typedef
for a generic stl class. May be they should be taught with this fact
in context ... which would require them to have the more basic
language elements in control.

--
Cheers,
Andy

"Andrew Koenig" <ar*@acm.org> wrote in message news:<3p***********************@bgtnsc04-news.ops.worldnet.att.net>...
I should think using char [ ] for strings must still be taught. Firstly,

it
is an intuitive way to "see" arrays and learn to use them. Secondly, there
is lots of code extant that uses them and a beginner better learn to
understand it. Thirdly, it still seems easier to me to teach it first and
then switch to standard string class. The relief or not having to deal

with
the problems of char [ ] shows the benefit of the standard libraries
clearly. But making the switch also involves discussion of namespaces

which
I tend to defer until the little coders are reasonably confident using the
scope resolution operator to access globals, statics, and classes.


I agree with you that every professional C++ programmer must eventually
learn how to use arrays. However, I don't think that teaching them before
teaching strings is the most useful way to go. The trouble is that
beginners have enough information to absorb that there's a very good chance
that they will be unable to write programs that work correctly for quite a
while--long enough for them to become frustrated.

I have taught C++ both ways, and my experience is that the students learn a
lot faster when they start with strings, then move to arrays of characters.
One reason is that they spend much less time debugging.
Without knowing what book, I'd say it isn't the best advice to abandon it
just yet. I'm aware you prefer to teach standard C++ from the outset and
fill in the retrograde topics later. Besides, I can't get the school to

use
your books -- damned committees!


Interesting. I'd like to know what the committees' objections are. Perhaps
we can discuss this offline?

Jul 22 '05 #34

P: n/a

"kazack" <ka****@talon.net> wrote in message
news:cw********************@news1.epix.net...

"jbruno4000" <jb********@aol.com> wrote in message
news:20***************************@mb-m18.aol.com...
Looking at the way you're wanting to use the variable, perhaps you'll be

better
off using a string variable instead of char. i.e.
#include <string>. You'll them be able to do what you seem to have in

mind.

string name;

name = "modem";

and you can still access the individual components of name:

for(int count = 0; count < strlen(name); count++)
cout << name[count] << "," ;
cout << endl;

outputs: m, o, d, e, m,

I thought the above code didn't look right. I have needed to do something
like this on more than one occasion and it just does not work.

strlen(name) gives an error of the following type:
cannot convert parameter 1 from 'class std ????' to const char *

What is the work around to this to do the same thing?


Use the interface, Luke! :-)

name.size()

or

name.length()
Or if you insist upon using 'strlen()':

strlen(name.c_str())

-Mike
Jul 22 '05 #35

P: n/a

"jbruno4000" <jb********@aol.com> wrote in message
news:20***************************@mb-m18.aol.com...
Looking at the way you're wanting to use the variable, perhaps you'll be better off using a string variable instead of char. i.e.
#include <string>. You'll them be able to do what you seem to have in mind.
string name;

name = "modem";

and you can still access the individual components of name:

for(int count = 0; count < strlen(name); count++)


This is not correct.

for(string::size_type count = 0; count < name.size(); ++count)
/* etc */

-Mike
Jul 22 '05 #36

P: n/a

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:Ob********************@comcast.com...
clearly. But making the switch also involves discussion of namespaces which I tend to defer until the little coders are reasonably confident using the
scope resolution operator to access globals, statics, and classes.


So how do you tell them to do "Hello World"?

-Mike
Jul 22 '05 #37

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:%X******************@newsread1.news.pas.earth link.net...

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:Ob********************@comcast.com...
clearly. But making the switch also involves discussion of namespaces

which
I tend to defer until the little coders are reasonably confident using the scope resolution operator to access globals, statics, and classes.


So how do you tell them to do "Hello World"?


#include <iostream.h>

int main( )
{
cout << "Hello World" << endl;
return 0;
}

Then we add a class with public data; then add public function and change
data to private; then discuss class names (Class::showData( ) for the show
data function); then we make objects of a class; then we add namespaces
outside of classes; then we switch header files to headers using namespace
std; then we cover friends; then we do overloading of functions; then we
overload operators; and all the while discuss the three ways to refer to
variables, functions, and members (directly by identifier, using a pointer,
and by reference); and eventually we have

#include <iostream>
using namespace std;

class X
{
int x;
public:
void showData( );
};

void X::showData( )
{
cout << "Here is x: " << x << endl;
}

int main( )
{
X myX;
myX.showData( );
return 0;
}

All this takes about eight weeks. You tell me, are kids getting dumber? The
whole thing took us about one week using notepad and command line. Of
course, we now have the "simplicity" of using Dev-C++ or Visual C++ and
creating projects and forgetting where everything is and writing code that
never lines up, and, and, and ...
--
Gary
Jul 22 '05 #38

P: n/a

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:4U******************@newsread1.news.pas.earth link.net...

"kazack" <ka****@talon.net> wrote in message
news:cw********************@news1.epix.net...

"jbruno4000" <jb********@aol.com> wrote in message
news:20***************************@mb-m18.aol.com...
Looking at the way you're wanting to use the variable, perhaps you'll
be better
off using a string variable instead of char. i.e.
#include <string>. You'll them be able to do what you seem to have in

mind.

string name;

name = "modem";

and you can still access the individual components of name:

for(int count = 0; count < strlen(name); count++)
cout << name[count] << "," ;
cout << endl;

outputs: m, o, d, e, m,

I thought the above code didn't look right. I have needed to do something like this on more than one occasion and it just does not work.

strlen(name) gives an error of the following type:
cannot convert parameter 1 from 'class std ????' to const char *

What is the work around to this to do the same thing?


Use the interface, Luke! :-)

name.size()

or

name.length()
Or if you insist upon using 'strlen()':

strlen(name.c_str())

-Mike

Which is better, or more efficient, or accepted?
Jul 22 '05 #39

P: n/a
"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:p4********************@comcast.com...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:%X******************@newsread1.news.pas.earth link.net...

"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:Ob********************@comcast.com...
clearly. But making the switch also involves discussion of namespaces which
I tend to defer until the little coders are reasonably confident using the scope resolution operator to access globals, statics, and classes.


So how do you tell them to do "Hello World"?


#include <iostream.h>


But why teach them about a nonstandard header?

int main( )
{
cout << "Hello World" << endl;
return 0;
}

Then we add a class with public data; then add public function and change
data to private; then discuss class names (Class::showData( ) for the show
data function); then we make objects of a class; then we add namespaces
outside of classes; then we switch header files to headers using namespace
std; then we cover friends; then we do overloading of functions; then we
overload operators; and all the while discuss the three ways to refer to
variables, functions, and members (directly by identifier, using a pointer, and by reference); and eventually we have

#include <iostream>
IMO you should have used this header from the start.
using namespace std;
And perhaps convince them to take the 'namespace issue'
'on faith' until you feel they're ready for more in-depth
explanations.

class X
{
int x;
public:
void showData( );
};

void X::showData( )
{
cout << "Here is x: " << x << endl;
}

int main( )
{
X myX;
myX.showData( );
return 0;
}

All this takes about eight weeks. You tell me, are kids getting dumber?
I don't think they're 'getting dumber', but imo (at least in
the U.S.) that the quality of schools leaves *much* to be
desired. I suppose also a child's 'home life' and parenting
issues could contribute to simply laziness. :-)
The
whole thing took us about one week using notepad and command line. Of
course, we now have the "simplicity" of using Dev-C++ or Visual C++ and
creating projects and forgetting where everything is and writing code that
never lines up, and, and, and ...


I am a proponent of students initially *not* using an IDE,
as it hides what's really going on and promotes laziness.
I would only introduce an IDE when they're ready to see
it as a productivity tool, rather than a vehicle to hide
details.

But I'm not a teacher, so perhaps I don't know what I'm
talking about. :-)

-Mike
who wrote his first program in assembly language, using
an IBM 029 card-punch machine, and ran it on a 1950's
model computer (IBM 1620). :-)

Jul 22 '05 #40

P: n/a

"kazack" <ka****@talon.net> wrote in message
news:O9********************@news1.epix.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:4U******************@newsread1.news.pas.earth link.net...

"kazack" <ka****@talon.net> wrote in message
news:cw********************@news1.epix.net...

"jbruno4000" <jb********@aol.com> wrote in message
news:20***************************@mb-m18.aol.com...
> Looking at the way you're wanting to use the variable, perhaps you'll
be
better
> off using a string variable instead of char. i.e.
> #include <string>. You'll them be able to do what you seem to have
in mind.
>
> string name;
>
> name = "modem";
>
> and you can still access the individual components of name:
>
> for(int count = 0; count < strlen(name); count++)
> cout << name[count] << "," ;
> cout << endl;
>
> outputs: m, o, d, e, m,
>
I thought the above code didn't look right. I have needed to do

something like this on more than one occasion and it just does not work.

strlen(name) gives an error of the following type:
cannot convert parameter 1 from 'class std ????' to const char *

What is the work around to this to do the same thing?


Use the interface, Luke! :-)

name.size()

or

name.length()
Or if you insist upon using 'strlen()':

strlen(name.c_str())

-Mike

Which is better, or more efficient, or accepted?


Forget about 'efficiency'. Strive to write correct, clear code.
Only if a profiler proves a performance problem should one be
concerned with optimzation.

Others might differ, but my opinion is that if you're using C++
and want to use a string, use std::string and its member
functions rather than 'C-style' strings.

-Mike
Jul 22 '05 #41

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<eg******************@newsread1.news.pas.eart hlink.net>...
"Gary Labowitz" <gl*******@comcast.net> wrote in message
news:p4********************@comcast.com...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:%X******************@newsread1.news.pas.earth link.net...

<snip>
I am a proponent of students initially *not* using an IDE,
as it hides what's really going on and promotes laziness.
I would only introduce an IDE when they're ready to see
it as a productivity tool, rather than a vehicle to hide
details.


Not sure if I agree or not. Initially it sounds good. But I think you
could replace "an IDE" with "the standard library" in that argument,
whereupon I would certainly disagree. But I haven't really convinced
myself that the analogy is valid.

GJD
Jul 22 '05 #42

This discussion thread is closed

Replies have been disabled for this discussion.