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.
- you using
getch();
requires#include <conio.h>
- you using
printf()
family functions. requires#include <stdio.h>
- the function prototype
main()
hasint 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
Post a Comment