problem with getch()

This is a discussion on problem with getch() within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include <conio.h> void main() { int age; cout << "Please enter your age here -> "; cin ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    problem with getch()

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    void main()
    {
      int age;
      cout << "Please enter your age here -> ";
      cin >> age;
      if (age > 100)
    	  {
    		  cout << "Your pretty old are'nt u ! lol";
    	  }
      else if (age == 100)
    	  {
    		  cout << "So you got 100 years old .. haha";
    	  }
      else
    	  {
    		  cout << "Your pretty young :P";
    	  }
      getch();
    }
    so with this code , i need to press a key before it show the answer , thats not what i want , i want it to show then wait for the user to press a key .. what should i do ?

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    um

    I think you mean that you don't want them to have to press enter after typing the age variable in? It can be done...but with the logic available, it's not optimal....basically you could do this
    Code:
    using namespace std;
    ...
    
    int max=2;//this allows the age to be 10-99 years
    std::cin.get(age,max);
    
    //or
    
    int max=3;//allows to be 100-999
    std::cin.get(age,max);
    note: it is possible to get any age up to max (so for max=2 you could be 0-9 years old, but they'd have to press the enter key.


    If i'm answering a question besides the one you asked ...then could you be more specific as to what you want?
    PHP and XML
    Let's talk about SAX

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    no sorry , i mean the user enter his age , then press enter , then nothing happen and the user needs to press enter again , the answer come up and the prog qui . but i want that the answer come up and then he press enter than the prog quit , to have a little pause

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    DarkKinG,
    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    {
      int age;
      std::cout << "Please enter your age here -> ";
      std::cin >> age;
      if (age > 100)
    	  {
    		  std::cout << "Your pretty old are'nt u ! lol";
    	  }
      else if (age == 100)
    	  {
    		  std::cout << "So you got 100 years old .. haha";
    	  }
      else
    	  {
    		  std::cout << "Your pretty young :P";
    	  }
      getch();
      return 0;
    }
    This code executes perfectly well, both on Borland and MSVC++6.. (Your "smilies" don't work, but...)

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    cin and getch() don't go together. Use fgets() and getch(), no problem.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Sebastiani,

    'fgets()'?

    Are you trying to send this entire Board into a 'tailspin'?

    You don't presume to think that C++ people know about 'fgets()', do you?

    You're right. getch() is non-Standard, as is <conio>. (Not an insurmountable problem, but ...)

    The code presented is fine, "as is". (Ran it myself.That'll make you sleep better tonight!! )

    Ever notice that the "poster" catches" up much later than the rest of us?

    Understandable, I'm sure. They're coding and we're goofing off!

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  7. #7
    Registered User Unreg1stered's Avatar
    Join Date
    Jul 2002
    Posts
    25
    Originally posted by TheDarKinG
    no sorry , i mean the user enter his age , then press enter , then nothing happen and the user needs to press enter again , the answer come up and the prog qui . but i want that the answer come up and then he press enter than the prog quit , to have a little pause
    How about include <stdlib.h> and use system("Pause"); instead of using getch()? (assuming that your code is to run under Dos/Windows)

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Use fgets() in both C & C++, otherwise overload cin. Why? Because it can cause unexpected tailspins of it's own, such as passing multiple inputs, newline insertions, etc.
    If you wish to use cin >> then beware of it's idiosyncrocies, that's all.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Unreg1stered


    How about include <stdlib.h> and use system("Pause"); instead of using getch()? (assuming that your code is to run under Dos/Windows)
    system() calls are slow, very very platform dependant
    hello, internet!

  10. #10
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    USE THIS

    you can do this:
    Code:
    std::cin>>age;
    std::cin.clear()://clears the input buffer so it will not conflict with getch()
    ...
    getch()
    it will run fine.
    PHP and XML
    Let's talk about SAX

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    My understanding of std::cin.clear(); is that this command "clears" error bits, i.e. resets the input stream error flags. (Wish someone would clarify this one, once and for all. )

    One surprisingly simple method of flushing the stream is to use 'std::endl;' which does the trick. (Also surprising how few of us remember this little goodie, me included.)

    (MSVC++, for one, has a problem with the istream buffer and getch();, so it behooves folks using that compiler to recognize this shortcoming. Relying exclusively on Borland, this gave me fits for a time.)

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  12. #12
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    it is my understanding that std::cin.clear() clears EVERYTHING from the input buffer. I've also been told that cin.flush() does similar, but is used differently as far as syntax goes. Another trick of the trade, besides std::endl; is to do something like this:
    Code:
    std::cin>>age,cin.get();
    basically, the return key signals the input of a varialble, but the '\n' code is not included in the variable, so it sits in the input buffer. If you have getch() or sometimes even another cin (depends on the quirkieness of the compiler ie: MSVC++) it will read the input buffer, see the '\n' and believe that what you've typed (which is nothing) has been completed so it almost seamlessly skips over that little section of code. IE:
    Code:
    cin>>age;
    cin>>something;
    cout<<age<<'\n'<<something;
    //would ask for age, then seem to skip over asking for something
    PHP and XML
    Let's talk about SAX

  13. #13
    Unregistered
    Guest
    clear() clears error bits from istream input. Is to be used with cin.fail() or cin.good().

    endl clears ostream buffers, not istream buffers.

    There is no "good" way to clear an istream buffer that I know of. The people I respect say the best way to try is to include the limits.h file. In there is a #define for something like MAX_INT which defines the largest int value allowed. Use that value as the limit in a call to ignore(), like: cin.ignore(MAX_INT). I usually use cin.ignore(80), but that isn't as secure as it could be.

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    Thx guys ! i appreciate

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. Problem with my file opener
    By kzar in forum C Programming
    Replies: 7
    Last Post: 04-20-2005, 04:20 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem with getch() and fgetc()
    By Krupux in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 11:38 PM

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