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

S

Superman859

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();
private:
char originalString[80];
char reversedString[80];
};

#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? 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.
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...

Here is my main...

#include <iostream>
using namespace std;

#include "MidTerm.h"

int main() {

MidTerm test1;
test1.getString();
//test1.mytokenizer("I coundnt pass original string");
test1.myreverse(originalString);
return 0;
}

It's very simple so far, yet doesn't work. myreverse works if I
actually type in a string rather than use a variable as the
parameter. It reverses all the letters. However, I need to pass the
char array - not static text.

I'm just having a lot of syntax trouble with this char array as
string. How do I initialize it when the string will be input by
user? How do I pass it as a variable to a function correctly? How do
I know the size to use when declaring the char array? How can I get
it to work in the main()?

Any help would greatly be appreciated. I bombed this midterm because
it all dealt with char array, and I'm so used to using String in Java
- much simpler.

Here is that function...

void MidTerm::myreverse(char os[]) {

int i = 0;
int j = strlen(os) -1;

while (i <= strlen(os)) {
reversedString = os[j];
i++;
j--;
}

i = 0;
while (reversedString != '\0') {
cout << reversedString;
i++;
}
}
 
I

Ian Collins

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.
Why? In Java you would use a string object, same in C++.
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 [] );

Why do all of these have a parameter, don't they work on your
originalString member?
void getString();
private:
char originalString[80];
char reversedString[80];
};

#endif

First off, how do I get access to originalString in main()?

Provide a method to access it.
 
S

Superman859

Yes, I know that C++ has a string object just as Java does. However,
I'm required to use a char array, and not the string function. String
was created by a C++ user, not built directly into the language.

Next, I have the parameters because I do not want to alter
originalString. Once I begin to tokenize it, the originalString would
not have it's original value upon completion. I need it to have it's
originalValue at the end of every function. I wanted to pass it as a
parameter so that I could simply work on the new copy of it (pass-by-
value) and not the original.

Finally, I did try to create a method to access originalString. It
didn't work. It was the getString() function. I had originally tried

char[] getString() {
return originalString;
}

The prototype also read char[] and not void.

However, it wasn't working either. getString() is commented out in
the program and never actually used as it gave errors.
 
O

Old Wolf

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.

Says who? Talk about learning to run before learning to walk.
IMHO you would be better off getting it working using strings,
and then later on convert the program to use char arrays.
class MidTerm {
public:
void myappend(char [] , char []);
void mytokenizer(char []);
void myreverse(char [] );
void getString();
private:
char originalString[80];
char reversedString[80];
};

First off, how do I get access to originalString in main()?
I always get an undeclared identifier error.

Well, originalString is private. That means it is only visible within
member functions of MidTerm objects. It isn't visible to main().
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...

You have to have a loop where you allocate some memory, read some data
into it, then if there is still more data then re-allocate a larger
block, read in some more data, and so on.

Yet another reason you should be using strings instead of char arrays.
Here is my main...

#include <iostream>
using namespace std;

#include "MidTerm.h"

int main() {
MidTerm test1;
test1.getString();
//test1.mytokenizer("I coundnt pass original string");
test1.myreverse(originalString);

Wouldn't it make more sense to be reversing the string that the
user entered? The function should take no parameters, and instead
work on 'originalString' within the MidTerm object.
return 0;
}

I'm just having a lot of syntax trouble with this char array as
string. How do I initialize it when the string will be input by
user? How do I pass it as a variable to a function correctly? How do
I know the size to use when declaring the char array? How can I get
it to work in the main()?

You should get a C++ book, or even look for some Internet tutorials.
Reading the C++ Faq Lite could also help.
Here is that function...

void MidTerm::myreverse(char os[]) {

int i = 0;
int j = strlen(os) -1;

while (i <= strlen(os)) {
reversedString = os[j];
i++;
j--;
}

i = 0;
while (reversedString != '\0') {
cout << reversedString;
i++;
}

}


Why bother having this function as a member of the MidTerm
class if it does not operate on any data members of the class?
 
S

Sarath

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();
private:
char originalString[80];
char reversedString[80];

};

#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? 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.
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...

Here is my main...

#include <iostream>
using namespace std;

#include "MidTerm.h"

int main() {

MidTerm test1;
test1.getString();
//test1.mytokenizer("I coundnt pass original string");
test1.myreverse(originalString);
return 0;

}

