By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,168 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,168 IT Pros & Developers. It's quick & easy.

Strings C++

P: n/a
Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}

I'm wating for help.
Thanks.

Nov 5 '06 #1
Share this Question
Share on Google+
29 Replies


P: n/a
MHL

lu**@flypublicidade.com.br wrote:
Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}

I'm wating for help.
Thanks.
you either use strcmp or create a == operator. you can't compare string
to string directly.

hope this will help.

Nov 5 '06 #2

P: n/a
lu**@flypublicidade.com.br wrote:
Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}

I'm wating for help.
Thanks.

you either use strcmp or create a == operator. you can't compare string
to string directly.

hope this will help.
I've used strcmp, but for example if i compare "love" and "ovel" it
will return true.
And its is a problem, cause it isn't true.
Thanks.

Nov 6 '06 #3

P: n/a
lu**@flypublicidade.com.br wrote:
Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
That isn't a C++ string.
if (string == "test") {
printf("OK");
}
Use C++ strings.

std::string whatever;

if( whatever == "test ) ...

--
Ian Collins.
Nov 6 '06 #4

P: n/a
"lu**@flypublicidade.com.br" <lu**@flypublicidade.com.brwrites:
I've used strcmp, but for example if i compare "love" and "ovel" it
will return true.
No, it won't: 'l' != 'o', so it will return `false', that is != 0
(strcmp returns 0 only if both strings are equal, maybe that's your mistake?)
And its is a problem, cause it isn't true.
Thanks.
--
Simias
Nov 6 '06 #5

P: n/a
Hi

lu**@flypublicidade.com.br wrote:
I've used strcmp, but for example if i compare "love" and "ovel" it
will return true.
And its is a problem, cause it isn't true.
$ man strcmp
[...]
The strcmp() function compares the two strings s1 and s2. It returns
an integer less than, equal to, or greater than zero if s1 is found,
respectively, to be less than, to match, or be greater than s2.

You should first know how things work before you try to use them.

Markus

Nov 6 '06 #6

P: n/a
On 5 Nov 2006 16:11:22 -0800 in comp.lang.c++,
"lu**@flypublicidade.com.br" <lu**@flypublicidade.com.brwrote,
>Hello. I have a problem with strings in C++.
Use the std::string class found in #include <string>
See any decent C++ textbook for details.
Avoid using naked char arrays as a string-substitute.

Nov 6 '06 #7

P: n/a
<lu**@flypublicidade.com.brwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}

I'm wating for help.
Thanks.
If you use a c++ std::string it'll do what you want.

std::string MyString = "test";
if ( MyString == "test" ) {
std::cout << "OK" << std::endl;
}

If you use C style strings (char array) you gotta use c-string functions.

char MyString[10];
strcpy( MyString, "test" );
if ( strcmp( MyString, "test" ) == 0 ) {
std::cout << "OK" << std:;endl;
}

std::string is much prefered.
Nov 6 '06 #8

P: n/a
lu**@flypublicidade.com.br wrote:
Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}
as other folks have mentioned, you are using C in C++. it is not a
standard C++ programme. try this one using C++ strings:

// C++ strings

#include <iostream>
#include <string>

int main() {
std::string s;
std::cout << "guess the word: ";
std::cin >s;

if(s == "test")
std::cout << "You guessed :-)\n";
else
std::cout << "NOPE, you are wrong.\n";
}

you can customise it to your requirements. i just gave you an idea.
I'm wating for help.
hey, a newbie helped you ;-)
Thanks.
not a problem ;-)
-- arnuld
http://arnuld.blogspot.com

Nov 6 '06 #9

P: n/a
MHL wrote:
lu**@flypublicidade.com.br wrote:
>Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}

I'm wating for help.
Thanks.

you either use strcmp or create a == operator. you can't compare string
to string directly.

hope this will help.
You can not create a == operator for arrays or pointers, only
user defined types (classes and enums). The true answer is that
the C++ string class is called string.
Nov 6 '06 #10

P: n/a
lu**@flypublicidade.com.br wrote:
>
I've used strcmp, but for example if i compare "love" and "ovel" it
will return true.
And its is a problem, cause it isn't true.
Thanks.
strcmp doesn't return a boolean. It returns zero if
the strings are equal, or a positive or negative value
if they are not.

Nov 6 '06 #11

P: n/a
Simias wrote:
"lu**@flypublicidade.com.br" <lu**@flypublicidade.com.brwrites:
>I've used strcmp, but for example if i compare "love" and "ovel" it
will return true.

No, it won't: 'l' != 'o', so it will return `false', that is != 0
(strcmp returns 0 only if both strings are equal, maybe that's your mistake?)
>>
strcmp returns zero if the strings are equal. The return value is int
not bool.
Nov 6 '06 #12

P: n/a
Here is my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define false 0;
#define true 1;

typedef int BOOL;

//interpret comand
BOOL Comando(char *cmd) {
int i = 0;
char interp[8];

while ((cmd[i] != ' ') && (cmd[i] != '\0')) {
interp[i] = toupper(cmd[i]);
i++;
}

interp[i] = '\0';

if ((strcmp(interp,"JOGO") == 0)) {
return true;
}
else {
return false;
}

}

int main() {
char comand[50];
printf("OK.\n\r");
fgets(comand,50,stdin);
if (Comando(comand)) {
printf("It's a valid comand.\n\r");
}
else {
printf("It ins't a valid comand.\n\r");
}
return 0;
}

What's wrong?
Thanks.

Nov 6 '06 #13

P: n/a

lu**@flypublicidade.com.br wrote:
Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}

