count empty char cells c++

2.1k views Asked by At

For goodness' sake, it's seems such a simple piece of code and I just can't figure out where I went wrong.

int count = 0;
 for (int i = 0; i<10;i++){
    if (chararray[i]=='\0' && i == 0){
        cout << "Empty \n";
        break;
    }
    if (chararray[i]!='\0') {
        count = count ++;
    }
 }
cout << "Deleted " << count << "elements \n";

So the basic idea is that it goes through the array and if it's empty, then returns "Empty" and if not then it counts all the non empty cells and returns how many of them there were. If it makes any difference, I'm putting this under deconstructor method.

Generally it works fine, it just won't COUNT right. It either counts all or none.


UPDATE!

Thank you all! I removed the count = count++ line with ++count and it displayed more correct results than before, but not for all test values (and I promise not to make this same mistake again). As it is, I took the advice to use strlen function as there isn't a specific need for a 0 in place of, well, nothing. Also it made the code much, much shorter. Thank you!

4

There are 4 answers

0
Vlad from Moscow On BEST ANSWER

You titled your question as

count empty char cells c++

however as it is seen in the code snippet the count is increased when an element of the array is not equal to '\0'

if (chararray[i]!='\0') {
    count = count ++;
}

So what is the empty char cell that you are going to count?

Take into acccount that this statement

    count = count ++;

has undefined behaviour because applying the side effect of operaator ++ is not sequenced relative to the left operand assignment.

If the array contains a string and you want to know whether it is empty and how many characters in the string then you should use standard C function strlen

If the array does not contain a string and elements with value '\0' can be in any place of the array then that tp count non-zero elements you should use standard algorithm std::count_if

For example

#include <algorithm>
#include <functional>

//..

int n = std::count_if( chararray, chararray + 10, 
                       std::bind2nd( std::not_equal_to<char>(), '\0' ) );
2
Baldrickk On
  if (chararray[i]=='\0' && i == 0){

This line looks for a null value and then checks if it is at index 0
Try this:

if(chararray[i]==`\0`){

to break on the first null value and stop counting.

Also:

Please please please change this:

 if (chararray[i]!='\0') {
      count = count ++;

It is Undefined Behaviour (UB) and will very likely not be working as intended or break at some later point.

It should be:

if (chararray[i]!='\0') {
      count ++;
0
P0W On

Assuming chararray has type char [] you can simply use std::count

auto num_items = std::count(  std::begin( chararray ),
                              std::end( chararray ), 
                              '\0' 
                            ); // returns 0 or number of occurrence
0
hs_ On

One problem:

count = count ++;

is undefined behavior: it could result in something like...

tmp = count; // count == 0 
count++; 
count = tmp; // count == 0  

or...

tmp = count; // count == 0
count = tmp;
count++;     // count == 1

Try ++count; instead.