Help with a very simple program

This is a discussion on Help with a very simple program within the C++ Programming forums, part of the General Programming Boards category; Hello, I am a student just starting out with C++. I'm using the DevC++ compiler. I've written this program. Its ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Help with a very simple program

    Hello,
    I am a student just starting out with C++. I'm using the DevC++ compiler. I've written this program. Its syntax is correct, and when I compile I get no errors. When I run the program, an output window opens for a split second and closes before I can read anything. Do you have any ideas as to why this might be happening?
    Code:
    /* first.cpp
    * This is my first C Program!
    * This program will display the statement "This is 
    * my first C program" on the output screen
    */
    
    #include <stdio.h>
    
    main ()
    {
         printf ("This is/n");
         printf ("my first C program/n");
    }
    
    /* Computer...end program */

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    your program is running as it should. like any program, it starts and displays, does what it is told to do, then exits. so your window opens, prints the output, then closes because its done doing what it has been told to do. if you run your exe file from a command window you will notice the behavior appears to be different.

    to delay the program from exiting so quickly you could use a command like 'system("pause")' for windows. another option is to read junk user input before quiting. if you havent dont input yet ill let you figure it out or i can help show you.

    hope this helps and good luck!

  3. #3

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    2
    I simply added the getchar command at the bottom. The program now holds. Thanks for your help in solving this id10t error.
    Final code:
    Code:
    /* first.cpp
    * This is my first C Program!
    * This program will display the statement "This is 
    * my first C program" on the output screen
    */
    
    #include <stdio.h>
    
    main ()
    {
         printf ("This is\n");
         printf ("my first C program\n");
         getchar();
         /* The getchar command is here to force the output window to remain open to view the results. 
         I figured out that that is what the command does, when I looked at the prnt20 program on the 
         programming sheet
         */
         
    }
    
    /* Computer...end program */

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you're programming in C++, you might as well use the C++ version.
    Code:
    /* first.cpp
    * This is my first C Program!
    * This program will display the statement "This is 
    * my first C program" on the output screen
    */
    
    #include <iostream>
    
    int main ()
    {
         std::cout << "This is\n";
         std::cout << "my first C program\n" << std::flush;
         std::cin.get();
         /* The cin.get() function is here to force the output window to remain open to view the results. 
         I figured out that that is what the command does, when I looked at the prnt20 program on the 
         programming sheet
         */
         
    }
    
    /* Computer...end program */

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    endl flushes the buffer too, so you could use that instead of flush of you wanted to. just so you know.
    Code:
    std::cout << "my first C program" << std::endl;

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    15
    I just use

    system("PAUSE")

    it works perfectly fine to me

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    but not to someone running a UNIX system...
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    75
    You can just use system ("PAUSE") make sure it is in caps. Just for informational purposes you can also use system ("cls") if you want to clear the window

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by htdefiant View Post
    Hello,
    I am a student just starting out with C++.
    Your code and your comments all point to 'C' rather than C++. (Although most C++ compilers can facilitate 'C' code). Just be wary that the two languages are rather different, despite their appearances, so make sure you know which one you're dealing with

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    cin.get() gets the next item in the input buffer, so you might want to wipe the buffer, or it'll still continue if anything is in the buffer. Use cin.ignore() to wipe the buffer first, you stand a better chance of it pausing and waiting for the input

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> endl flushes the buffer too, so you could use that instead of flush of you wanted to. just so you know.

    Flushing the buffer there isn't even necessary, since the cin.get() will force cout to be flushed, as will the end of the program.

    Using endl or flush is really only necessary when outputting error or log messages so that they are flushed immediately, or when using non-standard code that affects the console like system("PAUSE").

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    add this code at end of your code

    Code:
    system ("pause");

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    int main()

    ha Ha! :-D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 03:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21