Thread: Comparing Chars

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Comparing Chars

    I'm using Borland C++. How would I go about having a space when using if(!strcmp(chc1,getkys))? Here is a sample of my code:

    //The Highlight Problem Is The getkys variable


    //Filename: lv1.cpp
    //Function: Level 1
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    void main()
    {
    int keyamt=0;
    int lp=1;
    unsigned char chc1[20];
    unsigned char getkys[]="GET KEYS";
    unsigned char getchc[20];
    unsigned char inv[]="INVENTORY";
    cout << "LEVEL ONE" << endl;
    cout << "ESCAPE FROM THE DUNGEON" << endl;
    cout << endl << "YOUR IN A DARK DUNGEON CELL." << endl;
    cout << "THE WALLS ARE COVERED WITH MOSS AND COBWEBS." << endl;
    cout << "THE SLEEPING GUARD SEEMS EXTREMELY VULNERABLE" << endl;
    cout << "A PAIR OF KEYS ARE ATTACHED TO HIS BELT." << endl;
    while(lp!=0)
    {
    cout < ">";
    cin >> chc1;
    if(!strcmp(chc1,getkys))
    {
    cout << "YOU NOW HAVE THE KEYS." << endl;
    }
    if(strcmp(chc1,getkys))
    {
    cout << "YOU HAVE NOTHING." << endl;
    }
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could use getline().

    >cin >> chc1;

    Change to:
    cin.getline(chc1,20);

  3. #3
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Understanding

    I don't specifically understand that... Can you put it in more specific words? Maybe a more specific example? And what header file do I need?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Change this:
    cin >> chc1;

    To this:
    cin.getline(chc1,20);

  5. #5
    Shadopw12345
    Guest

    Unsigned char?

    Exactly what does it mean when you declare a character to be "unsigned"?

    I thought that only had to do with variables of type int.

  6. #6
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Change

    It doesn't seem to work using the change this: to this: in my compiler... I don't know why... again, I'm using borland c++.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Can you please post your latest code and what the error message is?

  8. #8
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Post Reply

    //Filename: lv1.cpp
    //Function: Level 1
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    void main()
    {
    int keyamt=0;
    int lp=1;
    unsigned char chc1[20];
    unsigned char getkys[]="GETKEYS";
    unsigned char getchc[20];
    unsigned char inv[]="INVENTORY";
    cout << "LEVEL ONE" << endl;
    cout << "ESCAPE FROM THE DUNGEON" << endl;
    cout << endl << "YOUR IN A DARK DUNGEON CELL." << endl;
    cout << "THE WALLS ARE COVERED WITH MOSS AND COBWEBS." << endl;
    cout << "THE SLEEPING GUARD SEEMS EXTREMELY VULNERABLE" << endl;
    cout << "A PAIR OF KEYS ARE ATTACHED TO HIS BELT." << endl;
    while(lp!=0)
    {
    cout < ">";
    cin >> chc1;
    if(!strcmp(chc1,getkys))
    {
    Change this:
    cin >> chc1;

    To this:
    cin.getline(chc1,20);
    cout << "YOU NOW HAVE THE KEYS." << endl;
    }
    else if(strcmp(chc1,getkys))
    {
    cout << "YOU HAVE NOTHING.";
    }
    }
    }
    //errors:
    //undefined symbol "change"
    //statement missing ; in function main
    //undefined symbol "to"
    //statement missing ; in function main

  9. #9
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    use some delimeter in the getline()

    cin.getline(chc1,20,'\n');
    -

  10. #10
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question It still doesn't work...

    Can you show me some full sample code of how this might work?

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    //Filename: lv1.cpp 
    //Function: Level 1 
    #include <iostream.h> 
    #include <conio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    void main() 
    { 
    int keyamt=0; 
    int lp=1; 
    unsigned char chc1[20]; 
    unsigned char getkys[]="GETKEYS"; 
    unsigned char getchc[20]; 
    unsigned char inv[]="INVENTORY"; 
    cout << "LEVEL ONE" << endl; 
    cout << "ESCAPE FROM THE DUNGEON" << endl; 
    cout << endl << "YOUR IN A DARK DUNGEON CELL." << endl; 
    cout << "THE WALLS ARE COVERED WITH MOSS AND COBWEBS." << endl; 
    cout << "THE SLEEPING GUARD SEEMS EXTREMELY VULNERABLE" << endl; 
    cout << "A PAIR OF KEYS ARE ATTACHED TO HIS BELT." << endl; 
    while(lp!=0) 
    { 
    cout < ">"; 
    cin.getline(chc1,20); 
    if(!strcmp(chc1,getkys)) 
    { 
    cout << "YOU NOW HAVE THE KEYS." << endl; 
    } 
    else if(strcmp(chc1,getkys)) 
    { 
    cout << "YOU HAVE NOTHING."; 
    } 
    } 
    }

  12. #12
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Thank You, Swoopy

    Thank You,
    It worked. Now I can conitinue with the game I'm creating.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Whew ... I was starting to get worried there!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems Comparing Char's
    By MrKnights in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2007, 01:49 PM
  2. comparing chars
    By jaxlle in forum C Programming
    Replies: 5
    Last Post: 12-27-2006, 12:00 PM
  3. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM
  4. Comparing Chars
    By Jperensky in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2002, 12:29 PM
  5. comparing chars with pointer to char
    By papous in forum C Programming
    Replies: 5
    Last Post: 11-24-2001, 01:23 AM