473,789 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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(cha r []);
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.mytokeniz er("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::myreve rse(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++;
}
}

Mar 9 '07 #1
13 6250
Su*********@gma il.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.
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(cha r []);
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.
Mar 9 '07 #2
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.

Mar 9 '07 #3
On Mar 9, 1:35 pm, Superman...@gma il.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(cha r []);
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.mytokeniz er("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::myreve rse(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?

Mar 9 '07 #4
On Mar 9, 9:35 am, Superman...@gma il.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(cha r []);
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.>Fr om 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.mytokeniz er("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::myreve rse(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(con st char* org )
{
if( org )
strcpy(original String,org);
}
void myreverse(const char* rev)
{
if( rev )
strcpy(reversed String,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];

};

Mar 9 '07 #5
Su*********@gma il.com 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.
Mar 9 '07 #6
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.

Mar 9 '07 #7
Su*********@gma il.com 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!!!
Mar 9 '07 #8
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?

Mar 9 '07 #9
Su*********@gma il.com 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.
Mar 9 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3719
by: muser | last post by:
I'am having problem with the following: rec1len = strlen(temp1); temp1 is a character array, multi dimensional array. it has been initialised as char temp1; int rec1len. error for this message: C:\Program Files\Microsoft Visual Studio\MyProjects\Valid\Zenith124\Zenith.cpp(162) : error C2664: 'strlen' : cannot convert parameter 1 from 'char ' to 'const
5
7535
by: cppaddict | last post by:
Is it possible to avoid using push_back repeatedly when initializing a vector? That is, is there a vector syntax that would be analogous to the following array initialization syntax: MyClass myArray = { MyClass("Stan"), MyClass("Julie") }; Thanks for any ideas,
28
2711
by: Merrill & Michele | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *p; p=malloc(4); strcpy(p, "tja"); printf("%s\n", p); free(p); return 0;
11
3612
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
3
42420
by: huey_jiang | last post by:
Hi All, I am trying to figure out a right syntax to convert an integer array into hex array. sprintf worked for me on doing single integer: int i, Iarray, n=15; char buf; sprintf(buf, "0x%02x", n); The above code worked. Howeve, what I am trying to do is to convert an
12
5532
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the following program: #include<stdio.h> #define SIZE 1 int main()
7
2958
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that manipulate (and report on) members of the class. Interface consists of: - 5 private variables char author; char title; char code;
2
2517
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of the array is an object itself but what is this syntax of the consecutive double quotes inside the brackets ?
13
2238
by: arnuld | last post by:
this does not work, i know there is some problem in the "for loop" of "print_arr" function. i am not able to correct the weired results i am getting. i have no compile time error, it is only semantic-bug that is causing the trouble: EXPECTED: january, february, march....december GOT: january, january, january.........january ------------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 10
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.