I'm wating for help.
Thanks.
You could try:

char s[10];
....
if (std::string(s) == "test") {
std::cout << "OK" << std::endl;
}

Nov 6 '06 #14

P: n/a
lu**@flypublicidade.com.br wrote:
Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}

I'm wating for help.
Thanks.

You could try:

char s[10];
...
if (std::string(s) == "test") {
std::cout << "OK" << std::endl;
}
when i put:
std::string -this generate a error =(

Nov 6 '06 #15

P: n/a
On 5 Nov 2006 20:36:56 -0800, "arnuld" <ar*****@gmail.comwrote:
>lu**@flypublicidade.com.br wrote:
>char string[10];
if (string == "test") {
printf("OK");
}

as other folks have mentioned, you are using C in C++. it is not a
standard C++ programme.
What's not standard C++ in the above code? It doesn't print "OK" but
that's another question ...

Best wishes,
Roland Pibinger
Nov 6 '06 #16

P: n/a
"lu**@flypublicidade.com.br" <lu**@flypublicidade.com.brwrote in
news:11********************@i42g2000cwa.googlegrou ps.com:
Here is my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define false 0;
#define true 1;

typedef int BOOL;
Why are you redefining the bool type? C++ has one. Use it.
>
//interpret comand
BOOL Comando(char *cmd) {
int i = 0;
char interp[8];

while ((cmd[i] != ' ') && (cmd[i] != '\0')) {
interp[i] = toupper(cmd[i]);
i++;
}

interp[i] = '\0';

if ((strcmp(interp,"JOGO") == 0)) {
return true;
}
else {
return false;
}

}

int main() {
char comand[50];
printf("OK.\n\r");
fgets(comand,50,stdin);
if (Comando(comand)) {
printf("It's a valid comand.\n\r");
}
else {
printf("It ins't a valid comand.\n\r");
}
return 0;
}

What's wrong?
Doesn't fgets also capture the newline character? Try displaying the
length of the input string in the Commando function. Also, if I entered
a 50 character command in main, you'll get Undefined Behaviour in the
Commando function. Your while loop won't stop at the end of the 8
character array that you've defined in Commando.
Nov 6 '06 #17

P: n/a


On Nov 6, 2:11 pm, "l...@flypublicidade.com.br"
<l...@flypublicidade.com.brwrote:
Here is my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define false 0;
#define true 1;

typedef int BOOL;

//interpret comand
BOOL Comando(char *cmd) {
int i = 0;
char interp[8];

while ((cmd[i] != ' ') && (cmd[i] != '\0')) {
interp[i] = toupper(cmd[i]);
i++;
}

interp[i] = '\0';

if ((strcmp(interp,"JOGO") == 0)) {
return true;
}
else {
return false;
}

}int main() {
char comand[50];
printf("OK.\n\r");
fgets(comand,50,stdin);
if (Comando(comand)) {
printf("It's a valid comand.\n\r");
}
else {
printf("It ins't a valid comand.\n\r");
}
return 0;

}What's wrong?
You're in the wrong newsgroup. Try comp.lang.c

Nov 6 '06 #18

P: n/a
"lu**@flypublicidade.com.br" <lu**@flypublicidade.com.brwrote in
news:11**********************@k70g2000cwa.googlegr oups.com:
>lu**@flypublicidade.com.br wrote:
Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}

I'm wating for help.
Thanks.

You could try:

char s[10];
...
if (std::string(s) == "test") {
std::cout << "OK" << std::endl;
}

