Thread: How to input one single character

  1. #1
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75

    How to input one single character

    I want to enter say "1" without having to press enter after typing "1". I don't mean x=getchar(); because after typing "1" the flow stops. Is there a way to also implicitly get carriage return stuck to the x?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,503
    It's possible, but there's no standard way of doing this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by laserlight View Post
    It's possible, but there's no standard way of doing this.
    It can be done via information re which key was pressed.

  4. #4
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    I want to continue with "writeBill" after pressing "1", however the "1" in the buffer stops this from happening. How can I do this?

    Code:
        if (kbhit)
        {
            switch(getche())
            {        
                case '1': writeBill(); break;
                case '2': retrieve();  break;
                // . . . .
            }
        }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,663
    Code:
    while ( 1 ) {
        if (kbhit) {
            what = getche();
        }
        switch(what)
        {        
            case '1': writeBill(); break;
            case '2': retrieve();  break;
            // . . . .
        }
        // maybe sleep()
    }
    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.

  6. #6
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by Salem View Post
    Code:
    while ( 1 ) {
        if (kbhit) {
            what = getche();
        }
       . . . 
    }
    Yes!! I had done it another way (did a dummy print). But got a bug. Was looking for it in the wrong place. Found it.

    Thank you for your bedtime wishes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me to print a single character please
    By Jilbo_Jaggins in forum C Programming
    Replies: 3
    Last Post: 05-03-2015, 10:28 AM
  2. How to loop a single character?
    By spcx in forum C Programming
    Replies: 4
    Last Post: 08-23-2011, 10:54 PM
  3. Need help with Single Character
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 10-05-2010, 01:29 AM
  4. Single Character Input.
    By mintsmike in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2009, 08:16 AM
  5. How to get single character input without pressing return
    By Mr. Flibble in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2006, 08:55 AM

Tags for this Thread