need some help

This is a discussion on need some help within the C++ Programming forums, part of the General Programming Boards category; #include <iostream> #include <string> #include <cctype> using namespace std; void main() { // build command map string commands[] = { ...

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

    Question need some help

    #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 string:
    cout << "East" << endl;
    break;
    case string:
    cout << "North" << endl;
    break;
    case string:
    cout << "West" << endl;
    break;
    case string:
    cout << "South" << endl;
    break;


    default:
    cout << "Unknow command." << endl;
    }
    }


    someone want to explain whats going on in this to me?... i understand a little but it would be great to know it all... just reply please

  2. #2
    ZAK
    ZAK is offline
    Registered User
    Join Date
    May 2002
    Posts
    13
    First the programming is building an array of strings...then it is finding out the size of the array...getting input in the form of a string converting the string to lowercase checking to see if there is a command through the for if statment (the break statement is very very sloppy loop should be a while loop) and performs an operation based on the input it got using a switch structure which outputs a command.
    "Computer Science is no more about computers than astronomy is about telescopes"
    - E.W. Dijkstra

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    10
    so whats an example of some none sloppy loops?

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by JPed2003
    so whats an example of some none sloppy loops?
    Code:
    x = 0;  //it was declared on a previous 'for'
    while(commands[x] != strCmd && x < nCount){x++;}
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Re: need some help

    Sorry I post it again in this way so I can write it:
    Originally posted by JPed2003

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    int 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;
        }//for
    
        // execute command
        switch(i) {
        case string:
               cout << "East" << endl;
               break;
        case string:
            cout << "North" << endl;
            break;
       case string:
            cout << "West" << endl;
            break;
       case string:
            cout << "South" << endl;
            break;
        
        default:
            cout << "Unknow command." << endl;
      }//switch
    return 0;
    }//main
    someone want to explain whats going on in this to me?... i understand a little but it would be great to know it all... just reply please
    let me see it again.
    Last edited by NANO; 05-15-2002 at 01:04 PM.
    C++
    The best

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21