Thread: Code Priorities?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    12

    Code Priorities?

    OK, I'm wondering why system("pause") excecutes before everything else, even if it hasent been written first in the coding.... here's an example:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
    cout << "Hello world!";
    system("pause");
    
    return 0;
    }
    OK heres what it does:
    (system pause here, usually says (Press any key to continue) )
    Hello World!


    See? It excecutes the system("pause") first....

    Is there anyway to stop it... OR another way to pause the program until the user presses a key?


    Thanks for your help..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try

    cout << "Hello world!";
    cout.flush();
    system("pause");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    12
    Awesome, thanks man that worked.... appreciate it

    Also, does every function HAVE to have a "return" in it?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Every function which isn't returning void must return something

    A void function can just fall off the end. These are the same
    Code:
    void foo ( void ) {
      // stuff
    }
    void foo ( void ) {
      // stuff
      return;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Salem
    Every function which isn't returning void must return something
    Except for main() which is special.

    The following program is valid:

    Code:
    #include <iostream>
    int main()
    {
       std::cout << "Hi!";
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    12
    Ok sweet, thanks guys...


    Also is there any way to display a "static" Loading thing, such as
    Loading...
    CLEAR SCREEN
    Loading....
    CLEAR SCREEN
    Loading.....
    CLEAR SCREEN
    Loading......
    CLEAR SCREEN
    Loading.......

    You get me? So It clears the screen and adds a dot to "Loading"

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use a for loop which prints one more dot each time around the loop
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    12
    Ok... (If i can remember loops lol) Im definetly not 1337 at C++ lol, little help?

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Eminence
    Ok... (If i can remember loops lol) Im definetly not 1337 at C++ lol, little help?
    There's nothing l337 about writing a for or while loop. There are plenty of examples on this board, try looking for some.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    12
    Hmm.... the dots appear much too fast... is there any way to slow em down?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Lookup a sleep function
    Code:
    #include <iostream>
    #include <unistd.h> // where sleep() is for me, yours may differ
    
    using namespace std;
    
    int main ( ) {
        cout << "Loading";
        for ( int i = 0 ; i < 10 ; i++ ) {
            cout << ".";
            cout.flush();
            sleep(1);
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I have tried like 3 or more different variations, but none seem to work when compiled with MSVC++ 6.0 on XP Pro. Even the one defined in windows.h hasn't worked...

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by face_master
    I have tried like 3 or more different variations, but none seem to work when compiled with MSVC++ 6.0 on XP Pro. Even the one defined in windows.h hasn't worked...
    Post what you've written... and what's the symptoms?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Code:
    cout.flush()
    what is does is that it flushes(empty) the buffer which is used to store the output, which depends on the system, you can also use:
    Code:
    cout << endl;
    which will make a new line and emplty the screen buffer.
    also if you use :
    Code:
    cin >> something
    this will also automaticaly empty the buffer.

  15. #15
    Registered User
    Join Date
    Sep 2002
    Posts
    12
    Grrr:

    (from MSVC++ in Win XP Pro)

    fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory

    Is there ANY way possible to do this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM