-2

Im new to c++ and im trying to get a bool* function with a dynamic array as its parameter and its size to return true or false when none of them is zero but im getting an error: cannot convert 'bool' to 'bool*' in return.

bool* noneZero(int *zero, int N) {
    int PL = 0;
    for (int i = 0; i < N; i++) {
        if (i == 0) {
            PL++;
        }
    }
    if (PL == N)
        return false; //cannot convert 'bool' to 'bool*' in return
    else
        return true; //cannot convert 'bool' to 'bool*' in return
}
        


int main(int argc, char** argv) {
    int *z=new int[5]{0,0,0,0,0};
    cout << noneZero(z,5);
}

Also the question is how the teacher gave it to me. we dont work with vectors. Basically i have to return false when all of the numbers in my dynamic array are 0 and true when they arent. My question is why i get an error: cannot convert 'bool' to 'bool*' in return

Gary Strivin'
  • 908
  • 7
  • 20
Catharsis
  • 3
  • 3
  • 3
    there are too many pointers in your code. It appears you simply want `bool result = noneZero(z,5);` and change the return type to `bool` – 463035818_is_not_an_ai Dec 23 '20 at 14:17
  • 2
    If you're only trying to return true or false, why have you declared the function to return a `bool *`? And why are you using the return value from `nonZero()` in `main()` as if it's an array of length (at least) 5? – scohe001 Dec 23 '20 at 14:17
  • 1
    for dynamic arrays use `std::vector`. Do you want to return a single boolean from the function or 5 of them? – 463035818_is_not_an_ai Dec 23 '20 at 14:18
  • Unrelated to your error, but your function basically ends with `if (PL == N) { return false; } else { return true; }`. And any code following that pattern could be replaced by a simple `return` statement. In your case `return PL != N;`. – Some programmer dude Dec 23 '20 at 14:22

2 Answers2

2

Frankly, there are many problems in your code and I suggest to start from scratch.

First, dynamic arrays in C++ are std::vector. You need a good reason to use something else.

Next, your function is too complicated. You need not count the number of zeros if all you want is to check if there is none or at least one:

bool noneZero(const std::vector<int>& in) {
     for (size_t i=0; i< in.size(); ++i) {    // a vector "knows" its size !
         if ( in[i] == 0 ) return false;      // no need to search further
     }
     return true;
}

int main() {
    std::vector<int> z{1,2,3,4,5};
    bool x = noneZero(z);
    if (x) std::cout << "there is no 0 in the vector\n";
    for (const auto& e : z) {
         std::cout << e << " ";
    }
}

A std::vector manages the memory for you, ie no need for manual new or delete. It is not clear why you use the pointer returned from the function as if it points to an array (it does not and your code has undefined behavior). I added a loop that prints the vectors elements.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

Problem:

  1. A bool* must return a pointer a pointer to a bool, not a bool itself.
  2. You're checking the value of i, not the values of the array.

Solution:

  1. Change the function from a bool* to a bool.
  2. Change i == 0 to *(zero + i) == 0.

Additional information:

  1. Seems like you're using using namespace std;. using namespace std; is considered a bad practice (More info here).
  2. You probably should use std::vector if you can.

Full code:

#include <iostream>

bool noneZero(int *zero, int N) {
    int PL = 0;
    for (int i = 0; i < N; i++) {
        if (*(zero + i) == 0) {
            PL++;
        }
    }
    if (PL == N)
        return false;
    else
        return true;
}

int main(int argc, char** argv) {
    int *z = new int[5]{0,0,0,0,0};
    std::cout << noneZero(z,5);
    delete[] z;
}

Gary Strivin'
  • 908
  • 7
  • 20