Apr 11, 2020 at 6:47pm Apr 11, 2020 at 6:47pm UTC
bool Armstrong(int origNum ) {
int origNum ,
don't do that ^^
you tried to recreate a second 'variable' of the same name (parameters are not exactly variables, but you get the idea).
Last edited on Apr 11, 2020 at 6:47pm Apr 11, 2020 at 6:47pm UTC
Apr 11, 2020 at 6:54pm Apr 11, 2020 at 6:54pm UTC
Hi I did what you did and would like to thank you for that.
Can you check what goes wrong in my error here? it says uninitialized local variable 'orignum'
#include <iostream>
using namespace std;
bool Armstrong(int);
int main()
{
for (int i = 1; i < 500; ++i) {
cout << Armstrong(i) << " ";
}
}
bool Armstrong(int) {
int origNum, num, rem, sum = 0;
bool Armstrong;
num = origNum;
while (num != 0)
{
rem = num % 10;
sum += rem * rem * rem;
num /= 10;
}
if (sum == origNum) {
Armstrong = true;
cout << sum << " ";
}
else {
Armstrong = false;
}
return origNum;
}
Apr 11, 2020 at 9:25pm Apr 11, 2020 at 9:25pm UTC
int origNum, num, rem, sum = 0;
bool Armstrong;
num = origNum;
origNum has no vaue here. num now has a copy of the junk value. The compiler is telling you this is no good.
you have to be explicit:
int origNum = 0, num = 0, rem = 0, sum = 0;
Last edited on Apr 11, 2020 at 9:26pm Apr 11, 2020 at 9:26pm UTC