Home > Software design >  Two programs are same but one is showing error
Two programs are same but one is showing error

Time:12-04

I have two programs. 1:

#include <iostream>

using namespace std;

int main () {

    

    do {

        cout<<"Hello world";

        char yn='y';

    } while (yn=='y' || yn=='Y');

    return 0;

}

2:

#include <iostream>

using namespace std;

int main () {

    char yn='y';

    do {

        cout<<"Hello world";

    } while (yn=='y' || yn=='Y');

    return 0;

}

First program is showing error like this:

comparison between pointer and integer ('double (*)(int, double)' and 'char')

    } while (yn=='y' || yn=='Y');

I think both programs are same but still first program is showing error.

CodePudding user response:

In the first program, the yn variable that you have declared is not in scope in the while loop test

int main () {
    do
    {
        cout<<"Hello world";
        char yn='y';
        // scope of yn variable stops here
    }
    while (yn=='y' || yn=='Y'); // so yn variable not in scope here
    return 0;
}

The reason that you don't get an error that says undeclared variable (or something like that) is because very unfortunately for you there is a POSIX standard function called yn (see here) and the compiler thinks that is what you are referring to in while (yn=='y' || yn=='Y');. That explains the error message, the compiler is interpreting yn as a function pointer.

In your first program try changing the name of the variable (say yn -> yesno) and see what difference that makes to the error message.

CodePudding user response:

The code is not the same. When you declare a variable between {...} it is in scope only between the braces and in this case not in scope in the while condition.

The error is confused by the fact that a different symbol yn (a function) happens to be in scope through indirect inclusion of <cmath> by <iostream> and the ill-advised use of using namespace std; moving the entire standard library into the global namespace.

That is what namespaces are for and it is always a bad idea to defeat an entire namespace. Here either use scope resolution std::cout or just declare the symbols you actually use:

using namespace std::cout ;

In this case, with only one instance if a std:: symbol, the using directive has saved you nothing whilst causing a great deal of confusion. Get out if that habit is my advice.

In the second instance you will still get an error, but it will make much more sense, telling you that yn is undefined at the while expression.

All that said, yn() is not a standard library function, but a POSIX extension; it may therefore not be in the std namespace. You may need also to compile with an option that excludes such extensions such as -ansi.

CodePudding user response:

Code 1

#include <iostream>
using namespace std;

int main () {

    do {

        cout<<"Hello world";        
        char yn='y';

    } while (yn=='y' || yn=='Y');
    return 0;
}

In this code, char yn='y' is inside the exclusive scope of the do-while loop. The object yn is destroyed when the compiler leaves the scope. Now, when the line while (yn=='y' || yn=='Y') is executed, the compiler doesn’t know what yn is, thus causing an error.

Code 2

#include <iostream>
using namespace std;

int main () {

    char yn='y';

    do {

        cout<<"Hello world";        

    } while (yn=='y' || yn=='Y');
    return 0;
}

In this code, char yn='y' is outside the exclusive scope of the do-while loop. It is in the scope of the main() function. Thus yn is not destroyed till the main() function finishes executing. Therefore, this code executes properly (it infinitely prints “Hello World”.

  • Related