-2

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.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    Next time, tell us what the error is. I don't know C++ but maybe the variable yn is out of scope in the first program. – Mark Cilia Vincenti Dec 04 '22 at 07:49
  • **Stackoverflow is not an introduction to C++.** These basic things are explained in any beginner [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Dec 04 '22 at 08:42
  • @JasonLiam : The reason for the function-pointer comparison to `char` error is not covered by _any_ book though I suspect. Had the expected error message occurred, the issue might be as obvious as you suggest and warrant such admonishment. – Clifford Dec 04 '22 at 10:30

3 Answers3

3

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • Better not to use the `using namespace std` than to have to work around poor naming in math.h. – Clifford Dec 04 '22 at 08:33
  • @Clifford Make no difference on this particular compiler, since the name is defined in the global namespace. – john Dec 04 '22 at 08:36
  • Given that he has only include that is rather poor form. – Clifford Dec 04 '22 at 08:42
  • ... looking at the gnu source (mathcalls.h) it looks like defining `__STRICT_ANSI__` would resolve that (defined when `-ansi` compilation is used). – Clifford Dec 04 '22 at 09:03
  • Someday, maybe the POSIX functions will be in the `posix` namespace, which has been reserved for that purpose. My inner Eeyore expects that will never happen. – Eljay Dec 04 '22 at 12:55
1

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”.

1

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.

Clifford
  • 88,407
  • 13
  • 85
  • 165