Thread: Help with strings.

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    Help with strings.

    ok. I'm trying to learn C++ and i'm trying to make a simple program that i've done in 3 or 4 other languages. What it does is it asks the user for their name. If the name entered matches the name I want it to match, the program says "Hello!". Otherwise it says "Mewshi" . Here's the code i have so far.
    Code:
    #include <iostream.h>
    int main ()
    {
    	cout << "What is your name?" << endl;
    	char name;
    	cin >> name;
    	if (name==kitty)
    		cout << "Hello!" << endl;
    	else
    		cout << "Mewshi" << endl;
    	return 0;
    }
    Could someone help me get it to compile? i really wanna learn how to do this.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main ()
    {
    	cout << "What is your name?" << endl;
    	string name;
    	cin >> name;
    
    	if (name == "kitty")
    		cout << "Hello!" << endl;
    	else
    		cout << "Mewshi" << endl;
    	return 0;
    }
    This particular example making use of the C++ string class

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Thanks. It works fine now ^^
    So if i want to work with strings, i need to include #include <string> at the beginning of the code?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Yes and make sure you have the using namespace std
    Woop?

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation Just to add to the confusion...

    Kitty,

    There are two types of strings in C++.

    C++ string objects <string> are more complicated behind the scenes, but they are easier to use. Often, the syntax looks like you're using a single variable.

    C-style strings <cstring> are null-terminated character-arrays. An array of ASCII values, with a zero-value indicating the end-of string. C-style strings are sometimes called character arrays or null-terminated strings.

    char name; // name can only hold a single character, not the whole string.

    Also, you cannot use == to compare two arrays. There is a function in <cstring> for comparing C-Style strings. It works by comparing one pair of characters at a time in a loop. However, == does work with C++ string objects because its been overloaded for the C++ string class... but that's another subject.

    Because a single variable cannot hold the entire array, you generally need to use a pointer to access the array. The Cprogramming.com string tutorial uses C-style strings.

    In general, C++ strings are preferred. Sometimes (as with the Win32 API) you'll have to use C-style strings, or convert your C++ string to a C-style string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM