Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > string comparisons

Reply
Thread Tools

string comparisons

 
 
Horn
Guest
Posts: n/a
 
      10-03-2003
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 http://www.velocityreviews.com/forums/(E-Mail Removed)

//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
}
 
Reply With Quote
 
 
 
 
WW
Guest
Posts: n/a
 
      10-03-2003
Horn wrote:
> 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 (E-Mail Removed)



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

> //Britton Horn
>
> #include "stdafx.h"


Non-standard header

> #include <iostream.h>


Non-standard header

> #include <stdlib.h>
> #ifndef _OOSTRING_H
> #define _OOSTRING_H


WHY on Earth is this in a header???

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


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

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


WHY on Earth is this (the main function) in a header???

> }


--
WW aka Attila


 
Reply With Quote
 
 
 
 
Aggro
Guest
Posts: n/a
 
      10-03-2003
Horn wrote:


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


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

 
Reply With Quote
 
WW
Guest
Posts: n/a
 
      10-03-2003
Aggro wrote:
> 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:


Fish, or fishing?

--
WW aka Attila


 
Reply With Quote
 
Kris Wempa
Guest
Posts: n/a
 
      10-03-2003

"Horn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> int main()
> {
> char yourname[25];
> char myname[25] = "britton";
> int answer;
>
> cout << "What is your name?\n";
> cin.get(yourname, 25);
>
> if (yourname == myname)


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)

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


Is this part working correctly ?


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      10-03-2003
Horn wrote:

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


I won't, and it's rude to ask. Post here, read here.

>
> //Britton Horn
>
> #include "stdafx.h"


Incomplete code makes it difficult to diagnose problems. You haven't
given us the contents of this header file.

> #include <iostream.h>


This is not a standard C++ header. Standard C++ uses <iostream>.

> #include <stdlib.h>


This header is deprecated in favor of <cstdlib>.

> #ifndef _OOSTRING_H
> #define _OOSTRING_H


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.

> #include <string.h>


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.

 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      10-03-2003
> I am having trouble comparing strings.


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

Look at the end of the post.


Jonathan


 
Reply With Quote
 
Ashish
Guest
Posts: n/a
 
      10-06-2003

"Horn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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 (E-Mail Removed)
>
> //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)


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)


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



 
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
Fast Case Insensitive String Comparisons Merk ASP .Net 12 12-13-2006 07:37 AM
XPath String Value Comparisons Nishad XML 2 09-30-2006 02:56 PM
the lowest number of comparisons in string matching laura C Programming 2 09-22-2006 03:35 PM
cPAMIE/Python String and Date Comparisons Sam R Python 1 11-09-2005 12:59 AM
XPath's with String comparisons gfrommer@hotmail.com Java 1 06-27-2005 10:59 PM



Advertisments