Program gets terminated abruptly in Visual C++ -


whwenever try execute block of code abruptly shuts down if dont use getch() function. have tried different combinations of accepting , printing string gets() , puts() etc. question causes error , how can remove error?

void main() {   char str[100];   printf("enter string\n");   fgets(str,100,stdin);   printf("%s",str);   getch(); } 

void main() {   char str[100];   printf("enter string\n");   fgets(str,100,stdin);   printf("%s",str);   getch(); } 

you have many problems small program.

  1. you using getch(); requires #include <conio.h>
  2. you using printf() family functions. requires #include <stdio.h>
  3. the function prototype main() has int main(void) in case

in conclusion, fixed code:

#include <stdio.h> #include <conio.h> int main(void) {   char str[100];   printf("enter string\n");   fgets(str,100,stdin);   printf("%s",str);   getch();   return 0; } 

run cmd.exe command line window.

the reason shuts down because after completing in program, program automatically terminates. getch() makes system wait input.


Comments

Popular posts from this blog

Unlimited choices in BASH case statement -

Redirect to a HTTPS version using .htaccess -

javascript - jQuery: Add class depending on URL in the best way -