It's very simple so far, yet doesn't work. myreverse works if I
actually type in a string rather than use a variable as the
parameter. It reverses all the letters. However, I need to pass the
char array - not static text.

I'm just having a lot of syntax trouble with this char array as
string. How do I initialize it when the string will be input by
user? How do I pass it as a variable to a function correctly? How do
I know the size to use when declaring the char array? How can I get
it to work in the main()?

Any help would greatly be appreciated. I bombed this midterm because
it all dealt with char array, and I'm so used to using String in Java
- much simpler.

Here is that function...

void MidTerm::myreverse(char os[]) {

int i = 0;
int j = strlen(os) -1;

while (i <= strlen(os)) {
reversedString = os[j];
i++;
j--;
}

i = 0;
while (reversedString != '\0') {
cout << reversedString;
i++;
}

}


Seems you are trying to do something like this


class MidTerm {
public:

void mytokenizer(const char* org )
{
if( org )
strcpy(originalString,org);
}
void myreverse(const char* rev)
{
if( rev )
strcpy(reversedString,rev);
// Or do the string reverse processing here
}
void myappend(const char* org , const char* rev)
{
mytokenizer(org);
myreverse(rev);
}
const char* getString() const
{
return originalString;
}
private:

char originalString[80];
char reversedString[80];

};
 
I

Ian Collins

Yes, I know that C++ has a string object just as Java does. However,
I'm required to use a char array, and not the string function. String
was created by a C++ user, not built directly into the language.
That's nonsense, std::string is part of the C++ standard library. It is
there for a reason!
Next, I have the parameters because I do not want to alter
originalString. Once I begin to tokenize it, the originalString would
not have it's original value upon completion. I need it to have it's
originalValue at the end of every function. I wanted to pass it as a
parameter so that I could simply work on the new copy of it (pass-by-
value) and not the original.
But originalString is a data member of the class, so you don't have to
pass it to member functions, they can just use it.
Finally, I did try to create a method to access originalString. It
didn't work. It was the getString() function. I had originally tried

char[] getString() {
return originalString;
}
Why not const char* getString() { return originalString; } ?
However, it wasn't working either. getString() is commented out in
the program and never actually used as it gave errors.
Define not working.
 
S

Superman859

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.

I had thought that the reason I couldn't access originalString was
because it was private. However, I tried moving it into public: but
it still gave the same error. Instead of passing originalString as a
parameter, I had also tried passing test.getString() as a parameter,
which I had set to return originalString. This only resulted in more
compilation errors. (Using Visual Studio .NET 2003 in the classroom,
although I've hardly used that one before. We never even touch a
computer during class, it's pretty sad. I have 2005 on my computer.
On a side note, I emailed a copy to myself and found out that strtok()
has been deprecated now? I didn't have any errors with it with the
2003 edition, and we were taught strtok. 2005 mentioned trying
strtok_s instead. Just another reason I hate working in the
classroom).

As for reversing the original string - here is the problem. I needed
the originalString to remain unchanged. I wanted to pass-by-value,
etc. because I was required to do multiple things on it. I needed to
reverse it, I needed to tokenize it. I needed to combine two
strings. I didn't want to change the original array itself because
that would result in any of the other functions working on the
incorrect string.

We were also required to include pass-by-value, pass-by-reference, as
well as use pointers. Unfortunately I never had much time to worry
about any of these, as time was very limited and I struggled so much
with the smallest things regarding char arrays.
 
P

Phlip

Yes, I know that C++ has a string object just as Java does. However,
I'm required to use a char array, and not the string function. String
was created by a C++ user, not built directly into the language.

So why would you use a language which _couldn't_ build a healthy string
class, or any similar thing, from its primitive keywords?
 
S

Superman859

Because it's what the professor requested. He wants us to understand
the foundations of the language from the ground up. Like I said, if
it were up to me then I would use the string class. It's not up to
me.

Does anyone have any advice on using a char array in this case other
than it'd be easier or make more sense to use the string class?
 
I

Ian Collins

Because it's what the professor requested. He wants us to understand
the foundations of the language from the ground up. Like I said, if
it were up to me then I would use the string class. It's not up to
me.
Please keep the context you are replying to.
Does anyone have any advice on using a char array in this case other
than it'd be easier or make more sense to use the string class?
You have received several useful bits of advice, have you followed them?
 
P

Phlip

Superman859 said:
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!
 
J

John Harrison

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

Superman859

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

Dennis \(Icarus\)

John Harrison said:
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
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top