Strings C++

L

luan

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

MHL

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

luan

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

Ian Collins

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

Simias

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

Markus Moll

Hi

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
 
D

David Harmon

On 5 Nov 2006 16:11:22 -0800 in comp.lang.c++,
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.
 
J

Jim Langston

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

arnuld

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 ;-)

not a problem ;-)


-- arnuld
http://arnuld.blogspot.com
 
R

Ron Natalie

MHL said:
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.
 
R

Ron Natalie

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

Ron Natalie

Simias said:
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.
 
L

luan

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 != ' ') && (cmd != '\0')) {
interp = toupper(cmd);
i++;
}

interp = '\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.
 
P

pzehnder

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

luan

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 =(
 
A

Andre Kostur

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 != ' ') && (cmd != '\0')) {
interp = toupper(cmd);
i++;
}

interp = '\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.
 
A

Andre Kostur

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

Earl Purple

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 != ' ') && (cmd != '\0')) {
interp = toupper(cmd);
i++;
}

interp = '\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
 
E

Earl Purple

What is worse is that he has redefined two C++ reserved words with
pre-processors.
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top