Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: C++ pause  (Read 5725 times)

0 Members and 1 Guest are viewing this topic.

Dilbert

    Topic Starter
  • Moderator


  • Egghead

  • Welcome to ComputerHope!
  • Thanked: 44
    C++ pause
    « on: August 14, 2006, 06:41:34 PM »
    OK, I'm trying to make the long jump from Visual Basic to C++. I'm reading a few online tutorials, and C++ isn't the monster I remember (if I can look at a C++ code block and say, "hey, that's just like JavaScript!", I may have it made). However, it's still a little confusing.
     
    I'm starting with command-line based programs at the moment, just a few "hello world" variants. I want to pause the program so I can actually see what's going on. One person recommends including stdlib.h and using the line  
     
    Code: [Select]
    system("PAUSE");
    But it won't pause. I've compiled it three times and ran it as many, no syntax errors, but it's just not stopping long enough to read. What can I do to make it pause?
     
    Using Dev C++ 4.9.9.2.
    « Last Edit: August 14, 2006, 06:44:21 PM by Timothy_Bennett »
    "The geek shall inherit the Earth."

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: C++ pause
    « Reply #1 on: August 14, 2006, 07:00:17 PM »
    I know next to nothing about C++, but Google and I are thisclose

    Code: [Select]
    #include <stdio.h>

    int main()
    {
      printf ("Press ENTER to continue.\n");
      getchar ();
      return 0;
    }

    OR

    Code: [Select]
    #include <stdlib.h>

    int main()
    {
      system ("pause");
      return 0;
    }

     8-)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    ghostdog74



      Specialist

      Thanked: 27
      Re: C++ pause
      « Reply #2 on: August 14, 2006, 09:57:35 PM »
      Sometimes, simple print statements can also help you debug your program. Or you could try the C++ exception catching. http://cplus.about.com/od/beginnerctutorial/l/aa122202a.htm

      Dilbert

        Topic Starter
      • Moderator


      • Egghead

      • Welcome to ComputerHope!
      • Thanked: 44
        Re: C++ pause
        « Reply #3 on: August 14, 2006, 10:06:35 PM »
        But this program is far too simple to have such huge difficulties, you'd think.

        dl65, I too used Google. I'm posting because it didn't work.

        The full source code (nobody laugh, I'm on a tutorial):

        Code: [Select]
        #include <iostream.h>
        #include <stdlib.h>

        int main()
        {
            int myNumber;
            long myNumber2 = 5;
            char myCharacter;
            char mycharacter2 = 'c';
            bool myBoolean;
            bool myBoolean2 = true;
            
            
            myNumber = 3678;
            myCharacter = 'a';
            myBoolean = false;
            
            cout << "mynumber = " << myNumber << endl;
            cout << "mynumber2 = " << myNumber2 << endl;
            
            cout << "mycharacter = " << myCharacter << endl;
            cout << "mycharacter2 = " << myCharacter2 << endl;
            system("PAUSE");
            return 0;
        }

        It isn't pausing...
        "The geek shall inherit the Earth."

        ghostdog74



          Specialist

          Thanked: 27
          Re: C++ pause
          « Reply #4 on: August 14, 2006, 11:09:47 PM »

          Neil



            Expert
          • Fear me Track. Noone can escape my wrath.
          • Thanked: 3
            Re: C++ pause
            « Reply #5 on: August 15, 2006, 08:24:03 AM »
            The solution is simple. ;D

            char temp; (or whatever) where you define your variables.

            then at the end

            cin >> temp;

            The program will pause and wait for you to enter in some letters. Just press anything and hit enter to end the program.


            Just so you know, cin is where the user enters in data. For example cin >> temp, I type in 'a', and temp now = 'a'
            « Last Edit: August 15, 2006, 08:25:08 AM by Neil »

            Dilbert

              Topic Starter
            • Moderator


            • Egghead

            • Welcome to ComputerHope!
            • Thanked: 44
              Re: C++ pause
              « Reply #6 on: August 20, 2006, 09:23:51 AM »
              "The geek shall inherit the Earth."

              Neil



                Expert
              • Fear me Track. Noone can escape my wrath.
              • Thanked: 3
                Re: C++ pause
                « Reply #7 on: August 20, 2006, 09:38:26 AM »
                What? You are going to do THAT?

                All you need to do is

                char temp;
                cin >> temp;

                at the end of your program.

                Simple!

                Dilbert

                  Topic Starter
                • Moderator


                • Egghead

                • Welcome to ComputerHope!
                • Thanked: 44
                  Re: C++ pause
                  « Reply #8 on: August 20, 2006, 12:10:18 PM »
                  Oh, OK. I thought that... well... whatever. That works just as well, if not better. :)

                  Like I said, sorry, this is really new to me. It's not easy learning this stuff. :)

                  EDIT: ACK! IT'S THE P2K (Post 2K) VIRUS!!! :)

                  Seriously, look at it. It's calling me an expert, for some reason. :(
                  « Last Edit: August 20, 2006, 12:11:15 PM by Timothy_Bennett »
                  "The geek shall inherit the Earth."