How to fix redefinition of formal parameter

Apr 11, 2020 at 6:39pm
Hello all I hope you are doing great, I am a new to Programming and decided to create a bool function for Armstrong number, however I don't quite understand the error I am getting "Redefinition of formal Parameter"
Here is the code as follows, thank you all.

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
  #include <iostream>

using namespace std;

bool Armstrong(int origNum);



int main()
{
    cout << Armstrong(153) << " ";
}


bool Armstrong(int origNum) {

    int origNum, num, rem, sum = 0;
    bool Armstrong;
    cout << "Enter a positive  integer: ";
    cin >> origNum;

    num = origNum;

    while (num != 0)
    {
        rem = num % 10;
        sum += rem * rem * rem;
        num /= 10;
    }

    if (sum == origNum)
        Armstrong = true;
    else
        Armstrong = false;

    return origNum;
}
Apr 11, 2020 at 6:47pm
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:54pm
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 8:01pm
You used code tags with your first code post, why did you not use them for the second post?

int origNum, num, rem, sum = 0;
4 variables created, only one is given an initial value; sum. The other three are uninitialized.

Create each variable individually and assign as value on creation.
1
2
3
4
int origNum = 0;
int num = 0;
int rem = 0;
int sum = 0;


or

int origNum = 0, num = 0, rem = 0, sum = 0;


As jonnin mentioned: Do not create a new variable in a function body that uses the same name as a passed parameter!

Your function should look like:
1
2
3
4
5
6
7
8
bool Armstrong(int origNum)
{

    int num = 0, rem = 0, sum = 0;
    bool Armstrong;

    // the rest of the code
}

Apr 11, 2020 at 9:25pm

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
Topic archived. No new replies allowed.