Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Syntax Killing Me - Using Char Array For Strings

Reply
Thread Tools

C++ Syntax Killing Me - Using Char Array For Strings

 
 
Phlip
Guest
Posts: n/a
 
      03-09-2007
Superman859 wrote:

>I agree - I would much rather be using strings than char arrays.
> Unfortunately, the Professor specifically said we must use char
> arrays. This is why I ask about it.


Some professors think you must learn C style programming to then learn C++.
Often that's how they did it. Just remember that high-level C++ can look
like it's very far from the metal, and that's where you should do most of
your work. And treat such professors as just coaches making you do mental
push-ups.

> As for reversing the original string - here is the problem.


Forget this originalString thing and learn std::string. Get more C++
tutorials than just your class's textbook, too!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-09-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello everyone. Heads up - c++ syntax is killing me. I do quite well
> in creating a Java program with very few syntax errors, but I get them
> all over the place in c++. The smallest little things get me, which
> brings me to...
>
> I'm trying to create a program that gets a string from standard input
> and then manipulates it a little bit. It has to be a char array and
> not use string from the library.
>
> Here are my prototypes:
>
> //Header file MidTerm.h
>
> #ifndef MIDTERM_H
> #define MIDTERM_H
>
> class MidTerm {
> public:
> void myappend(char [] , char []);
> void mytokenizer(char []);
> void myreverse(char [] );
> void getString();


char[] is wrong. It's not illegal but it's confusing (put there for
newbies I think, this was a mistake). In C++ you cannot pass arrays as
parameter, so don't pretend you can, especially if you are use to
programming Java.

In C++ it's pointers

void myappend(const char*);
void mytokenizer(char*);
void myreverse(char*);
void getString();



> private:
> char originalString[80];
> char reversedString[80];


Remove reversedString.

Allthough you know Java, you seem to think basic OO design doesn't apply
to C++? The method you've written above operate on originalString, you
don't need to pass originalString as a parameter. This is your biggest
mistake I think, not arrays and pointers.

> };
>
> #endif
>
> First off, how do I get access to originalString in main()? I always
> get an undeclared identifier error. Is this because I didn't
> initialize it?


No. Access and initialisation are unrelated topic (true for Java as well).

In your class add

const char* getString() const { return originalString; }

The thing is, I'm supposed to take input from the user
> for the file - I don't know how to initilize it if that is the case.


In your class add

void setString(const char* s) { strcpy(originalString, s); }

Of course this code illustrates why you should be using std::string
instead if C strings, and why your professor is wrong.

There is no guarantee that the passed in string is not longer than the
room available in originalString. It's because of code like this that we
have to update our copies of Windows (TM) every month.


> I also wasn't sure how large of an array to make in the declaration.
>>From what I understand, you must make a size, but how do you know the

> size when any given sentence can be typed in? I picked 80, a number
> large enough for most sentences...


Exactly, to code this properly, you have to use a pointer, and
dynamically resize the array to copy with the size of the sentence. If
you do that then you end up with something that looks very much like
std::string!

You really need to clarify with your professor whether you need t do
this or whether you can just pick a maximum length like 80.


john
 
Reply With Quote
 
 
 
 
Superman859@gmail.com
Guest
Posts: n/a
 
      03-09-2007
Thanks for these last few responses - they've been pretty helpful.

You all are right. I think I'm forgetting the big picture of OOP
because I am getting so tied up with the language of C++. I forget
things like the public functions have access to private variables. I
just realized today that you cannot pass an array as a parameter or
return one. We just briefly started discussing pointers in class. I
feel the major problem with this professor is we don't do any 'live'
or real work. We've not once touched the computer as part of the
course during class. He talks about theory, what goes on with memory,
etc. but it's our first c++ class - we need more hands on type stuff
than that. This causes me to get caught up on syntax and details of c+
+, forgetting everything else I know.

Hopefully soon we will be allowed to use std::string.

Until then, I think I'm going to take learning the language into my
own hands - I feel like I'm not learning anything as part of the
course (I struggle with a simple program like this, yet I've made 100s
on all Homework assignments so far - that shouldn't happen). I'm
going to study it in more detail on my own.

 
Reply With Quote
 
Dennis \(Icarus\)
Guest
Posts: n/a
 
      03-10-2007
"John Harrison" <(E-Mail Removed)> wrote in message
news:Ro7Ih.21532$(E-Mail Removed)...
> (E-Mail Removed) wrote:
> > Hello everyone. Heads up - c++ syntax is killing me. I do quite well
> > in creating a Java program with very few syntax errors, but I get them
> > all over the place in c++. The smallest little things get me, which
> > brings me to...
> >
> > I'm trying to create a program that gets a string from standard input
> > and then manipulates it a little bit. It has to be a char array and
> > not use string from the library.
> >
> > Here are my prototypes:
> >
> > //Header file MidTerm.h
> >
> > #ifndef MIDTERM_H
> > #define MIDTERM_H
> >
> > class MidTerm {
> > public:
> > void myappend(char [] , char []);
> > void mytokenizer(char []);
> > void myreverse(char [] );
> > void getString();

>
> char[] is wrong. It's not illegal but it's confusing (put there for
> newbies I think, this was a mistake). In C++ you cannot pass arrays as


It was, IIRC, designed to make it clear that the parameter points to
multiple items (so expect a size parameter also somewhere) as opposed to a
single item. Doesn''t seem to be used that way very often anymore though,
and can cause confusion.

http://cm.bell-labs.com/cm/cs/who/dmr/chist.html
The notation survived in part for the sake of compatibility, in part under
the rationalization that it would allow programmers to communicate to their
readers an intent to pass f a pointer generated from an array, rather than a
reference to a single integer. Unfortunately, it serves as much to confuse
the learner as to alert the reader.

> parameter, so don't pretend you can, especially if you are use to
> programming Java.
>

<snip of useful information>

Dennis


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 6 09-01-2006 05:57 PM
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments