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 <iostream>
#include <cassert>
#include <sstream>
#include <string>
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
* =====================================================================================
*/
long int
get_integer ()
{
while(true){
stringstream converter;
converter << get_line();
int long result;
if(converter >> result){
char remaining;
if(converter >> remaining)
cout << "Unexpected character: " << remaining << endl;
else{
return result;
}
} else
cout << "Please enter an integer." << endl;
cout << "Retry:" << endl;
}
} /* ----- end of function get_integer ----- */
/*
* === FUNCTION ======================================================================
* Name: get_line
* Description: wraps getline
* =====================================================================================
*/
string
get_line()
{
string result;
getline(cin,result);
return result;
} /* ----- end of function get_line ----- */