C ++语法杀死我 - 使用字符串的字符串数组 [英] C++ Syntax Killing Me - Using Char Array For Strings

查看:68
本文介绍了C ++语法杀死我 - 使用字符串的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。抬头 - c ++语法让我筋疲力尽。我在创建一个语法错误很少的Java程序时做得很好

但是我在c ++中得到了它们

。最小的东西让我,这是
带我到...


我正在尝试创建一个从标准输入中获取字符串的程序

然后稍微操纵一下。它必须是一个char数组,并且

不使用库中的字符串。


这是我的原型:


//头文件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,如何在main()中访问originalString?我总是

获得未声明的标识符错误。这是因为我没有将b $ b初始化吗?问题是,我应该从用户输入

来获取文件 - 如果是这样的话,我不知道如何启动它。

我也不确定声明中有多大的数组。

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



可以输入任何给定句子的大小?我选了80,一个数字

足够大的句子...


这是我的主...


#include< iostream>

使用命名空间std;


#include" MidTerm.h"


int main(){


MidTerm test1;

test1.getString();

//test1.mytokenizer (我肯定传递原始字符串);

test1.myreverse(originalString);

返回0;

}


到目前为止这很简单,但是没有用。如果我实际输入字符串而不是使用变量作为

参数,则myreverse可以工作。它会反转所有字母。但是,我需要传递

char数组 - 而不是静态文本。


我只是在这个char数组中遇到很多语法问题

字符串。当字符串将由

用户输入时,如何初始化它?如何将其作为变量正确传递给函数?怎么做

我知道声明char数组时要使用的大小?我如何获得

它在main()中工作?


任何帮助都将非常感激。我轰炸了这个中期因为

它都处理了char数组,而且我已经习惯在Java中使用String

- 更简单。


这是函数...


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]!=''\'''){

cout<< reversedString [i];

i ++;

}

}

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++;
}
}

推荐答案

Su*********@gmail.com 写道:

大家好。抬头 - c ++语法让我筋疲力尽。我在创建一个语法错误很少的Java程序时做得很好

但是我在c ++中得到了它们

。最小的东西让我,这是
带我到...


我正在尝试创建一个从标准输入中获取字符串的程序

然后稍微操纵一下。它必须是一个char数组并且

不使用库中的字符串。
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.



为什么?在Java中你会使用一个字符串对象,在C ++中也是如此。

Why? In Java you would use a string object, same in C++.


这是我的原型:


//头文件MidTerm.h


#ifndef MIDTERM_H

#define MIDTERM_H


class MidTerm {

public:

void myappend(char [],char []);

void mytokenizer(char []);

void myreverse (char []);
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 [] );



为什么所有这些都有参数,它们不能用于你的

originalString成员吗?

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


首先,我如何获得对originalString的访问权限主要()?
void getString();
private:
char originalString[80];
char reversedString[80];
};

#endif

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



提供一种方法来访问它。


-

Ian Collins。

Provide a method to access it.

--
Ian Collins.


是的,我知道C ++有一个字符串对象,就像Java一样。但是,

我需要使用char数组,而不是字符串函数。字符串

是由C ++用户创建的,不是直接构建到语言中的。


接下来,我有参数因为我不想改变

originalString。一旦我开始对它进行标记化,原始字符串将在完成时没有它的原始值。我需要它在每个函数的末尾都有它的
originalValue。我想将它作为

参数传递给我,这样我就可以简单地处理它的新副本(传递 -

值)而不是原始版本。 />

最后,我确实尝试创建一个访问originalString的方法。它b / b
没用。这是getString()函数。我最初尝试过


char [] getString(){

返回originalString;

}

原型也读取char []而不是无效。


然而,它也没有工作。 getString()在

程序中被注释掉,并且从未实际使用,因为它给出了错误。

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.


3月9日,1 :35 pm,超人... @ gmail.com写道:
On Mar 9, 1:35 pm, Superman...@gmail.com wrote:

我正在尝试创建一个程序,从标准中获取字符串

输入然后稍微操纵它。它必须是一个char

数组,而不是使用库中的字符串。
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];

};


首先,如何在main()中访问originalString?

我总是得到一个未声明的标识符错误。
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.



好​​吧,originalString是私有的。这意味着它只能在MidTerm对象的
成员函数中可见。它对main()是不可见的。

Well, originalString is private. That means it is only visible within
member functions of MidTerm objects. It isn''t visible to main().


我也不确定声明中有多大的数组。

根据我的理解,你必须制作一个大小,但是当你输入任何给定的句子时,你怎么知道

大小?我选了80,一个数字

大到足以支持大多数句子...
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.


这是我的主要内容......


#include< iostream>

使用命名空间std;


#include" MidTerm.h"


int main(){

MidTerm test1;

test1.getString();

//test1.mytokenizer (我肯定传递原始字符串);

test1.myreverse(originalString);
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);



翻转

用户输入的字符串是不是更有意义?该函数不应该使用任何参数,而是使用
处理MidTerm对象中的''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.


返回0;

}


我只是在这个char数组中遇到很多语法问题,因为

string。当字符串将由

用户输入时,如何初始化它?如何将其作为变量正确传递给函数?怎么做

我知道声明char数组时要使用的大小?如何才能在main()中使用


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



你应该得到一本C ++书,或者甚至找一些互联网教程。

阅读C ++ Faq Lite也可以提供帮助。 />

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


这是函数...

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]!=''\'''){

cout<< reversedString [i];

i ++;

}


}
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++;
}

}



如果这个函数作为MidTerm

类的成员,如果它不对该类的任何数据成员运行,为什么还要这么做?

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


这篇关于C ++语法杀死我 - 使用字符串的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