473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Strings C++

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
29 1706
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
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
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
"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
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
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
<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
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
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
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
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
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

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
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
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
"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


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
"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


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

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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.