Assert and Void Help

Need help. It says there is a problem with my asserts (test) and how do I fix the average function.

void test(){
assert(findSum(1, 1) - 2);
assert(findSum(2, 2) - 4);
assert(findDifference(1, 1) - 0);
assert(findDifference(2, 1) - 1);
assert(findProduct(1, 1) - 1);
assert(findProduct(2, 2) - 4);
assert(findQuotient(1, 1) - 1);
assert(findQuotient(4, 2) - 2);
assert(findLarger(2, 1) - 2);
assert(findLarger(3, 4) - 4);
assert(findSmaller(1, 2) - 1);
assert(findSmaller(5, 3) - 3);
//assert(findAverage(2, 4) - 3);
//assert(findAverage(6, 6) - 6);
printf("All test cases passed...\n");
}
Last edited on
If the value passed into assert evaluates to true, nothing happens.
If the value passed into assert evaluates to false, stderr receives an error message and the program is aborted.

Anything non-zero in C++ is evaluated as true.
Anything zero is evaluated as false.

assert(findSum(1, 1) - 2);
findSum(1,1) returns 2.
2 - 2 returns 0.
assert(0) will abort your program, which I presume you don't want.

Change your statements to something like:
assert(findSum(1,1) == 2);

Also note the warnings in your code.

  In function 'double findSum(const double&, const double&)':
147:8: warning: variable 'sum' set but not used [-Wunused-but-set-variable]
  In function 'double findDifference(const double&, const double&)':
153:8: warning: variable 'difference' set but not used [-Wunused-but-set-variable]
  In function 'double findProduct(const double&, const double&)':
160:8: warning: variable 'product' set but not used [-Wunused-but-set-variable]
  In function 'double findQuotient(const double&, const double&)':
195:8: warning: variable 'quotient' set but not used [-Wunused-but-set-variable] 


Last edited on
Thank you! I knew it was something simple. And then what about the findAverage function? I need help with getting that to work... Specifically case 7
Last edited on
1
2
3
4
5
6
case 7:
    getTwoNumbers(n1, n2);
    double avg;
    printf("(%.2f + %.2f) / %.2f = %.2f\n", n1, n2, n3, avg);
    break;
}


You never assign avg a value.

Hope that helps :)
Perfect! Okay and then how would I do assert for the findAverage function?
Assert(findAverage(6, 4, avg?) == 5);
Your findAverage function is putting the "result" into the third variable.
As you have it now, its return type is void. That means you shouldn't be trying to compare it to 5.

Either change findAverage so that it returns a double:
1
2
3
double findAverage(const double &n1, const double &n2){
    return findSum(n1, n2) / 2;
}

assert(findAverage(6,4) == 5);

or do something like:
1
2
3
double avg;
findAvg(6, 4, avg);
assert(avg == 5);
Last edited on
Topic archived. No new replies allowed.