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

 
 
Superman859@gmail.com
Guest
Posts: n/a
 
      03-09-2007
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[i] = os[j];
i++;
j--;
}

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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-09-2007
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.
>

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.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Superman859@gmail.com
Guest
Posts: n/a
 
      03-09-2007
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.

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      03-09-2007
On Mar 9, 1:35 pm, Superman...@gmail.com wrote:
> 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[i] = os[j];
> i++;
> j--;
> }
>
> i = 0;
> while (reversedString[i] != '\0') {
> cout << reversedString[i];
> 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?

 
Reply With Quote
 
Sarath
Guest
Posts: n/a
 
      03-09-2007
On Mar 9, 9:35 am, Superman...@gmail.com 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();
> 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[i] = os[j];
> i++;
> j--;
> }
>
> i = 0;
> while (reversedString[i] != '\0') {
> cout << reversedString[i];
> 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];

};

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-09-2007
wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
Superman859@gmail.com
Guest
Posts: n/a
 
      03-09-2007
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.

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-09-2007
wrote:

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

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


 
Reply With Quote
 
Superman859@gmail.com
Guest
Posts: n/a
 
      03-09-2007
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?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-09-2007
wrote:
> 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?

--
Ian Collins.
 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57