the user inputs two strings to compare. The first string is the matching string and the second string is the string that will compared with the first string. the first character in the second string could be anything; The rest have to be in order. for example, the matching string is net and the comparing string is xet. The program returns net.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string word1[81]={" "};
string word2[81]={" "};
cout << "Enter two words and I will compare them to see if there ";
cout << "is a match. " << endl;
cout << "*\nNote, the first character in the second word could be ";
cout << "anything. " << endl;
getline(cin, word1[81]);
getline(cin, word2[81]);
word2[0].swap(word1[0]);
cout << word1 << endl;
cout << word2 << endl;
return 0;
}
because remember, each element(or character) except the first has to match. So I want to step through each element in the string array to check if there is match. This is also why I'm swapping the first element in the second string with the first element in the matching string since the first character could be anything. Regardless, the problem that I have is that it's crashing.
well that says it all then. You want to declare an array of characters rather than an array of strings then?
I think you're getting confused between an array of characters (i.e. ONE string), which I think you need, with an array of strings (which you've defined here).
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string word1[81];
cout << "Enter two words and I will compare them to see if there ";
cout << "is a match. " << endl;
cout << "\n*Note, the first character in the second word could be ";
cout << "anything. " << endl;
getline(cin, word1[81]);
cout << word1;
return 0;
}
you are asking the user to type in a string and put it in the 'last' element of your string array, but remember if you declare an array with 81 elements, you access them from word1[0] to word1[80]. word[81] does not exist.
in other words it's not liking it because you're trying to write to memory that's not your array.