Thread: I'm new at strings, or "char"s.

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    I'm new at strings, or "char"s.

    With my console program, I use iostream.h, and therefore cin >> number. However, how would I be able to use words/characters instead of numbers with that, or would I need something aside from iostream?

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    nothing really..

    Code:
    #include <iostream.h>
    
    char word[20]; // that makes a variable that can hold letters and makes it 20 spaces long
    
    int main()
    {
        cout << " Type a word under 20 spaces: ";
        cin >> word;
    
        cout << endl;
        cout << " You typed: " << word;
    
        cin.get();  // pauses the program until some one types a key and hits enter 
        return 0;
    }
    What is C++?

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    Ok got that working... but how do I use it in an if statement? Right now I have something like this:
    Code:
    cin >> word;
    if(word == "quit")
    {
            Code to end loop(not shown here) goes here.
    }
    However, that doesn't work.

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    try a whil loop

    Code:
    while ( word != "quit" )
    {
          cout << "type: ";
          cin >> word;
       
          cout << word;
    }
    What is C++?

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    I fail to see how a while loop would succeed where an if statement fails...

    The problem is not the statement...

    Code:
    cin >> word;
    if(word == 1)
    {
            Code to end loop(not shown here) goes here.
    }
    If I have declared word to be an integer, the preceding section of code would have worked. However, when I change it to the char type, it does not.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    178
    Instead of unsing
    Code:
    cin >> word;
    if(word == 1)
    {
            Code to end loop(not shown here) goes here.
    }
    use
    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    int main()
    {
        char word[20];
        
        cout<<"Enter a word : ";
        cin>> word;
    
        if(strcmp(word, "quit"))   //strcmp compares two strings
                                                //it compares the char word with 
                                          //quit,  just don't forget to include stdio
        {
                return 0;
        }
         return 0;
    }
    Oi Oi Oi!!!!

  7. #7
    Unregistered
    Guest
    you could also using string.h for strcmp

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    It doesnt work... as in it recognizes strcmp, but it doesn't work...
    Code:
    if(strcmp(command, "quit"))
    	{
    		running = false;
    	}
    That right?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try either:
    Code:
    if (strcmp(command, "quit") == 0)
    {
    	running = false;
    }
    Or:
    Code:
    if (!strcmp(command, "quit"))
    {
    	running = false;
    }

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    Attention! Use the string class instead, it supports strings with all relational operators. Thus,

    Code:
    string word;
    cin >> word;
    
    if(word == "quit")
    {
    //do whatever you want
    }
    works just fine!

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    35
    string is unidentified blah blah blah...

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try putting this at the top (use string, not string.h).
    Code:
    #include <string>
    
    using namespace std;

  13. #13
    string is good, but compilers like to b*tch about string a lot for some reason

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. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM