Hello VOLTS100,
I loaded your program and found that "ProblemSolve" does not work the way you might be thinking.I discovered that the first for loop is trying to set "A" to "U" and it is not working and what does work sets "S" in the reverse order of "U" .Not what you want and "S" should have a value before you reach "ProblemSolve".
The idea of "ProblemSolve" is to compare the two strings. The for loop should compare not set something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
bool PuzzleSolve(int k, char* S, char* U)
{
bool equal{ false };
for (int lc = 0; lc < 5; lc++)
{
if (S[lc] == U[lc])
equal = true;
else
equal = false;
}
return equal;
}
|
Since the idea here is to compare you could use "strcmp(S, U);". Requires the header file "cstring" or "string.h". I would use "cstring". Not only does it do the same work it will tell you which string is less. This does require that "S" have a value to work right.
Acording to what the program needs "Input: An integer k, sequence S, and set U". This tells me that the program should start with:
1 2 3
|
int k{ 0 };
std::string S{ "" };
std::string U{ "" };
|
You could use a C style character array if you need to. I would make the size of each 50, so you have enough room to store what is entered.
Something that occurred to me this morning. You define "S" and "U" as having a size of five, e.g.,
char U[5] = { 'a','b','c','d','e' };
, but you are giving the array five characters in positions 0 - 4 which leaves no room for the terminating null character of the string. If you want to store five characters then the size needs to be six to leave room for the "\0" at the end.
When you call "ProblemSolve" the first for loop starts with
int i = 0, j = k
, so when "k" = five you are starting outside the boundaries of the array and will get unpredictable results.
The specs of the program are good if you know how the game is played and what it is about. You might want to start with a function that displays the instructions of how to play. Also a known sample input and output would go along way to figure out the program. Right now I am making guesses that do not seem to work.
Questions. Where did this come from? A homework assignment? Something from a web sight? Is there a web address where I might be able to look at?
Right now I am stuck trying to figure out how this program should work.
Andy