-1

when I call display function it doesn't call in program and when I use this in main function without using it in function it works perfectly.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main() 
{
int d;
int choose;
int display();
printf("enter 1 to display and 2 to quit\n");
 switch(choose)
{
    case 1:
    display();
    break;
    
}
scanf("%s",choose);
int display();
system("cls");
}
int display()
{
char fname[20], str[500];
FILE *fp;
printf("Enter the Name of File: ");
gets(fname);
fp = fopen(fname, "r");
if(fp==NULL)
    printf("Error Occurred while Opening the File!");
else
{
    fscanf(fp, "%[^\0]", str);
    printf("\nContent of File is:\n\n");
    printf("%s", str);
}
fclose(fp);
getch();
  return 0; 
}

Any ideas what the error is?

Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42

1 Answers1

1

Inside main() you are not calling the display() function, instead you are redeclaring(int display()) it. There is difference between declaring a function and calling it.

Function declaration: int display()

Function call: display();

Declaration simply tells a program about the existence of a function. If you want to invoke a function then make a function call.

EDIT: Your question is poorly framed and difficult to understand. I assume you are asking-

Why function call inside the switch statement is not executing?

There are two reasons for that-

1.First of all you are not assigning any value to choose variable which is used in switch (also pointed out by @Nicholas).

2.scanf(...) statement is wrongly placed and syntax is also wrong. Read here

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main() 
{
int d;
int choose;
int display();
printf("enter 1 to display and 2 to quit\n");
scanf("%d",&choose); //assigning value to 'choose'.
 switch(choose)
{
    case 1:
    display();
    break;
    default: break; //always use a default case for handling wrong input.
}
//int display(); //idk why you're redeclaring it, there is no need. If you're making a function call then remove `int` from syntax.
system("cls");
return 0;
}
int display()
{
char fname[20], str[500];
FILE *fp;
printf("Enter the Name of File: ");
gets(fname);
fp = fopen(fname, "r");
if(fp==NULL)
    printf("Error Occurred while Opening the File!");
else
{
    fscanf(fp, "%[^\0]", str);
    printf("\nContent of File is:\n\n");
    printf("%s", str);
}
fclose(fp);
getch();
  return 0; 
}
avm
  • 387
  • 3
  • 16