Thread: Build Quitting

  1. #1
    Lonewolf
    Join Date
    Nov 2004
    Location
    St. Louis, MO
    Posts
    24

    Build Quitting

    Lets say I'm doing a calculator program. The user inputs his/her two numbers and press enter. The screen shows the answer for like 3 seconds than quits. I tried using the getch() and cin.get() function, but it always quits. How can I get around this?
    COMPILER: Borland 5 ( trying to get MV C++ ); LANGUAGE(s): English, Vietnamese, and basic German;
    Current IDE: Dev C++ 5 (Beta)

  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
    Post some code

  3. #3
    Lonewolf
    Join Date
    Nov 2004
    Location
    St. Louis, MO
    Posts
    24
    Its a code from the tutorial on Structures.

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct xampl {
      int x;
    };
    
    int main()
    {  
      xampl structure;
      xampl *ptr;
      
      structure.x = 12;
      ptr = &structure; // Yes, you need the & when dealing with structures
                        //  and using pointers to them
      cout<< ptr->x;    // The -> acts somewhat like the * when used with pointers
                        //  It says, get whatever is at that memory address
                        //  Not "get what that memory address is"
      cin.get(); 
      return 0;                   
    }
    Once again, this is the code from the tutorial "Structures". I modified this a bit ( deleted the modified one, sorry... ) and thats when the quitting problem happened. Displays for less than 3 seconds ( u can't even see the output ) and it quits.
    COMPILER: Borland 5 ( trying to get MV C++ ); LANGUAGE(s): English, Vietnamese, and basic German;
    Current IDE: Dev C++ 5 (Beta)

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That code looks alright to me.

  5. #5
    You can try using:
    Code:
    #include <stdlib.h>
    system("PAUSE");
    Or
    Code:
    int x; cin >> x;
    I'm not very experienced, so I donīt know other ways

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Kai_Legends


    Once again, this is the code from the tutorial "Structures". I modified this a bit ( deleted the modified one, sorry... ) and thats when the quitting problem happened. Displays for less than 3 seconds ( u can't even see the output ) and it quits.
    So, make some code that is broken and post that. It shouldn't be too hard to make broken code (you did it once --- you can do it again).

    "If it ain't broke, we can't fix it."

    Regards,

    Dave

  7. #7
    Lonewolf
    Join Date
    Nov 2004
    Location
    St. Louis, MO
    Posts
    24
    Heres the modified version. Compiled with Dev C++ 5 if that helps out at all.

    Code:
    #include <iostream>
    #include <conio>
    
    using namespace std;
    
    struct xampl {
      int x;
    };
    
    int main()
    {  
      xampl structure;
      xampl *ptr;
      
      structure.x = cin.get();
      ptr = &structure;
    
      cout<< ptr->x;
    
      getch();
    
    return 0;
    
                        
    }
    COMPILER: Borland 5 ( trying to get MV C++ ); LANGUAGE(s): English, Vietnamese, and basic German;
    Current IDE: Dev C++ 5 (Beta)

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
    structure.x = cin.get();
    When the code execution reaches this line, it waits for you to type in something. Let's say, for example, that you type in 7. The code is still waiting, because get() waits for you to press <enter> before getting what you typed. So you press <enter>. Now the cin.get() function gets the 7 out of the input stream and you use it in the program.

    Later, the code execution reaches getch(). The point of the getch() function is to get the first character from the stream. Since the <enter> you typed before is still there, the getch() gets it and the program doesn't need to wait for anything else. If you had used two getch() functions, the first one would get the <enter> from earlier, and the second one would wait for you to hit enter again, effectively pausing the program like you want.

    A possibly better solution is to ignore everything in the stream, then get again. Instead of <conio> and getch(), use <limits> and:
    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    That will ignore any and all garbage still in the input stream (for example, if you had type 75 instead of 7, then there would be a 5 and an <enter> still in the stream). It will then wait for you to press enter.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    structure.x = cin.get();
    That is going to give you the ASCII value of the entered number. It might be better to do:
    Code:
    cin >> structure.x;

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    Can you explain to me what you are trying to learn because it might be easier If we just taught you what a Struct was and how to use it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PlaySound
    By cangel in forum C++ Programming
    Replies: 16
    Last Post: 10-08-2009, 05:29 PM
  2. How to build, run Boland C++ 6 source file via VS2005?
    By userpingz in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2009, 03:25 AM
  3. Replies: 0
    Last Post: 10-07-2008, 12:09 PM
  4. Replies: 0
    Last Post: 09-24-2006, 06:26 AM
  5. Boom, Headoshot!!
    By mrafcho001 in forum A Brief History of Cprogramming.com
    Replies: 50
    Last Post: 07-21-2005, 08:28 PM