problems after compiling C ++ with compiler G ++ [closed]

-1

I'm having a problem here after compiling a program in C ++ using the g ++ compiler, after I compile everything, I try to open the executable file, then open my windows cmd (I use windows 10), the problem is that when the terminal starts to show something it automatically closes, how do I fix this? I already tried using the getch () and scanf () functions but nothing happened

PROGRAM

#include <stdio.h>
void main(){
    printf("Teste");
}
    
asked by anonymous 21.03.2017 / 16:43

1 answer

1

Yes, this is the expected one, after all, it does not have a loop holding or pause to avoid, the program runs what has to run and delivers main() so the process ends and the window closes, this is the Exactly expected effect, it's no problem with the compiler, but with your understanding.

Program with graphical interface does not close because it has an internal (+ or -) loop, which prevents the process from ending while the window is open.

If you want to prevent the window from closing, just use pause (in windows I think)

#include <stdio.h>  /* printf */
#include <stdlib.h> /* system */

void main(){
    printf("Teste");
    system("pause");
}

Or an example you can do is to drag your executable after compiled into the cmd window, press Enter on the cmd screen the answer will appear:

getch,getcheandgetchar

Waittotypesomethingtofinish:

#include<stdio.h>/*printf*/#include<conio.h>/*getch*/intmain(){printf("teste");
    printf("\nPressione qualquer tecla para finalizar");
    getch();
    return 0;
}

Wait to type something and send what was typed into the output:

#include <stdio.h> /* printf */
#include <conio.h> /* getch e getche */

int main(){
    printf("teste");

    printf("\nDigite uma letra ou numero:\n");
    getche();

    printf("\nDigite qualquer coisa para finalizar");
    getch();
    return 0;
}

However, depending on this link , the use of anything related to conio.h may not be reliable , then use getchar :

#include <stdio.h>

int main ()
{
    printf("Digite algo:");

    getchar();
    return 0;
}

If you need to get the output:

#include <stdio.h> /* printf e getchar */

int main (){
    printf("Digite algo:");

    int test = getchar();
    putchar(test);
    return 0;
}
    
21.03.2017 / 18:28