I need some basic C++ help

J

jeffc

Gary Labowitz said:
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.
 
J

jeffc

Andrew Koenig said:
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?
 
L

lilburne

Andrew said:
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.
 
G

Gary Labowitz

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.
 
G

Gary Labowitz

jeffc said:
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.
 
K

kazack

jbruno4000 said:
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
 
S

Severin Ecker

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
 
J

Jupiter5F

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.
 
G

Gavin Deane

Gary Labowitz said:
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.
 
J

Jon Bell

jbruno4000 said:
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()
 
J

jeffc

Gary Labowitz said:
if

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.
 
J

jeffc

Gavin Deane said:
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.....
 
G

Gary Labowitz

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?
 
A

Andy

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 said:
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?
 
M

Mike Wahler

kazack said:
jbruno4000 said:
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
 
M

Mike Wahler

jbruno4000 said:
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
 
M

Mike Wahler

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
 
G

Gary Labowitz

Mike Wahler said:
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 ...
 
K

kazack

Mike Wahler said:
kazack said:
jbruno4000 said:
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?
 
M

Mike Wahler

Gary Labowitz said:
#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). :)
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top