943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7550
  • C++ RSS
Feb 12th, 2005
0

comparing strings

Expand Post »
I am trying to make a program that needs to compare a string. here is a basic program of what i mean, i want it to say "it worked" but it seems to only read as not true. anyone know how to do this? thanx for any help.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <conio>
  3. #pragma hdrstop
  4. #include <condefs.h>
  5.  
  6.  
  7. //---------------------------------------------------------------------------
  8. #pragma argsused
  9. int main(int argc, char **argv)
  10. {
  11. char *name = new char [100];
  12. cout << "the word is silver (all lower case)." << endl;
  13. cout << "enter name:";
  14. cin >> name;
  15. if (name == "silver") {
  16. cout << "it worked." << endl;
  17. }
  18. else if (name != "silver"){
  19. cout << "it didn't work." << endl;
  20. }
  21. cout << name;
  22. getch();
  23. return 0;
  24. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
evilsilver is offline Offline
84 posts
since Feb 2005
Feb 12th, 2005
0

Re: comparing strings

You can't compare strings with == (just like you can't assign strings with = ), use:

if ( strcmp( first_string, second_string) == 0 )
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Feb 12th, 2005
0

Re: comparing strings

Quote originally posted by winbatch ...
You can't compare strings with == (just like you can't assign strings with = ), use:

if ( strcmp( first_string, second_string) == 0 )
yes you can asign them with =, try this

C++ Syntax (Toggle Plain Text)
  1. char *test = new char [100];
  2. test = "this works";

p.s. i tried what you said and it work.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
evilsilver is offline Offline
84 posts
since Feb 2005
Feb 12th, 2005
0

Re: comparing strings

(Ah, my bad, forgot that you could with char * = rather than with char [] which requires strcpy.)
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Feb 12th, 2005
0

Re: comparing strings

>yes you can asign them with =, try this
Brilliant Holmes, nice memory leak you've got there. First you assign memory to a pointer, then you reseat the pointer to a string literal, thus losing a reference to the memory you just allocated.

You need to figure out the difference between an array, a pointer, and a string before trying stuff like that.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 12th, 2005
0

Re: comparing strings

Quote originally posted by evilsilver ...
yes you can asign them with =, try this
C++ Syntax (Toggle Plain Text)
  1. char *test = new char [100];
  2. test = "this works";
That's not assigning a string. That's assigning a pointer (and a memory leak).
Last edited by Dave Sinkula; Feb 12th, 2005 at 11:34 pm. Reason: D'oh!
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 13th, 2005
0

Re: comparing strings

lol good point sorry.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
evilsilver is offline Offline
84 posts
since Feb 2005
Feb 13th, 2005
0

Re: comparing strings

When I run your programme using Dev C++ at the line

C++ Syntax (Toggle Plain Text)
  1. if (name == "silver") {

I add-watch to the comparison

C++ Syntax (Toggle Plain Text)
  1. name == "silver"

I get

C++ Syntax (Toggle Plain Text)
  1. name == {115's', 105'i', 108'l', 118'v', 101'e', 114'r', 0'\0'}

name occurs like C string and is delimited by '\0'. So name is "silver\0" but not "silver".
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jimFan is offline Offline
22 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: FlushConsoleInputBuffer() not doing it's job
Next Thread in C++ Forum Timeline: deitel C++ 4th edition question??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC