I need some basic C++ help

M

Mike Wahler

kazack said:
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
 
G

Gavin Deane

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
 

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,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top