I've been working on a code for an Armstrong problem, but when trying to compile the code, I got this error:
prog4a.cpp: In function ‘int main()’:
prog4a.cpp:42:25: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
answer = getRaiseAndAdd(i);
^
prog4a.cpp:42:25: note: (if you use ‘-fpermissive’ G++ will accept your code)
I tried Googling the -fpermissive flag, but could not find any help on how to prevent it from showing as an error. Any suggestions?
#include <iostream>
usingnamespace std;
//Function name: getRaiseAndAdd
//Purpose: Calculate each digit cubed and the sum of the results.
//We are sending in the current number from the loop starting at 100 and
//counting one by one to 999.
//Parameters: &numberInput
//Return value: Result of adding 3 numbers
int getRaiseAndAdd(int &numberInput)
{
int firstNumber, secondNumber, thirdNumber;
int remainder;
int sum;
int firstPower, secondPower, thirdPower;
firstNumber = numberInput / 100;
remainder = numberInput % 100;
secondNumber = remainder / 10;
thirdNumber = remainder % 10;
firstPower = firstNumber * firstNumber * firstNumber;
secondPower = secondNumber *secondNumber * secondNumber;
thirdPower = thirdNumber * thirdNumber * thirdNumber;
return sum = firstPower + secondPower + thirdPower;
}
int main()
{
int answer;
int originalNumber;
for(int i = 100; i < 1000; i++)
originalNumber = i;
answer = getRaiseAndAdd(i);
{
//Function name: is_Armstrong
//Purpose: finding the Armstrong numbers
//Parameters: answer
//Return value: 0
bool is_Armstrong (int answer);
if(answer == originalNumber);
{
cout << "found an Armstrong number " << originalNumber << endl;
}
}
return 0;
}
{
int Y;
for ( int X = 0; X < 42; ++X ) {
// code
}
// X and Y still exist
} // X and Y go out of scope
That did change in the (ISO) C++ standard:
1 2 3 4 5 6 7 8
{
int Y;
for ( int X = 0; X < 42; ++X ) {
// code
}
// X is out of scope; does not exist any more
// Y still exists
} // Y goes out of scope
The compiler has a command line option that makes the compiler forget the standard and operate in the early mode. This allows recompilation of legacy code. If it did not have legacy support and be "helpful", then the error would be undeclared identifier 'i'.
man gcc and look for "fpermissive" within.
(Does Google treat prefix hyphen as logical NOT?)