Scrabble Solver

This is a discussion on Scrabble Solver within the C++ Programming forums, part of the General Programming Boards category; Hi, I been programming a scrabble solver. For some reason I cant get my functions to take in strings? Ill ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    110

    Scrabble Solver

    Hi, I been programming a scrabble solver. For some reason I cant get my functions to take in strings? Ill post my code below but I ran these functions in a diffrent program and they worked fine, but in my current program they dont work at all.

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    void copyword(string &newW,string old);
    int searchWord(string word,char letter);
    void ShowWord(string word);
    struct Words {
    	char str[30];
    };
    int main()
    {
      char str[30];
      string word[19000];
      int i = 0;
      
      ifstream b_file ( "Final.txt" );
    
      while(!b_file.eof())
      {
      b_file>> str;
      word[i] = str;
    
      i++;
    
      }
      b_file.close();
      string finallist[12000];
      int count = 0;
      int letters = 0;
      int cpywrd = 0;
      string phrase = "EAS";
    
    
      //REDUCE THE LIST OF WORDS TO ALL WORDS THAT START WITH ONE OF THESE LETTERS
    	int x = 0;
    	int sizeX = phrase.length();
      for(letters = 0;letters<=phrase.length();letters++)
      {
    	for(count = 0;count<=i;count++)
    	{
    	  if(word[count][0] == phrase[letters])
    	  {
    		  copyword( finallist[x],word[count]);
    		  x++;
    	  }
    	}
      }
    
      int sWord = 0;
      int v1 = 0;
       int v2 = 0;
       int nextList= 0;
       char temp ;
       char temp2;
      string finallist2[5000];
    
    
      //SEARCH EACH WORD FOR ALL OF THE LETTERS WHICH RETURNS a 1 IF ITS IN 0 IF ITS NOT.
      for(v1 = 0;v1<=x;v1++)
      {
    	for(letters = 0;letters<=phrase.size();letters++)
    	{
    	 sWord += searchWord(finallist[x].c_str(),phrase[letters]);
    	}
    	if(sWord >= finallist[x].length())
    		cout << "Found Word " ;
    	    ShowWord("HELLO");
    		cout << endl;
    
      }
    
    
      cout<< "Reduced to this many words " << x << endl;
       cout<< nextList << endl;
      system("pause");
    
      return EXIT_SUCCESS;
    }
    int searchWord(string word,char letter)
    {
    int i;
    int length = word.length();
    for(i = 0;i<=length;i++)
    {
    	if(word[i] == letter)
    		return 1;
    }
    return 0;
    
    
    }
    void ShowWord(string word)
    {
    
    int i;
    int length = word.length();
    for(i = 0;i<=length;i++)
    cout << word[i] ;
    }
    void copyword(string &newW,string old)
    {
    	int cpywrd;
    	  for(cpywrd = 0;cpywrd<=old.length();cpywrd++)
    		newW[cpywrd] = old[cpywrd];
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    You didn't say how it wasn't working, so I'll just mention a few problems.

    The C++ string does not have a terminating null, so you should use < instead of <= in your loops. Otherwise, you are invoking undefined behavior by accessing memory past the bounds of the string.

    You should not use eof() to control your loop. Instead, use the return value of operator>> to identify when any error has occurred, including an attempt to read past the end of the file, e.g. while (b_file >> str).

    I would also suggest using a vector instead of an array to hold your strings. It will grow as necessary when you push_back each string you read in, so you won't need to statically allocate a 19000 string array which might be too small or way too big. There is also no reason to use the C-style character array (str) to read in each string. Just make str a C++ string.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    The functions showword and searchWord are not taking in the string at all. When I debug it the variables fro the string are empty in those functions

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    You use finallist[x] in your loop when you should probably be using finallist[v1] since x is the number of valid strings in the finallist array. Since arrays are zero based, it is actually the string after the last valid one, which is why it is empty.

    Be sure to fix those other problems as well. One last thing is that you don't need to call .c_str() to pass a string to a function that takes a string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 8 puzzle game solver in C
    By LordMX in forum Game Programming
    Replies: 2
    Last Post: 08-06-2008, 10:24 AM
  2. Scrabble - find all possible plays
    By xErath in forum Tech Board
    Replies: 15
    Last Post: 11-15-2004, 01:25 AM
  3. Scrabble AI
    By vex_helix in forum C Programming
    Replies: 9
    Last Post: 04-03-2004, 02:03 PM
  4. IDEA: Equation solver
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 01-07-2003, 10:46 AM
  5. New Contest: Solitaire Solver!!
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 01:03 PM

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