Crossword Help

Hi I have some programming homework and I've got the main idea but i cant seem to get the strings to print out properly.

This was the question.
You are to write a program that allows two words to cross at that common letter. Your input will consist of two words. Print the two words crossing on the common letter in crossword fashion. The first word entered will be the horizontal word and the second the vertical word. Your program will stop execution when one or both of the works is "q" .

Here is my code.

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <math.h>
#include <array>
#include <string.h>

using namespace std;

int main()
{
    int a,b,x;
    int downum;
    int acnum;
    string across="",down=""; //create string variables
    cout << "Input the Across word: "; //ask for user input
    getline(cin, across); //get user input
    cout<<across<<endl; //display what they typed out
    cout << "Input the Down word: "; //ask for user input
    getline(cin, down); //get user input

    if(across=="q" && down=="q") //if user inputs q for down word and q for across
    {

        cout<<"Goodbye!"; //display good bye message
    }
    else //if anything else is entered
    {
        bool check_intersect;
        check_intersect =false;
        int len=down.length(); //get length of the down word
        int lenz=across.length(); //get length of the down word
        char cc;
        char c;
        cout<<"down word"<<len<<endl;
        cout<<"across word"<<lenz<<endl;
        for (int i=0; i<len; i++)
            for(int j=0; j<lenz; j++)  //for all the values less than the length of the across word
                if (c = down.at(i) == across.at(j))
                {
                    a = i;
                    b = j;
                }
        cout<<across;
        if (a==0 && b==0)
        {
            for (int i=1; i<len; i++)
            {
                cin.ignore(1);
                cout<<down.at(i);
            }
        }
    }

    return 0;
}
Minor point: The instructions say
when one or both of the works is "q" .

Line 21 has an AND condition, requiring both words to be "q".

Line 44: If the first letter of each word matches, a and b are going to be zero, therefore you won't print anything. I assume you're trying to detect if no match was found. If that's the case you should initialize a and b to -1 and then test for (a == 1 && b == -1).

Line 48: not needed

As you're print the down word, you want to indent a spaces except for a == b.
When (a == b), then you want to print the across word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>

using namespace std;

int main()
{   int a = -1, b = -1;
    string across, down; //create string variables
    
    cout << "Input the Across word: "; //ask for user input
    getline(cin, across); //get user input
    cout << "Input the Down word: "; //ask for user input
    getline(cin, down); //get user input

    if(across=="q" || down=="q") //if user inputs q for down word and q for across
    {   cout<<"Goodbye!"; //display good bye message
        return 0;
    }
    bool check_intersect = false;
    size_t dlen = down.length(); //get length of the down word
    size_t alen = across.length(); //get length of the down word
    
    for (size_t i=0; i<dlen; i++)
        for (size_t j=0; j<alen; j++)  //for all the values less than the length of the across word
            if (down[i] == across[j])
            {   a = i;
                b = j;
            }    
    if (a == -1)
    {   cout << "No matching letter found" << endl;
        return 0;
    }  
    for (size_t i=0; i<dlen; i++)
    {   if (i == a)
        {   //  print across word
            for (size_t j=0; j<alen; j++)
                cout << across[j];
        }
        else 
        {   //  Indent b times
            for (size_t j=0; j<b; j++)
                cout << '.';
            cout << down[i];                
        }
        cout << endl; 
    }
    system ("pause");
    return 0;
}

Last edited on
THANK YOU SO MUCH!
Topic archived. No new replies allowed.