Thread: Noob question, command prompt

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Noob question, command prompt

    I'm fairly new to C and I've run into a problem that I can't seem to figure out. When I try to run a program, the command prompt window will flash really fast and close. How do I get the .exe to stay open?

    I use Dev-C/C++,
    Windows Service pack 2
    2.8 ghz
    516 ram

    And it's not with just one program either, every source I try the command prompt window just flashes really fast.


    Some sources I use:
    Code:
    #include <stdio.h>
    
       main()
       {
         printf("hello, world\n");
       }
    and
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define TAX_RATE  0.05
    
    main()
    {
          float balance; 
          float tax;
          float total;
          balance = 100;
          tax = TAX_RATE * balance;
          total = tax + balance;
          printf("If you spend $%.1f, the tax will be $%.1f ... The total is $%.2f.", balance, tax, total); 
          }

  2. #2

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    6

    How to Stop Compiler to See Output

    This problem can be easily resolved if you use a function named getch(), which would force compiler to stop and you can view you output, but don't forget to put conio.h file in the header section and also conio.h is not valid in UNIX OS. The Code would be
    Code:
    //Header Files
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    printf("Hello World");
    getch();//Inbuilt Function Used
    }

  4. #4
    old man
    Join Date
    Dec 2005
    Posts
    90
    I'm primarily a unix guy, but I believe conio.h is a borland extension ... so it's not valid in more than just unix: it's not standard C at all

    You can use getchar() instead, which is in the FAQ

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    the latest version of dev has conio.h, as do microsoft visual series
    of compilers, however that is not my issue - its not a good idea
    to recommend the use of a non-standard header, especially to
    implement a single function, and even moreso when the same
    effect can be achieved using functions in a header already in
    use!!! also just so you know, main has a return type,

    Code:
    /*this will compile but that doesn't make it right!*/
    
    main ()
    {
    /*Program goes here*/
    }
    sholud be changed to

    Code:
    /*this is better, its the international standard way, so it compiles on any ANSI/ISO compliant compiler*/
    
    int main ()
    {
    /*Program goes here*/
    return 0;
    }
    just spreading the knowledge!!!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can use getchar() instead, which is in the FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);
    It's better to use that than just
    Code:
    getchar();
    in case there's something in stdin.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    if your on windows you can also do a system("PAUSE");

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Narcose
    if your on windows you can also do a system("PAUSE");
    Except, we don't make unnessasary calls to the system, do we?
    Sent from my iPadŽ

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    if your on windows you can also do a system("PAUSE");
    We don't want to make unnessesary, non-portable system calls, either. See my FAQ link.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 09:28 PM