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" .
#include <iostream>
#include <string>
#include <math.h>
#include <array>
#include <string.h>
usingnamespace 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;
}
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.
#include <iostream>
#include <string>
usingnamespace 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;
}