GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Mar-2004, 11:32
soccer022483 soccer022483 is offline
New Member
 
Join Date: Mar 2004
Posts: 17
soccer022483 is on a distinguished road
Exclamation

How do you compare two strings? [was: Easy Question I'm sure]


How do you compare two strings??

I.E.
CPP / C++ / C Code:
string a = "Hey";
string b = "Hey";

if( a == b )
  cout << "a is equal to b" << endl;
well it's not cout-ing anything!

Please help

Austin
Last edited by JdS : 22-Mar-2004 at 16:16. Reason: Please enclose c code in [c] & [/c] for syntax highlighting
  #2  
Old 22-Mar-2004, 11:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by soccer022483
How do you compare two strings??

I.E.
CPP / C++ / C Code:
string a = "Hey";
string b = "Hey";

if( a == b )
  cout << "a is equal to b" << endl;
well it's not cout-ing anything!

Please help

Austin


Here's a complete program that works for me (Borland bcc, Microsoft VC++, GNU g++ compilers). What are you using?

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{

  string a = "Hey";
  string b = "Hey";

  if( a == b )
    cout << "a is equal to b" << endl;
  else
    cout << " a is not equal to b" << endl;
  return 0;
}

Dave
  #3  
Old 22-Mar-2004, 11:48
soccer022483 soccer022483 is offline
New Member
 
Join Date: Mar 2004
Posts: 17
soccer022483 is on a distinguished road
Quote:
Originally Posted by davekw7x
Here's a complete program that works for me (Borland bcc, Microsoft VC++, GNU g++ compilers). What are you using?

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{

  string a = "Hey";
  string b = "Hey";

  if( a == b )
    cout << "a is equal to b" << endl;
  else
    cout << " a is not equal to b" << endl;
  return 0;
}

Dave

Ok I just copyed exactly that and it printed "a is not equal to b"
Did you compile and run yours?
I'm using microsoft visual C++

Austin
  #4  
Old 22-Mar-2004, 12:01
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Dave's example works just fine for me, too. The problem you're having is probably unrelated to the equality of the strings. Check your syntax to make sure everything is right.
__________________
-Aaron
  #5  
Old 22-Mar-2004, 12:18
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
I just ran the same code in my msvc++ 6.0 and it works fine. try initializing the strings using constructors: string a("Hey"); string b("Hey");

Quote:
Originally Posted by aaroncohn
Dave's example works just fine for me, too. The problem you're having is probably unrelated to the equality of the strings. Check your syntax to make sure everything is right.
  #6  
Old 22-Mar-2004, 12:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by soccer022483
Ok I just copyed exactly that and it printed "a is not equal to b"
Did you compile and run yours?
I'm using microsoft visual C++

Austin

I compiled the program (named st.cpp) and ran with the following command-line compilers:

Borland bcc (C++ version 5.5.1)

GNU g++ (GCC 3.3.1 cygwin)

Microsoft cl (Visual c++ version 6.0)

All said:
Quote:
a is equal to b

Dave
  #7  
Old 22-Mar-2004, 12:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by soccer022483
Ok I just copyed exactly that and it printed "a is not equal to b"
Did you compile and run yours?
I'm using microsoft visual C++

Austin

I just did a copy-and-paste from this post to see if anything got lost in file upload/download.

Still works as advertised.

Dave
Last edited by davekw7x : 22-Mar-2004 at 13:35.
  #8  
Old 22-Mar-2004, 15:13
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by soccer022483
Ok I just copyed exactly that and it printed "a is not equal to b"
Did you compile and run yours?
I'm using microsoft visual C++

Austin
There must be something wrong with your compiler or your process of compiling the file. Are you getting any warnings when you compile the program?
__________________
-Aaron
  #9  
Old 22-Mar-2004, 15:15
soccer022483 soccer022483 is offline
New Member
 
Join Date: Mar 2004
Posts: 17
soccer022483 is on a distinguished road

got it!


Ok I got it finally. I don't really know what the problem is, but I went to a different computer and used identical code and it worked. So something was wrong with that computer or something. Thanks for the help guys.

Austin
  #10  
Old 22-Mar-2004, 15:45
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by soccer022483
Ok I got it finally. I don't really know what the problem is, but I went to a different computer and used identical code and it worked. So something was wrong with that computer or something. Thanks for the help guys.

Austin
Glad to hear it. Keep up the learning!
__________________
-Aaron
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Complete, Fast and easy Template System Allowee MySQL / PHP Forum 3 07-Feb-2004 11:31
question of practice magiccreative C++ Forum 1 06-Feb-2004 08:17
a C input question.. tmike C Programming Language 2 19-Sep-2003 03:39
a C input question tmike C Programming Language 1 16-Sep-2003 03:31
AVI Joiner, MPEG Joiner, RM Joiner, all is in Easy Video Joiner SmartHero Computer Software Forum - Windows 2 08-Mar-2003 03:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.