comparing strings

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

comparing strings

 
0
  #1
Feb 12th, 2005
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: comparing strings

 
0
  #2
Feb 12th, 2005
You can't compare strings with == (just like you can't assign strings with = ), use:

if ( strcmp( first_string, second_string) == 0 )
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

Re: comparing strings

 
0
  #3
Feb 12th, 2005
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

  1. char *test = new char [100];
  2. test = "this works";

p.s. i tried what you said and it work.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: comparing strings

 
0
  #4
Feb 12th, 2005
(Ah, my bad, forgot that you could with char * = rather than with char [] which requires strcpy.)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,823
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 748
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: comparing strings

 
0
  #5
Feb 12th, 2005
>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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,443
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: comparing strings

 
0
  #6
Feb 12th, 2005
Originally Posted by evilsilver
yes you can asign them with =, try this
  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!
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

Re: comparing strings

 
0
  #7
Feb 13th, 2005
lol good point sorry.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: jimFan is an unknown quantity at this point 
Solved Threads: 1
jimFan jimFan is offline Offline
Newbie Poster

Re: comparing strings

 
0
  #8
Feb 13th, 2005
When I run your programme using Dev C++ at the line

  1. if (name == "silver") {

I add-watch to the comparison

  1. name == "silver"

I get

  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".
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC