Output isn't showing a number I need

I have this code completed. But it isn't showing me my first number that I know exist within these parameters. I should have 4 numbers displayed but I only receive the last 3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 #include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    ofstream digits;
             digits.open("SumOfDigits.out");

    int number, sum, remainder, starting_value = 100;

        digits << " The Armstrong Numbers up to 450\n";

        while(starting_value <= 450)
        {
            number = starting_value;
            sum = 0;

            while(number > 0)
            {
                remainder = number % 10;
                sum += pow(remainder,3.0);
                number /= 10;
            }
                if(sum == starting_value)
                {
                    digits << "\t\t" << starting_value
                           << endl;
                }

            starting_value++;
        }

digits.close();
return 0;
}


The output should be as follows: 153, 370, 371, 407.
? I'm getting the numbers that you wrote as output.
  The Armstrong Numbers up to 450
		153
		370
		371
		407


Btw, digits.close() is not needed. That is done automatically by the ofstream's destructor. (Personally I would just print to cout and then redirect it to a file if needed).

You can also change your constructor to just
ofstream digits("SumOfDigits.out");

Last, in general it's cleaner to declare a variable only in the scope that it's needed.
I would declare number, sum, remainder all inside the while loop, because that's where they're only used.
Last edited on
Since you are writing all the output to a file you could post here the contents you get.

I get the same results Ganado does, your expected output.
Another take on your program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>

bool is_armstrong_number(int n)
{
  int const original_n = n;
    
  int sum = 0;
  do sum += std::pow((n % 10), 3); while (n /= 10); 
    
  return original_n == sum;
}

int main()
{
  for (int i = 100; i <= 450; ++i)
    if (is_armstrong_number(i))
      std::cout << i << '\n';
}
Last edited on
I'm unsure if I am doing something wrong, or if my computer is wrong. But you all are saying you've printed the results just fine and you've shown it. But I am still only getting the last 3 numbers the 153 is not on my list.
Try writing to cout instead of digits and see if you get the expected output. If you see 153 when using cout there are a number of possible issues. If you don't, there's a different set of possible issues. Report back here and we'll help you troubleshoot.
Last edited on
In addition to what mbozzi said, try doing std::round(pow(remainder,3.0)) instead of just the pow to see if that changes the result. I don't think this should make a difference though.
Last edited on
I have suspicions about the same line as @Ganado. Another alternative is to avoid floating-point round-off altogether and write
sum += remainder * remainder * remainder;

You seem to be obsessed with reading/writing everything through files, but you can do a fudge whilst debugging: comment out the three lines concerned with declaring, opening, closing digits as a filestream and temporarily replace with
ostream &digits = cout;



Just for fun ...

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int sumCubes( int N )
{
   int remainder = N % 10;
   return N ? sumCubes( N / 10 ) + remainder * remainder * remainder : 0;
}

int main()
{
  for ( int i = 100; i <= 450; i++ ) if ( sumCubes( i ) == i ) cout << i << '\n';
}
Last edited on
@mbozzi, I changed the output to cout and my results were the same.

@lastchance, I am obsessed with writing to a file because that is what the professor has asked us to do. Granted I am very green to the C++ realm, there's a lot I don't know or don't understand. Even the feedback from you kind coders throws me a curveball. I wish I had studied this stuff years ago.
OK I just replaced the line
 
sum += pow(remainder,3.0);

with
 
sum += remainder * remainder * remainder;


and all 4 numbers are showing as they should. What caused this?
Last edited on
In a nutshell... floating-point math.

I don't know specifically what instructions are being called, but basically that implementation of pow is returning a number, very, very close to remainder^3, but in this case, it's returning a number just below remainder^3.

For example (not actual numbers): if remainder^3 is 5*5*5 = 125, it's returning 124.9999999.

When 124.9999999 is assigned to an integer, it is truncated to 124.

Try printing the result of
(pow(remainder, 3.0) < remainder * remainder * remainder)
Is it 1 (true)?

Bottom Line: Be very careful whenever you're converting something from a floating-point number back to an integer. When in doubt, don't use floating-point calculations for integers, and if you do, then round before assigning to an integer.
Last edited on
Topic archived. No new replies allowed.