Reputation: 928
This is a group assignment and it's become rather difficult to the point our professor has extended the project by 1 week. there are 50 stages/tests, we've only been able to reach up to stage 11 and then the function fails.
this function is in our .cpp file (we're positive it's this function causing the problem's because when we change parts of it, it affects stage 11 which we've passed).
int segment::match(const char word[]) {
int i;
cout << data[0];
data[0] == "OOP";
cout << data[0];
for(i=0;i<NUM_MAX;i++) {
cout << "word = " << &word[i] << " data[i] = " << data[i];
if(strstr(&word[i],data[i])!= NULL)
break;
}
return i==NUM_MAX ? 1 : i-1;
and from the main.cpp (provided to us as the assignment) this is the what we are trying to accomplish
Passed test 11...
Your match( ) return value ----> -1
Actual match( ) return value --> -1
Press the ENTER key to continue...
word = OOP data[i] =
Failed while testing the match( )
function... Failed on test 12...
Your match( ) return value ----> -1
Actual match( ) return value --> 1
Press the ENTER key to continue...
You passed 11/50 tests...
Your program is 22.00% complete!
Your program still needs some work!
Keep at it!
what the function is suppose to do is check for "oop" and if it isn't there it exits with -1, and if it is there it should return true with 1.
I guess what I'm asking is how do I make that function that it returns both -1 and 1 in the proper order?
If you would like access to the main.cpp and segement.cpp i can upload that as files somewhere because they're very long and I did not want to cram the post.
Any help is appreciated, thank you.
EDIT* Here is the full code that we have http://jsfiddle.net/h5aKN/
The "html" section has the segement.cpp which is what we built. and the jscript section has the a2main.cpp which is what our professor built.
Upvotes: 0
Views: 194
Reputation: 5569
data[0] == "OOP";
is probably not what you want to do. The double =
(==
) tests for equality, so here you're testing if the item at the first index of data
(data[0]
) and the character string "OOP"
are equal.
In the running of the test, you are cout'ing: word = OOP data[i] =
, which means that word[i]
is probably defined correctly, but data[i]
is not. This goes back to the usage of the equivalence test above.
If you set initialize data
correctly, (correctly meaning allocating the memory correctly, I don't know where data
is instantiated), then the test would likely return -1, as it would get a non NULL pointer from the strstr()
call (assuming that data
is the correct type), i
would be 0
on break
ing, and the ternary operator would yield i-1
, = -1
.
So fix the initialization / assignment of the data
variable
And if you're not limited to c style strings (char arrays), I'd use the std::string type and its associated methods (see the c++ string reference if you haven't already). Usually much nicer to work with
Upvotes: 1
Reputation: 264391
If you are passing a list of words to the function:
As the use of (strstr(&word[i],data[i])
) suggests you are looking for a string inside another string. Thus you are looping over a list of strings (words).
Then this looks wrong:
int segment::match(const char word[]) {
Here you are passing one word.
Its impossable to tell what it should be, but a guess would be:
int segment::match(const char* word[]) {
// ^^^^^
But to be honest the whole thing is rather ugly C++. If you were writting C I would say fine, but if you had been writing C++ correctly the type system would have saved you from all these problems. Use std::string to represent words.
Upvotes: 0