when i put:
std::string -this generate a error =(
We're not mind-readers. What's the error?
Nov 6 '06 #19

P: n/a


On Nov 6, 2:30 pm, Andre Kostur <nntps...@kostur.netwrote:
"l...@flypublicidade.com.br" <l...@flypublicidade.com.brwrote innews:11********************@i42g2000cwa.googlegr oups.com:
Here is my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define false 0;
#define true 1;
typedef int BOOL;Why are you redefining the bool type? C++ has one. Use it.
What is worse is that he has redefined two C++ reserved words with
pre-processors.

Nov 6 '06 #20

P: n/a
VJ
lu**@flypublicidade.com.br wrote:
Hello. I have a problem with strings in C++.
I'm using gcc/g++ gnu/linux compiler.
How i can do tests with strings and constants in C++?

Eg:
char string[10];
if (string == "test") {
printf("OK");
}

I'm wating for help.
Thanks.
Why not use std::string ? Look at this code how I compared strings:
#include <string>
#include <iostream>
int main()
{
std::string a("aaa");
std::string b("aba");
char str[10]="aba";

if(a==b)
{
std::cout<<"11111111111111"<<std::endl;
}
if(std::string(str)==b)
{
std::cout<<"2222222222222"<<std::endl;
}
}
In the 2nd if, I converted char[] to string and compared to string b
Nov 6 '06 #21

P: n/a
Roland Pibinger <rp*****@yahoo.comwrote:
On 5 Nov 2006 20:36:56 -0800, "arnuld" <ar*****@gmail.comwrote:
>>lu**@flypublicidade.com.br wrote:
>>char string[10];
if (string == "test") {
printf("OK");
}

as other folks have mentioned, you are using C in C++. it is not a
standard C++ programme.

What's not standard C++ in the above code? It doesn't print "OK" but
that's another question ...
Well, in the other post where he posted the entire code, he has such
things as:

#define false 0
#define true 1

Redefining keywords is not allowed. Granted, this wasn't in the
original post, nor in the quoted bits above.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 6 '06 #22

P: n/a
Marcus Kwok:
#define false 0
#define true 1

Redefining keywords is not allowed.

I don't see why not... the compiler proper won't ever know what was written
before the preprocessor had its way.

--

Frederick Gotham
Nov 6 '06 #23

P: n/a

Frederick Gotham wrote:
Marcus Kwok:
#define false 0
#define true 1

Redefining keywords is not allowed.


I don't see why not... the compiler proper won't ever know what was written
before the preprocessor had its way.
I was sure that redefining keywords was explicitly forbidden in the
standard, but I can't find anything that says so. Maybe someone knows
if it says that...

Gavin Deane

Nov 6 '06 #24

P: n/a
Gavin Deane wrote:
>
Frederick Gotham wrote:
>Marcus Kwok:
#define false 0
#define true 1

Redefining keywords is not allowed.


I don't see why not... the compiler proper won't ever know what was
written before the preprocessor had its way.

I was sure that redefining keywords was explicitly forbidden in the
standard, but I can't find anything that says so. Maybe someone knows
if it says that...
The closest I found is [17.4.3.1.1/2]:

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to keywords.

Now, this applies to virtually every program I write.
Best

Kai-Uwe Bux
Nov 7 '06 #25

P: n/a
Kai-Uwe Bux <jk********@gmx.netwrote:
The closest I found is [17.4.3.1.1/2]:

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to keywords.

Now, this applies to virtually every program I write.
So, is that saying that if you don't include any headers, then
redefining keywords is fine?

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 7 '06 #26

P: n/a
Marcus Kwok wrote:
Kai-Uwe Bux <jk********@gmx.netwrote:
>The closest I found is [17.4.3.1.1/2]:

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to
keywords.

Now, this applies to virtually every program I write.

So, is that saying that if you don't include any headers, then
redefining keywords is fine?
The wording itself leaves open the question about translation units that do
not include headers. However, I have not found any provision in the
standard that would say you cannot redefine keywords in such translation
units. So, as far as I can tell, redefining keywords is well-defined in
that particular case. Now, I may have missed something -- the standard is
huge.
Best

Kai-Uwe Bux
Nov 7 '06 #27

P: n/a
Kai-Uwe Bux <jk********@gmx.netwrote:
Marcus Kwok wrote:
>Kai-Uwe Bux <jk********@gmx.netwrote:
>>The closest I found is [17.4.3.1.1/2]:

A translation unit that includes a header shall not contain any macros
that define names declared or defined in that header. Nor shall such a
translation unit define macros for names lexically identical to
keywords.

So, is that saying that if you don't include any headers, then
redefining keywords is fine?

The wording itself leaves open the question about translation units that do
not include headers. However, I have not found any provision in the
standard that would say you cannot redefine keywords in such translation
units. So, as far as I can tell, redefining keywords is well-defined in
that particular case.
Based on what you wrote, I am inclined to agree, though it is surprising
to me. Of course, I think it would be extremely difficult to write any
non-trivial program without using any headers at all.

I think that the rationale for the above allows for the situation in
which you manually implemented every particular feature you use, then it
would be possible to redefine the keywords to create some sort of
pseudo-language and be fully conforming. However, when including a
header, the original header implementer would probably assume standard
language semantics and thus redefining keywords would cause undefined
behavior because of the unforeseen changes in semantics.
Now, I may have missed something -- the standard is
huge.
Very true.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 7 '06 #28

P: n/a


On Nov 6, 12:42 am, Markus Moll <m...@rbg.informatik.tu-darmstadt.de>
wrote:
>
You should first know how things work before you try to use them.
Perhaps it's what you meant but you should know what they do, not
necessarily how they do it.

Nov 7 '06 #29

P: n/a
On 6 Nov 2006 06:11:06 -0800, I waved a wand and this message magically
appears in front of lu**@flypublicidade.com.br:
#define false 0;
#define true 1;

typedef int BOOL;
You should be taken out and shot for that...
--
http://www.munted.org.uk

You've been eating the cat food again, haven't you?
Nov 9 '06 #30

This discussion thread is closed

Replies have been disabled for this discussion.