Thread: if statments

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    10

    Question if statments

    ok... im making a one player RPG game in my programming class and i cant seem to find anywhere in the deitel and deitel book that it will tell you how to make a piece of code that will repond IF someone types a WORD not a LETTER. i can get the letters to work find with the SWITCH command. only thing is, i cant get default to work on switch. so if anyone can help me please reply:

    QUESTIONS:
    -If statments using a WORD not a LETTER.
    -Defaults in the switch commands.

    JPed

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: if statments

    -If statments using a WORD not a LETTER.

    I assume you mean string? You cannot compare strings with the = operator ( if(String1 = String2) ), so you have to use strcmp(String1, String2) which returns 0 if the strings are equal.

    -Defaults in the switch commands.
    Code:
    switch(MyVar)
    {
       case 0:
          ...
       break;
       case 1:
       case 2:
       case 3:
          ...
       break;
       default:
          ...
       break;
    }
    Which part don't you understand? You can't compare strings with switch statements either, if that's what you're asking...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    10
    ok... the strcmp im assuming is just the same as the IF. and ive got it working for now. but now i need to make it if they equal it in lowercase or capital case, then it will do the same either way... is there anyway to make the string always equal lower or capital case?

    JPed

    oh yea... borland 5 also
    Last edited by JPed2003; 05-10-2002 at 01:40 PM.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    10
    oh yea... and the default isnt working... its repeating the default no matter what letter they enter.. its working... except for that... like this.......

    computer: Enter a command:
    user: n(stands for north)
    computer: (1,0)... thats the users position
    computer: Enter a command: Invalid command!(then from here it lets you enter another command but it does the invalid command everytime)

    my code:
    while ( ( command = cin.get() ) != 'x' ){//X is for exit
    switch (command){
    case 1:
    ...
    break;

    default:
    cout << "Invalid command!" << endl;
    break;
    }
    }

    sorry if thats ugly.. .but i think its right... but if not it is on mine everything except for the default...

    JPed

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    first, isn't is cin.get(command)? I don't know, you might be right on that one. But what your problem is is that you are comparing the ASCII value (char casted to an int by switch) of command to an integer, 1. A user is not likely to enter the ASCII character corresponding to 1. If you want to get a numeric value, and trust the user to enter a number, then you can use atoi() on the command.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    49
    Your can map a word to an int, then switch is in the way.

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    void main()
    {
        // build command map
        string commands[] = {
             "east", "north", "south", "west" // ... all the command word with lower case
        };
        const int nCount = sizeof(commands)/sizeof(string);
    
        // receive command from console and convert to lowercase
        string strCmd;
        cin >> strCmd;
        for(int i=0; i<strCmd.size(); i++)
            strCmd[i] = _tolower(strCmd[i]);
    
        // map command to int
        for(i=0; i<nCount; i++)
        {
            if(strCmd == commands[i])
                break;
        }
    
        // execute command
        switch(i)
        {
        case 0:
            //do_command_east();
            break;
        case 1:
            //do_command_north();
            break;
    
        // ...
    
        default:
            cout << "Unknow command." << endl;
        }
    }
    Hello, everyone.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by JPed2003
    while ( ( command = cin.get() ) != 'x' ){//X is for exit
    switch (command){
    case 1:
    ...
    break;

    default:
    cout << "Invalid command!" << endl;
    break;
    }
    }
    Is this for the user to press button '1'?
    Then you should have case '1': instead. Command is prolly a char, so you must compare it with '1' (the character 1), not 1 (the number 1).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    10
    ok... case 1 isnt actually the number one... im just using it as it being the first case... the real one is actually case 'n':.... i know what im doing alittle bit

    JPED

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    10
    and hotman_x is my hero

    jped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If statments using char variable, need help
    By pandapwnage in forum C++ Programming
    Replies: 20
    Last Post: 10-01-2005, 03:33 PM
  2. Not, Or, And statments
    By CompiConQuiso in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2005, 05:27 PM
  3. True or false statments used in my return...
    By correlcj in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2002, 04:26 PM
  4. Case statments with long strings... is it possible?
    By Unregistered in forum Game Programming
    Replies: 4
    Last Post: 06-21-2002, 11:22 PM
  5. If Then Statments
    By BladeBoss2 in forum C Programming
    Replies: 6
    Last Post: 05-20-2002, 04:50 PM