Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Strings C++

Reply
Thread Tools

Strings C++

 
 
luan@flypublicidade.com.br
Guest
Posts: n/a
 
      11-06-2006
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.

 
Reply With Quote
 
 
 
 
MHL
Guest
Posts: n/a
 
      11-06-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
luan@flypublicidade.com.br
Guest
Posts: n/a
 
      11-06-2006
> (E-Mail Removed) 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.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-06-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
Simias
Guest
Posts: n/a
 
      11-06-2006
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> 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
 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      11-06-2006
Hi

(E-Mail Removed) 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

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      11-06-2006
On 5 Nov 2006 16:11:22 -0800 in comp.lang.c++,
"(E-Mail Removed)" <(E-Mail Removed)> wrote,
>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.

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      11-06-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      11-06-2006
(E-Mail Removed) 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

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      11-06-2006
MHL wrote:
> (E-Mail Removed) 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.
 
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
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments