Connecting Tech Pros Worldwide Forums | Help | Site Map

string comparisons

Horn
Guest
 
Posts: n/a
#1: Jul 19 '05
I am having trouble comparing strings. When I run the program and I
type in what I think is the right string, it goes to the else part of
the program. This is for my computer science class in high school so
excuse me if you think it's really easy. If you know what I should do
then please send me a message at hornstar12@email.com

//Britton Horn

#include "stdafx.h"
#include <iostream.h>
#include <stdlib.h>
#ifndef _OOSTRING_H
#define _OOSTRING_H
#include <string.h>

int main()
{
char yourname[25];
char myname[25] = "britton";
int answer;

cout << "What is your name?\n";
cin.get(yourname, 25);

if (yourname == myname)
{
cout << "What is 2 + 2?\n";
cin >> answer;
cout << "Correct!\n";
}
else
{
cout << "What is 2 + 2?\n";
cin >> answer;
if (answer == 4)
{
cout << "I'm sorry. The answer is 22.\n";
}
if (answer != 4)
{
cout << "I'm sorry. The answer is 4.\n";
}
}
return 0;
#endif
}

WW
Guest
 
Posts: n/a
#2: Jul 19 '05

re: string comparisons


Horn wrote:[color=blue]
> I am having trouble comparing strings. When I run the program and I
> type in what I think is the right string, it goes to the else part of
> the program. This is for my computer science class in high school so
> excuse me if you think it's really easy. If you know what I should do
> then please send me a message at hornstar12@email.com[/color]


Posted AND mailed. LAST TIME! Post here and read here. Answers to my
mailbox will be deleted unread:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.4
[color=blue]
> //Britton Horn
>
> #include "stdafx.h"[/color]

Non-standard header
[color=blue]
> #include <iostream.h>[/color]

Non-standard header
[color=blue]
> #include <stdlib.h>
> #ifndef _OOSTRING_H
> #define _OOSTRING_H[/color]

WHY on Earth is this in a header???
[color=blue]
> #include <string.h>
>
> int main()
> {
> char yourname[25];
> char myname[25] = "britton";
> int answer;
>
> cout << "What is your name?\n";
> cin.get(yourname, 25);
>
> if (yourname == myname)[/color]

Learn about strcmp. Or better: learn about std::string.

http://www.dinkumware.com/manuals/re...ng.html#strcmp

http://www.dinkumware.com/manuals/re...h=string2.html

http://www.google.com/search?hl=en&l...torial+c%2B%2B

In short: http://tinyurl.com/pm6r
[color=blue]
> {
> cout << "What is 2 + 2?\n";
> cin >> answer;
> cout << "Correct!\n";
> }
> else
> {
> cout << "What is 2 + 2?\n";
> cin >> answer;
> if (answer == 4)
> {
> cout << "I'm sorry. The answer is 22.\n";
> }
> if (answer != 4)
> {
> cout << "I'm sorry. The answer is 4.\n";
> }
> }
> return 0;
> #endif[/color]

WHY on Earth is this (the main function) in a header???
[color=blue]
> }[/color]

--
WW aka Attila


Aggro
Guest
 
Posts: n/a
#3: Jul 19 '05

re: string comparisons


Horn wrote:

[color=blue]
> #include <string.h>
>
> int main()
> {
> char yourname[25];
> char myname[25] = "britton";
> int answer;
>
>
> if (yourname == myname)[/color]

Any particular reason why not using the std::string? If you must use
char arrays, you can't compare them like this. You need to use strcmp()
function, or write one by yourself. If you use std::string you can
compare strings like that. Have a look at these two examples:

-------------------------------
#include <string>
#include <iostream>

int main()
{
std::string a = "hello";
std::string b = "hello";

if( a == b )
{
std::cout << "Strings are equal." << std::endl;
}
else
{
std::cout << "Strings are not equal." << std::endl;
}

return 0;
}
--------------------------

------------------------------
#include <cstring>
#include <iostream>

int main()
{
char c[] = "hello";
char d[] = "hello";

if( strcmp( c,d ) == 0 )
{
std::cout << "Strings are equal." << std::endl;
}
else
{
std::cout << "Strings are not equal." << std::endl;
}

return 0;
}
--------------------------------

WW
Guest
 
Posts: n/a
#4: Jul 19 '05

re: string comparisons


Aggro wrote:[color=blue]
> Any particular reason why not using the std::string? If you must use
> char arrays, you can't compare them like this. You need to use
> strcmp() function, or write one by yourself. If you use std::string
> you can compare strings like that. Have a look at these two examples:[/color]

Fish, or fishing?

--
WW aka Attila


Kris Wempa
Guest
 
Posts: n/a
#5: Jul 19 '05

re: string comparisons



"Horn" <hornstar12@email.com> wrote in message
news:6e420ba5.0310030949.3c629499@posting.google.c om...[color=blue]
> int main()
> {
> char yourname[25];
> char myname[25] = "britton";
> int answer;
>
> cout << "What is your name?\n";
> cin.get(yourname, 25);
>
> if (yourname == myname)[/color]

You can't compare character strings like this. You may be able to do this
for C++ string data types, but not for traditional C character strings. You
need to do this:

if (strcmp(yourname,myname) == 0)
[color=blue]
> cout << "What is 2 + 2?\n";
> cin >> answer;
> if (answer == 4)
> {
> cout << "I'm sorry. The answer is 22.\n";
> }
> if (answer != 4)
> {
> cout << "I'm sorry. The answer is 4.\n";
> }
> }[/color]

Is this part working correctly ?


Kevin Goodsell
Guest
 
Posts: n/a
#6: Jul 19 '05

re: string comparisons


Horn wrote:
[color=blue]
> I am having trouble comparing strings. When I run the program and I
> type in what I think is the right string, it goes to the else part of
> the program. This is for my computer science class in high school so
> excuse me if you think it's really easy. If you know what I should do
> then please send me a message at hornstar12@email.com[/color]

I won't, and it's rude to ask. Post here, read here.
[color=blue]
>
> //Britton Horn
>
> #include "stdafx.h"[/color]

Incomplete code makes it difficult to diagnose problems. You haven't
given us the contents of this header file.
[color=blue]
> #include <iostream.h>[/color]

This is not a standard C++ header. Standard C++ uses <iostream>.
[color=blue]
> #include <stdlib.h>[/color]

This header is deprecated in favor of <cstdlib>.
[color=blue]
> #ifndef _OOSTRING_H
> #define _OOSTRING_H[/color]

This is illegal. You may not use identifiers beginning with an
underscore followed by either another underscore or an uppercase letter.
They are reserved for the implementation's use. Even if your compiler
fails to diagnose this (most won't), it is an error. A compiler may
reject the code, or may even accept it with a dramatically altered meaning.

The simplest rule of thumb is *never use an identifier that begins with
an underscore, or contains a sequence of two underscores.*

Besides that, "include guards" make no sense in a non-header file.
[color=blue]
> #include <string.h>[/color]

This header is deprecated in favor of <cstring>, but you should just use
<string> and the std::string class instead (as others have already said).
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jonathan Mcdougall
Guest
 
Posts: n/a
#7: Jul 19 '05

re: string comparisons


> I am having trouble comparing strings.


http://groups.google.ca/groups?hl=en....videotron.net

Look at the end of the post.


Jonathan


Ashish
Guest
 
Posts: n/a
#8: Jul 19 '05

re: string comparisons



"Horn" <hornstar12@email.com> wrote in message
news:6e420ba5.0310030949.3c629499@posting.google.c om...[color=blue]
> I am having trouble comparing strings. When I run the program and I
> type in what I think is the right string, it goes to the else part of
> the program. This is for my computer science class in high school so
> excuse me if you think it's really easy. If you know what I should do
> then please send me a message at hornstar12@email.com
>
> //Britton Horn
>
> #include "stdafx.h"
> #include <iostream.h>
> #include <stdlib.h>
> #ifndef _OOSTRING_H
> #define _OOSTRING_H
> #include <string.h>
>
> int main()
> {
> char yourname[25];
> char myname[25] = "britton";
> int answer;
>
> cout << "What is your name?\n";
> cin.get(yourname, 25);
>
> if (yourname == myname)[/color]

You cant compare string like this. This will compare the addresses pointed
to by 'yourname' and 'myname'. To compare string, use...

if(strcmp(yourname, myname) == 0)

[color=blue]
> {
> cout << "What is 2 + 2?\n";
> cin >> answer;
> cout << "Correct!\n";
> }
> else
> {
> cout << "What is 2 + 2?\n";
> cin >> answer;
> if (answer == 4)
> {
> cout << "I'm sorry. The answer is 22.\n";
> }
> if (answer != 4)
> {
> cout << "I'm sorry. The answer is 4.\n";
> }
> }
> return 0;
> #endif
> }[/color]


Closed Thread