Thread: I could use some help on my program

  1. #1

    Unhappy I could use some help on my program

    I'm making a string parser program to find specific words within large character arrays. Here's what I've got:
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <conio.h>
    #include <dos.h>
    #include <windows.h>
    
    char com[41];
    bool key = true, sword = true, shield = true, cup = true; // Item statuses
    
    void cls()
    {
       COORD coordScreen = { 0, 0 }; // A bunch of Windows API crud, ignore
       DWORD cCharsWritten;
       CONSOLE_SCREEN_BUFFER_INFO csbi;
       DWORD dwConSize;
       HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
       GetConsoleScreenBufferInfo(hConsole, &csbi);
       dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
       FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
       GetConsoleScreenBufferInfo(hConsole, &csbi);
       FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
       SetConsoleCursorPosition(hConsole, coordScreen);
    }
    
    void items() {
     cout << endl;
     if (key == true) { // If there's a key
      cout << "There is a key on the ground.\n";
     }
     if (sword == true) { // If there's a sword
      cout << "There is a sword on the ground.\n";
     }
     if (shield == true) { // If there's a shield
      cout << "There is a shield on the ground.\n";
     }
     if (cup == true) { // If there's a cup
      cout << "There is a cup on the ground.\n";
     }
     cout << endl;
    }
    
    void main() {
     int i = 0, n = 0; // For loop variables
     cout << "String Parser\nBy: Scott A. Hand" << endl;
     Sleep(750); // Pause
     cls(); // Clear the screen
     items(); // Initialize item status and screen
     cout << "Enter command.\n> ";
     cin >> com; // Get user input
     for (i = 0;i <= 40;i++) {
      if ((com[i] == 'g' && com[i+1] == 'e' && com[i+2] == 't') || com[i] == 'G' && com[i+1] == 'E' && com[i+2] == 'T') { // If the user has typed "get" anywhere
       cout << "You have typed 'get'" << endl; // Debugging statement
       for (n = 0;n < 40;n++) { // Loop to catch the argument
        if (com[n] == 'k' && com[n+1] == 'e' && com[n+2] == 'y') { // If the argument's "key"0
         key = false; // Means it shouldn't show the key.
         cout << "You picked up a key.\n"; // Statement to tell me that this works.
         goto goodbye; // Required to speed things up
        }
        if (com[n] == 's' && com[n+1] == 'w' && com[n+2] == 'o' && com[n+3] == 'r' && com[n+4] == 'd') { // If the argument's "key"0
         sword = false; // Means it shouldn't show the sword.
         cout << "You picked up a sword.\n"; // Statement to tell me that this works.
         goto goodbye; // Required to speed things up
        }
       }
      }
     }
     exit(0);
     goodbye:
     cout << "Goodbye!\n";
    }
    I try to indent, comment, space, etc. as much as possible. The problem is that it recognizes "get" but not "key" or "sword" it recognizes "getkey" and "getsword" though. I use Visual C++ 6.0 on Windows 98. Any help would be nice.
    -Save the whales. Collect the whole set.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    First problem is (apart from the void main) is that cin >> com; only reads one word from the input (it stops at a space).
    So your attempt to decode the second word will always fail....

    At the very least, you need to write some more functions to make like simpler - for instance, something which extracts words from the command typed in, rather than all those individual character tests.

    I've used strtok in my example, but there may be other (safer) ways of doing this in C++, especially when you get into using say the string class for storing strings.

    Here's an example
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main ( ) {
    
        // get input
        char com[80];
        // cin >> com;  this just gets the first word
        cin.getline( com, sizeof(com) );    // this gets a whole line
    
        // split input line into words
        char *words[80];
        int   nwords = 0;
        for ( char *p = strtok(com," ") ; p != NULL ; p = strtok(NULL," ") ) {
            words[nwords++] = p;
        }
    
        // print the words - or whatever
        for ( int i = 0 ; i < nwords ; i++ ) {
            cout << "word " << i << " is " << words[i] << endl;
        }
    
        // perhaps using strcmp to detect specific words
        // if ( strcmp( words[0], "get" ) == 0 ) { // get something }
        return 0;
    }
    
    And a command run....
    get sword and key
    word 0 is get
    word 1 is sword
    word 2 is and
    word 3 is key
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I think your problem is that the >> operator will stop reading when it hits whitesapce. If you want to be able to enter something like "get key", you'll have to use cin.get() or cin.getline() to read the input.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM