2

New to programming, I have tried just about everything to get my program to run, but cannot figure it out. This is for my Into to programming course, so I'll be posting the instructions for it as well for context purposes.

I know for a fact that my program is running properly up until the closing bracket in line 62. The primary issue that im having is right after that, where I call the function sharedLetters. For context, I have attempted multiple ways of trying to fix the function definition, so at this point its a little jumbled, but it was wrong to begin with, so there's that. As for the remainder of the code, im not entirely sure if its written correct or not, as I cant get beyond the called function.

Assignment Instructions

For this assignment, you will write a program that forms the basis of a crossword puzzle generator. In order to create a crossword puzzle you need to identify letters that are common to one or more words. This program helps you generate crossword puzzles by identifying letters that are common to several words and storing them in an intermediate data structure.

Using intermediate data structures is one way to simplify your coding. You create a data structure that organizes your input so the final output is easier to create. Your CrosswordGenerator program will open a file containing a list of words. To prepare for crossword puzzle generation your code should implement the following pseudo code:

  1. Open a file named wordlist.txt
  2. Determine the number of words (N) in the file.
  3. Create an array of that size called wordArray[];
  4. Read all the words from the file into an array called wordArray[N].
  5. Sort the array of words by length in descending order
  6. Start at the beginning of the array (the longest word) and examine the word
  7. Find all other words in the array that can intersect the current word Two words can intersect if they share a letter.

Write a function called String sharedLetters(String S1, String S2).

Implement sharedLetters(S1,S2) that will return a string containing letters shared by the two parameter strings S1 and S2. If no letters are shared between S1 and S2 sharedLetters() should return the empty string "". sharedLetters(S1,S2) should ignore case; capital and lower case letters are considered equal.

Create a two dimensional array of Strings wordsIntersect[N][N] where each dimension N is the size of wordArray.

Iterate over wordArray comparing distinct words. Do not compare a word to itself. If wordArray[i] shares any letters with wordArray[j] place the result of sharedLetters(wordArray[i],wordArray[j]) into wordsIntersect[i][j]. Non-alphabetic characters should be ignored.

Upper and lower case letters should be equivalent. Your program should not treat them as distinct tokens.

The order of shared letters is not a consideration for this program.

Write the contents of the wordsIntersect array to a Comma Separated Values (CSV) text file named wordsIntersect.csv in row-major form: row,col,value. In the example below, there is an entry in wordsIntersect[12][33] which contains the string "tci". There would also be a line in wordsIntersect.csv that contains 12,33,tci -- there are no quotes around the letters and the line ends after the letter 'i'. Given that wordsIntersect[12][78] will contains "" there would be a line in the file containing: 12,78, where there is an end of line after the last comma.

N.B.

Example of what sharedLetters() output.

  • SharedLetters(transaction,dictum) returns "tci"
  • SharedLetters(Transaction,kudzu) returns ""

If "transaction" is stored in wordArray[12] and "dictum" is stored in wordArray[33] then wordsIntersect[12][33] will contain "tci".

If "kudzu" is stored in wordArray[78] then wordsIntersect[12][78] will contain "".

My actual code

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
using std::string;

//calling function that will record shared letters between words, to be called later
string sharedLetters(string S1[], string S2[]);

int main ()
{
    //variables for indexes used in arrays
    int i, compareIndex, count = 0;
    
    //declaring array and its total size based on total words in file
    const int N = 644;
    string wordArray[N];
    
    //declaring a 2-D array wordsIntersect, setting size of both dimensions to 'N'
    string wordsIntersect[644][644];
    
    //declaring file
    ifstream file;
    
    //opening file
    file.open("wordlist.txt");
    cout << "Reading data from the file." << endl;
    
    while (count < N)
    {
        file >> wordArray[count];
        count++;
    }

    //closing the file
    cout << "Closing the wordlist.txt file now." << endl;
    file.close();

    //sort the array of strings in descending order using SelectionSort
    //going through array of strings, wordArray[]
    for (int i = 0; i < N; i++)
    {
        //creating a nested for loop to compare indexes
        for (int compareIndex = i + 1; compareIndex < N; compareIndex++)
        {
            //compare the words at both indexes to determine which one is longer
            if (wordArray[i].length() < wordArray[compareIndex].length())
            {
                //if true, swap the elements using selectionSort
                string longerWord = wordArray[compareIndex];
                
                //swap indexes i and compareIndex
                wordArray[compareIndex] = wordArray[i];
                wordArray[i] = longerWord;
            }
        }
    }
    
    //calling sharedLetters function
    for(i = 0; i < N; i++)
    {
        for(compareIndex = 0; compareIndex < N; compareIndex++)
        {
            string sharedLetters(wordArray[i], wordArray[compareIndex]);
        }
    }
    string word = string sharedLetters(wordArray[i], wordArray[compareIndex]);
    cout << word << endl;
    
    //compare every word in wordArray to every other word within the array, using the sharedLetters funtion 
    for (int i = 0; i < N; i++)
    {
        for (int compareIndex = i + 1; compareIndex < N; compareIndex++)
        {
            //saving a string result within the main function that will equal
            //the return value of the called function in the previous step
            wordsIntersect[i][compareIndex] = sharedLetters(wordArray[i], wordArray[compareIndex]);
        }
    }
    
    for (int i = 0; i < N; i++)
    {
        for (int compareIndex = i + 1; compareIndex < N; compareIndex++)
        {
            cout << wordArray[i] << "," << wordArray[compareIndex] << " " << wordsIntersect[i][compareIndex] << endl;
        }
    }
    return 0;
}
    
    //setting up 2D array wordsIntersect
    for (int i = 0; i < N; i++)
    {
        for (int compareIndex = 0; compareIndex < N; compareIndex++)
        {
            wordsIntersect[i][compareIndex];
        }
    }
    
    
    //writing the data in 2-D array into file wordsIntersect.csv
    cout << "Now opening the file wordsIntersect.csv." << endl;
    ofstream outputFile;
    outputFile.open("wordsIntersect.csv");
    cout << "Inputting sorted array wordsIntersect within file, now." << endl;
    outputFile << wordsIntersect[i][compareIndex];
    
    //closing file
    outputFile.close();
    cout << "Data has successfully been entered into the file. The file is now closed." << endl;
    
    return 0;
}

//function that will identify common letters between each word
string sharedLetters(string S1[], string S2[])
{
    //declaring string for sharedLetters
    string sharedletters = "";
    
    //declaring a string for words with common letters
    string commonLetter = "";
    
    //nested for loop to compare letters in S1 and S2
    for (int i = 0; i < S1.length(); i++)
    {
        for (int compareIndex = 0; compareIndex < S2.length(); compareIndex++)
        {
            //comparing letters in strings
            if(S1[i] == S2[compareIndex])
            {
                //assigning the letter at index i whenever common is found
                commonLetter = S1[i];
                //adding the value of commonLetter to sharedletters
                sharedletters = commonLetter;
                break;
            }
        }
    }
    //return string of sharedletters
    return sharedletters;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
LARod92
  • 29
  • 1
  • 2
    Since this is an Intro court -- they are probably required for the project, +1 for the good effort and providing code on your first post. – David C. Rankin Apr 07 '21 at 06:06
  • Read a good [C++ programming book](https://www.stroustrup.com/programming.html). See [this C++ reference](https://en.cppreference.com/w/cpp) and the documentation of your compiler (e.g. [GCC](http://gcc.gnu.org/)...). Take inspiration from the source code of *existing* open source C++ projects like [FLTK](http://fltk.org/), [Qt](https://qt.io/), [RefPerSys](http://refpersys.org/), [fish](https://fishshell.com/) etc.... Use also the [GDB](https://www.gnu.org/software/gdb/) debugger – Basile Starynkevitch Apr 07 '21 at 06:13
  • first comment: you say you have trouble with sharedLetters function: it should take in input two strings s1 and s2, and this is how you defined it: string sharedLetters(string S1[], string S2[]) do you see nothing strange here? What your function is actually getting in input? – Mauro Dorni Apr 07 '21 at 06:14
  • 1
    Always compile with *warnings enabled*, and **do not** accept code until it *compiles without warning*. To enable warnings add `-Wall -Wextra -pedantic` to your `gcc/clang` compile string (also consider adding `-Wshadow` to warn on shadowed variables). For **VS** (`cl.exe` on windows), use `/W3`. In your case you have multiple shadowed variables, `i`, `compareIndex`. You have errors. no matching function for call to `std::basic_string(std::__cxx11::string&, std::__cxx11::string&)’` and many more. You have `for (int i = 0; i < N; i++)` OUTSIDE `main()`??? – David C. Rankin Apr 07 '21 at 06:15
  • Also note, you have two `} return 0;` sets of lines that looks like you meant to add to `main()` and just started adding code below it, and then added another `} return 0;` forgetting to remove the first.... – David C. Rankin Apr 07 '21 at 06:28
  • Fixing your declaration of `int i, compareIndex, count = 0;` (e.g. make it `int count = 0;` to avoid all redeclarations in `for (int i = 0; ..)` and `for (int compareIndex = i + 1; ...)`) Be consistent. Remove the first `} return 0;` and the loop `//setting up 2D array wordsIntersect` that follows (you already did that). For all function calls, remove the `type` from the right size of `=`, e.g. `string word = sharedLetters(...)` NOT `string word = string sharedLetters(...)` In the `shareLetters()` function, use `for (size_t i = 0; ...)` and `for (size_t compareIndex = 0; ...)` A good start. – David C. Rankin Apr 07 '21 at 06:52
  • Note `using namespace std;` covers `using std::string;`, but see [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Apr 07 '21 at 06:54

0 Answers0