HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Determine number of even, odd, and zero digits of a given integer

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberdigitsdetermineandevenzerogivenoddinteger

Problem

I have been programming in C++ for around a month now, just long enough, I figure, to develop bad habits. Can anyone point out where I'm making mistakes and offer topics I should investigate to understand why?

```
#include
#include
#include
#include

using namespace std;
string get_line();
int long get_integer();
int* flex(long int num);
void test_flex();

int main()
{
//flex(get_integer());
test_flex();
}

/*
* === FUNCTION =============================================================
* Name: flex
* Description: flex takes one integer argument and returns an then number
* of odd, even and zero digits.
* flex(111022) -> [3,2,1] i.e [odd,even,zero's]
* ============================================================================
*/
int*
flex (long int num)
{
int res;
int even = 0;
int odd = 0;
int zero = 0;

while(num != 0){
res = num % 10;
if(res == 0){
zero++;
}
else if(res % 2 == 0){
even++;
}
else{
odd++;
}
num = (num - (num % 10))/10;
}
int oez[3] = {odd,even,zero};
return oez;
} / ----- end of function flex ----- /

/*
* === FUNCTION =============================================================
* Name: test_flex
* Description: test suite for flex function
* ============================================================================
*/
void
test_flex ( )
{
assert(flex(1)[0] == 1);
assert(flex(1)[1] == 0);
assert(flex(1)[2] == 0);

assert(flex(112220)[0] == 2);
assert(flex(112220)[1] == 3);
assert(flex(112220)[2] == 1);

} / ----- end of function test_flex ----- /

/*
* === FUNCTION ======================================================================
* Name: get_integer
* Description: checks if input is integer
* =====================================================================================
*/
l

Solution

Do not see the point in: get_line()

get_line(line);

// Vs

std::getline(std::cin, line);


Get Integer is well done. Most people forget to test if there is anything else remaining on the line.

A simple enhancement is:

char remaining;
        if(converter >> remaining)

        // easier to rewrite as:
        if (!converter.str().empty())


Also there is already a boost function that does something similar:

long value = boost::lexical_cast(line); // Will throw if anything left on line.


Over complicating this:

num = (num - (num % 10))/10;


You can simplify it too:

num /= 10;  // Integer division truncates.


I think this is a logical error:

if(res == 0){
        zero++;
    }
    else if(res % 2 == 0){ // zero is an even number
                           // do you really want that else?


Your one major error is:

int oez[3] = {odd,even,zero};
return oez;


You are returning a pointer to an array that has gone out of scope. Personally I would return a std::vector. Don't worry on a simple returning like this the copy back of the array will be optimized out (look up RVO and NVRO).

std::vector oez = {odd,even,zero};
return oez;

Code Snippets

get_line(line);

// Vs

std::getline(std::cin, line);
char remaining;
        if(converter >> remaining)

        // easier to rewrite as:
        if (!converter.str().empty())
long value = boost::lexical_cast<long>(line); // Will throw if anything left on line.
num = (num - (num % 10))/10;
num /= 10;  // Integer division truncates.

Context

StackExchange Code Review Q#17536, answer score: 8

Revisions (0)

No revisions yet.