-8

i was solving a problem and the describtion is: Your classmates asked you to copy some paperwork for them. You know that there are 'n' classmates and the paperwork has 'm' pages.

Your task is to calculate how many blank pages do you need. If n < 0 or m < 0 return 0.

so when i write my code like that

int paperwork(int n, int m){
  return (n>0,m>0)?m*n:0;
}

it says Should_work_for_negative_n Expected: equal to 0 Actual: -25

Menna
  • 1
  • 10
    Do you know what the comma operator actually does? Because this is definitely not a case for it – UnholySheep Aug 31 '23 at 15:26
  • 7
    Also note that the opposite of `n < 0` is *not* `n > 0`. That should have been taught in basic math lessons. – Some programmer dude Aug 31 '23 at 15:29
  • `(n>0,m>0)` is the same as `(m>0)` - try `return (n>0 && m>0) ? m*n : 0;` - or if the number of pages isn't allowed to be negative: `return n>0 ? m*n : 0;` – Ted Lyngmo Aug 31 '23 at 15:32
  • 5
    Looks like you are not learning c++ from a good source. Good sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Aug 31 '23 at 15:49
  • This seems to be an exercise about [logical operators](https://en.cppreference.com/w/cpp/language/operator_logical), so the [comma operator](https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator) is wrong, but you could also write `return std::max(0, n) * std::max(0, m);`. – Bob__ Aug 31 '23 at 16:00

0 Answers0