-1
void first_screen_output()
{
    int input_num(); 
    scanf("%d",&input_num);
    if(input_num==1) return;
    else if(input_num==2)
    {
        system("pause");
    } 
}

when I tried to compiled this code, the error occurred. the error occurred on if~else line. How can I solve this error?

Inkyu Lee
  • 67
  • 1
  • 1
  • 7

1 Answers1

4
int input_num();

This declares a function called input_num which returns an int and takes no arguments. Informally called "the most vexing parse". The fact that C++ allows you to declare functions inside of the implementation of other functions only adds to the confusion.

scanf("%d",&input_num);

"%d" means that you want to read into an int, so you need to pass an int*. &input_num, however, is a function pointer (int (*)()), as your compiler should have warned you. That's because specifying a function name without parentheses (input_num) indicates a function pointer (and the address-of operator & has no effect here because function pointers are different from all other pointers).

So this line would result in undesired behaviour anyway.

if(input_num==1) return;
else if(input_num==2)

Function pointers cannot be compared with ints.


The solution to all these problems is actually making input_num an int and not a function:

int input_num = 0;

While you're at it, get rid of scanf and use std::cin.

Community
  • 1
  • 1
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62