Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,624 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,052 people online right now. Registration is fast and FREE... Join Now!




String Input & Comparison

 
Reply to this topicStart new topic

String Input & Comparison, in C++....

SPlutard
post 28 Mar, 2006 - 10:15 AM
Post #1


code.ninja @ Java, C, Flash, Python

Group Icon
Joined: 13 Aug, 2005
Posts: 607



Dream Kudos: 545
My Contributions


Well, I'm switching over from C to C++, and a few things are kinda weird when it comes to strings:

a.) How can I input a string into a variable from the user? I have:
CODE
std::cin>>mystring;
but that makes my compiler go nuts.

b.) I'm having trouble comparing two strings....sort-of. Is something like
CODE
if(string1 == string2)
legal? What about using >, <, <=, >=, and != operators? Do those compare length or what?

c.) Is there a specific header file needed for strings? #include <string.h>?

Sorry these sound like weird quesions, but I'm used to doing things differently in C.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 28 Mar, 2006 - 10:29 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Here is a good overview of the string class, although more in depth ones are available.
http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm

a.) your compiler may be going nuts becasue you have not included the proper string library

CODE

#include<string>
using namespace std;

int main(void)
{
  string mystring;
  cin>>mystring;
  return 0;
}

should be fine, although if you want to include spaces, you'll have to use another method, like .getline().

b.) Here is a list of how operators perform on strings (although, to cmpare the two, you can also use the .compare() method):
http://www.cppreference.com/cppstring/string_operators.html

c.) see above code snippet.

Here are some string methods
User is offlineProfile CardPM

Go to the top of the page

SPlutard
post 28 Mar, 2006 - 10:30 AM
Post #3


code.ninja @ Java, C, Flash, Python

Group Icon
Joined: 13 Aug, 2005
Posts: 607



Dream Kudos: 545
My Contributions


Thanks!
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:24AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month