Menu

Output Window Closes After Pressing Enter

2006-03-01
2012-09-26
  • Nobody/Anonymous

    I have searched the forum, read the faq, and can't find this anywhere.

    I can keep the output window open when I hardwire the values in and then use getchar (), but when I use scanf to interactively input the values then hit enter the window closes. If I've missed this somewhere can someone please post a link

    I am using devcpp-4.9.9.2

    This is my program:

    include <stdio.h>

    main()

    {

    int a, b;

    b = a + a;

    printf("Input the value for a: \n");
    scanf("%lf", &a);

    getchar ();

    return 0;

    }

    This is my compile log:

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Documents and Settings\Owner\Desktop\Untitled.cpp" -o "C:\Documents and Settings\Owner\Desktop\Untitled.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
    Execution terminated
    Compilation successful

     
    • Nobody/Anonymous

      there are still characters in the que. in this case call fflush with stdin before the getchar and it should work.

      snork

       
    • Nobody/Anonymous

      I'm sorry, I'm not quite sure what you mean when you say,

      "call fflush with stdin before the getchar"

      Can you show me an example with this added to my program?

      Is stdin a preprocessor instruction?

         #include &lt;stdin&gt;
      
       
      • Nobody/Anonymous

        fflush(stdin)

        snork

         
    • Josh

      Josh - 2006-03-02

      I can input the value for a, but no matter what value I put in for a, b is always equal to 0.

      Am I missing something?

      This is my program:

      include <stdio.h>

      main()

      {

      int a, b;

      / When I put the value in like this b = 2 /
      a = 1;

      /* When I put the value in using scanf b = 0.

      printf("Input the value for a: \n");
      scanf("%lf", &a);

      I've double checked and I'm pretty sure I've typed everything in correctly.

      */

      b = a + a;

      printf("b = %d, \n", b);

      fflush(stdin);

      getchar ();

      return 0;

      }

      Compile Log:

      Compiler: Default compiler
      Executing g++.exe...
      g++.exe "C:\Documents and Settings\Owner\Desktop\Untitled4.cpp" -o "C:\Documents and Settings\Owner\Desktop\Untitled4.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
      Execution terminated
      Compilation successful

      Output:

      Input the value for a:
      1
      b = 0,

       
    • Josh

      Josh - 2006-03-02

      "there are still characters in the que. in this case call fflush with stdin before the getchar and it should work."

      Can some tell me what this means? Am I always going to have to use fflush(stdin); with scanf, or is there some other way to get the characters out of the que?

       
    • Nobody/Anonymous

      scanf("%lf", &a); // %i or %d are acceptable for integers

       
    • Josh

      Josh - 2006-03-02

      Thanks for all the help. I had thought maybe I should use something other than %lf because the values were integers, but for some reason I thought my professor said %lf was all you could use in scanf. Must of heard him wrong.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.