I want to know why my following code (or parts of my tests don't compile right)
CODE
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
int tally(const string a[], int n, string target);
// Return the number of strings in the array that are equal to target.
int tally(const string a[], int n, string target)
{
int result;
if (n < 0)
{
result = -1;
return result;
}
else if (n==0)
{
result = 0;
return result;
}
else
int result = 0;
for (int i=0; i<n; i++)
{
if (target == a[i])
result += 1;
else
result;
}
return result;
}
int findFirst(const string a[], int n, string target);
int findFirst(const string a[], int n, string target)
// Return the index of the first string in the array that is equal to target.
// Return -1 if there is no such string.
{
int result = -1;
if (n <= 0)
return result;
else
for (int i=0; i<n; i++)
{
if (a[i] == target)
result = i;
break;
}
return result;
}
int main()
{
string a[6] = { "lois", "peter", "", "meg", "", "meg" };
assert(tally(a,6,"meg") == 2);
// assert(tally(a,6,"") == 2);
// assert(tally(a,6,"chris") == 0);
assert(tally(a,0,"lois") == 0);
// assert(findFirst(a,6,"meg") == 3);
// assert(findFirst(a,3,"meg") == -1);
cout << "All tests succeeded" << endl;
}
CODE
assert(tally(a,6,"meg") == 2)
passes the test,
but why don't the following?
CODE
assert(tally(a,6,"chris") == 0;
assert(tally(a,6,"") == 2);
This post has been edited by aznballerlee: 4 Nov, 2006 - 04:52 PM