Thread: Array Indexing w/ strlen()

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    48

    Array Indexing w/ strlen()

    I'm designing a hangman game engine and I can't seem to get variably indexing to work with the strlen function. Is there anything I can do to get thsi to work? Here is the beginning part of my constructor file (Hangman.cpp... not like you care anyways)

    Code:
    struct hangman hang;
    
    void hangman()
    {
    	hang.PickWord();
    	hang.length=strlen(hang.word);
    
    	char spaces[10];
    
    	for(short x=0; x<hang.length; x++)
    	{
    		spaces[x]='_';
    	}
    
    	while(hang.loopvar!=false)
    	{
    		hang.BlankSpaces();
    		hang.Guess();
    		hang.Check();
    	}
    
    }
    
    
    void hangman::PickWord()
    {
    	srand(GetTickCount());
    	short random=rand()%15;
    	
    	switch(random)
    	{
    	case 0:
    		strcpy("corndog",hang.word);
    		break;
    	case 1:
    		strcpy("computer",hang.word);
    		break;
    	case 2:
    		strcpy("telephone",hang.word);
    		break;
    	case 3:
    		strcpy("electric",hang.word);
    		break;
    	case 4:
    		strcpy("stereo",hang.word);
    		break;
    	case 5:
    		strcpy("oreo",hang.word);
    		break;
    	case 6:
    		strcpy("microphone",hang.word);
    		break;
    	case 7:
    		strcpy("cat",hang.word);
    		break;
    	case 8:
    		strcpy("moose",hang.word);
    		break;
    	case 9:
    		strcpy("internet",hang.word);
    		break;
    	case 10:
    		strcpy("cow",hang.word);
    		break;
    	case 11:
    		strcpy("potato",hang.word);
    		break;
    	case 12:
    		strcpy("card",hang.word);
    		break;
    	case 13:
    		strcpy("oven",hang.word);
    		break;
    	case 14:
    		strcpy("speakers",hang.word);
    		break;
    	default:
    		strcpy("earthquake",hang.word);
    	}
    }
    And, uh... thanks in advance if you decide to help
    Hey, you gotta start somewhere

  2. #2
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    What did you declare hang.word as?

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    char * strcpy ( char * dest, const char * src );
    Copy string.
    Copies the content pointed by src to dest stopping after the terminating null-character is copied.
    dest should have enough memory space allocated to contain src string.

    Parameters.

    dest
    Destination string. Should be enough long to contain string2.
    string2
    Null-terminated string to copy.


    you got your paramaters mixed up in pickword()

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    48

    haha

    lol, thx It sucks when you spend so much time trying to fix a problem that small. Anyways, that worked. Thx again
    Hey, you gotta start somewhere

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    actually, um... that still doesn't work. I get these errors. Is there any way to do this? It says it expects a constant expression. Here's the errors I get:

    Code:
    C:\Program Files\Microsoft Visual Studio\MyProjects\Hangman\Hangman.cpp(12) : error C2057: expected constant expression
    C:\Program Files\Microsoft Visual Studio\MyProjects\Hangman\Hangman.cpp(12) : error C2466: cannot allocate an array of constant size 0
    C:\Program Files\Microsoft Visual Studio\MyProjects\Hangman\Hangman.cpp(12) : error C2133: 'spaces' : unknown size
    C:\Program Files\Microsoft Visual Studio\MyProjects\Hangman\Hangman.cpp(90) : error C2065: 'spaces' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\Hangman\Hangman.cpp(90) : error C2109: subscript requires array or pointer type
    Oh, and, BTW, here's the revised first part of hangman.cpp:

    Code:
    #include <iostream.h>
    #include <string.h>
    #include <windows.h>
    #include "Hangman.h"
    
    struct hangman hang;
    
    void hangman()
    {
    	hang.PickWord();
    	hang.length=strlen(hang.word);
    	char spaces[hang.length];
    
    	for(short x=0; x<hang.length; x++)
    	{
    		spaces[x]='_';
    	}
    
    	while(hang.loopvar!=false)
    	{
    		hang.BlankSpaces();
    		hang.Guess();
    		hang.Check();
    	}
    
    }
    
    
    void hangman::PickWord()
    {
    	srand(GetTickCount());
    	short random=rand()%15;
    	
    	switch(random)
    	{
    	case 0:
    		strcpy(hang.word, "corndog");
    		break;
    	case 1:
    		strcpy(hang.word, "computer");
    		break;
    	case 2:
    		strcpy(hang.word, "telephone");
    		break;
    	case 3:
    		strcpy(hang.word, "electric");
    		break;
    	case 4:
    		strcpy(hang.word, "stereo");
    		break;
    	case 5:
    		strcpy(hang.word, "oreo");
    		break;
    	case 6:
    		strcpy(hang.word, "microphone");
    		break;
    	case 7:
    		strcpy(hang.word, "cat");
    		break;
    	case 8:
    		strcpy(hang.word, "moose");
    		break;
    	case 9:
    		strcpy(hang.word, "internet");
    		break;
    	case 10:
    		strcpy(hang.word, "cow");
    		break;
    	case 11:
    		strcpy(hang.word, "potato");
    		break;
    	case 12:
    		strcpy(hang.word, "card");
    		break;
    	case 13:
    		strcpy(hang.word, "oven");
    		break;
    	case 14:
    		strcpy(hang.word, "speakers");
    		break;
    	default:
    		strcpy(hang.word, "earthquake");
    	}
    }
    Last edited by c++_n00b; 06-03-2002 at 10:53 AM.
    Hey, you gotta start somewhere

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    5
    You should allocate dinamically 'spaces', in order to allocate a non-constant size.

    Code:
    char* spaces = new char[ hang.length ];
    ...
    delete[] spaces;
    However, you should use 'string' instead of 'char*'. C++ is an OO language, so you can gain more flexibility using 'string' STL template class. <string>

    Bye.

  7. #7
    Unregistered
    Guest
    alternatively have spaces be a member of the struct type hangman, declared as an array of 11 char (just like the member word, presumably, and leave out the line:

    char spaces[hang.length];

    ______________________________________-

    Are you trying to write this in C or C++? I ask because the following line appears to be declaring an instance of a C style struct of type hangman with name of hang.


    struct hangman hang;

    Yet you have this line (and others)

    void hangman::PickWord()

    indicating that hangman is a C++ style struct/class. Instances of C++ style struct don't use the keyword struct in the instance declaration, and it is only sometimes used in C.

    In addition presumably hangman() is the constructor for the struct/class hangman, rather than an attempt as a replacement for main(), although conceivably it could be a non-member function if this is supposed to be is C. If hangman is supposed to be a constructor, then drop the return type void from the line below and replace it with the word hangman followed by the scope operator.

    void hangman()

    should be

    hangman::hangman()
    {


    Obviously if you are trying to write this in C, then different advice would apply.

    In C++ you don't use the an instance in the method definitions, if the changes made by the method are for the calling object. Only if you pass in an object and change the data do you need the instance name.

    It is a rule of debugging that fixing any one error statement may clear up or unmask one or more additional statements.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    48
    Actually I wasn't writing it in C at all. I was using 'struct' because it automatically declares the members a public and that's what i want. 'Struct' is more appropriate to use than 'class' in this case. This is not my 'main.cpp' either. It is a constructor file. But thx 4 the help anyways and thx a lot to the guy that showed me how to do it dynamically I don't know if it works yet but I've thougth of doing that and I think it will work.

    Oh, yeah, and r0x, I've tried to use 'string' in a class member and it doesn't work for me. Do you know a way to do this? And I'm using Visual c++ 6.0 (standard)
    Hey, you gotta start somewhere

  9. #9
    Unregistered
    Guest
    Code:
    ////////////
    //hangman.h
    ////////////
    #ifndef HANGMAN_H
    #define HANGMAN_H
    #include <iostream.h>
    
    struct hangman
    {
       char word[11];
       char spaces[11];
       int length;
       int numOfGuesses;
       char guesses[26];
       hangman();
       void Pickword();
       void run();
    };
    #endif;
    
    _____________________________________________
    
    /////////////////////
    //hangman.cpp
    /////////////////////
    #include "hangman.h"
    
    //default constructor
    hangman::hangman()
    {
       //call Pickword function to load word member
       PickWord();
    
       //determine how long the word is so can display that many spaces
       length = strlen(word);
       
       //initialize numOfGuesses to zero
       numOfGuesses = 0;
      
       //initialize spaces to one underscore per letter in word
       for(int x = 0; x < length; x++)
      {
          spaces[x] = '_';
      }
    }
    
    //--------------------------------------------------
    void hangman::PickWord()
    {
       srand(GetTickCount());
       int random=rand() % 15;
    	
       switch(random)
       {
    	case 0:
    		strcpy(hang.word, "corndog");
    		break;
    	case 1:
    		strcpy(hang.word, "computer");
    		break;
    	case 2:
    		strcpy(hang.word, "telephone");
    		break;
    	case 3:
    		strcpy(hang.word, "electric");
    		break;
    	case 4:
    		strcpy(hang.word, "stereo");
    		break;
    	case 5:
    		strcpy(hang.word, "oreo");
    		break;
    	case 6:
    		strcpy(hang.word, "microphone");
    		break;
    	case 7:
    		strcpy(hang.word, "cat");
    		break;
    	case 8:
    		strcpy(hang.word, "moose");
    		break;
    	case 9:
    		strcpy(hang.word, "internet");
    		break;
    	case 10:
    		strcpy(hang.word, "cow");
    		break;
    	case 11:
    		strcpy(hang.word, "potato");
    		break;
    	case 12:
    		strcpy(hang.word, "card");
    		break;
    	case 13:
    		strcpy(hang.word, "oven");
    		break;
    	case 14:
    		strcpy(hang.word, "speakers");
    		break;
    	default:
    		strcpy(hang.word, "earthquake");
    	}
    }
    
    //------------------------------------------
    void hangman::run()
    {
       int i;
      while(numOfGuesses < 26)
      {
        //clear the screen
        for(i = 0; i < 50; i++)
       { 
          cout << endl;
       }
      
       cout << "the secret word is " << word << endl << endl;
    
       cout <<  "your guesses so far are :  ";
       if(numOfGuesses == 0)
         cout << "no guesses yet" << endl;
       else
       {
         for(i = 0; i < numOfGuesses; i++)
        {
          cout << guesses[i] << ' ';
        }
        cout << endl << endl;
       }
    
       cout << " the correct guesses so far include: ";
       for(i = 0; i < length; i++)
       {
         spaces[i];
       }
       
       bool done = true;
      
       for(i = 0; i < length; i++)
      {
        if(spaces[i] == '_')
            done = false;
       }
      
       if(done == true)
       {
         cout << "you guessed the word correctly" << endl;
         numOfGuesses = 26;
       }
       else
       {
         cout << "enter next guess" << endl;
         cin >> guesses[numOfGuesses];
         //check to see if this guess is in word, and if so replace the _ at that index in spaces by the the current guess.
         numOfGuesses++;
       } 
     }
    }
    
    ///////////////////
    //game.cpp
    ///////////////////
    #include <iostream.h>
    #include "hangman.h"
    
    int main()
    {
      hangman hang;  //this will call default constructor
      hang.run();  //this will start the actual game
      
      return 0;
    }
    You will need to work through the routine of matching the guess to the word and placing the letter at the appropriate index in spaces.

    There are probably a few other things that need tidying up, too. The above is an example of how to use classes in a (somewhat) modular format.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM