victor victor - 1 month ago 124
C++ Question

Returning a number

I'm still very new to C++, and seem to have an issue. When I run my program, my

getInt()
doesn't seem to be returning the private data member,
myInteger
, in my
print()
method. It just returns 0, but the rest of my method calls in
print()
are returning the appropriate number. I've also tried to just type in
myInteger
instead of
getInt()
, but that still comes back with 0. I know I did something wrong, but I'm not really sure what.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;

class myInt {
public:
myInt();
myInt(int newInt);

void print();
void setInt(int);

int getInt();
int sumDigits();
int reverse();

int oddDigitCount();
int evenDigitCount();
int zeroCount();

private:
int myInteger;
};

int main()
{
srand(815);

myInt theNum;
for (int i = 0; i != 10; i++) {
theNum.setInt(rand());
theNum.print();
}

return 0;
}

myInt::myInt()
{
myInteger = 0;
}

myInt::myInt(int newInt)
{
setInt(newInt);
}

void myInt::setInt(int newInt)
{
if (newInt > 0)
myInteger = newInt;
else
myInteger = abs(newInt);
}

int myInt::getInt()
{
return myInteger;
}

int myInt::sumDigits()
{
int sum = 0, rem;
while (myInteger != 0) {
rem = myInteger % 10;
sum += rem;
myInteger /= 10;
}
return sum;
}

int myInt::reverse()
{
int reversed = 0;
int quo, rem;
quo = myInteger;
while (quo != 0)
{
rem = quo % 10;
reversed = reversed * 10 + rem;
quo /= 10;
}
return reversed;
}

int myInt::oddDigitCount()
{
return 0;
}

int myInt::evenDigitCount()
{
return 0;
}

int myInt::zeroCount()
{
return 0;
}

void myInt::print()
{
cout << "The number is " << getInt() << endl
<< "----------------------------------------" << endl
<< "Adding the digits result" << setw(16) << sumDigits() << endl
<< "Reversing the digits result" << setw(13) << reverse() << endl
<< "Odd digits" << setw(30) << oddDigitCount() << endl
<< "Even digits" << setw(29) << evenDigitCount() << endl
<< "Zeros" << setw(35) << zeroCount() << endl
<< endl;
}


Output for first two iterations of for loop,


The number is 0




Adding the digits result 9

Reversing the digits result 72

Odd digits 0

Even digits 0

Zeros 0

The number is 0




Adding the digits result 20

Reversing the digits result 41681

Odd digits 0

Even digits 0

Zeros 0

Answer
int myInt::sumDigits()
{
    int sum = 0, rem;
    while (myInteger != 0) {
        rem = myInteger % 10;
        sum += rem;
        myInteger /= 10;  // <-- HERE
    }
   return sum;
}

This code resets myInteger back to 0 before you print it.

You call it as part of this statement:

cout << "The number is " << getInt() << endl
     << "----------------------------------------" << endl
     << "Adding the digits result" << setw(16) << sumDigits() << endl
     << ...

The compiler apparently has chosen to evaluate the call to sumDigits before the call to getInt (C++ leaves this order unspecified), so by the time getInt looks at myInteger, it's already been set to 0 by sumDigits.

To fix this, do the same thing you do in reverse: Create a local variable (such as quo), which you can modify without affecting anything else.