devxlogo

Problems with Getc and Scanf

Question:
I want to make a menu, where you can select from 1-4. By pressing one of these keys you should get into a subprogram. Now I have to press enter after I select 1,2,3 or 4. Is it possible to have C++ bypass “enter” and go directly to the subprogram. I can’t see how I can use getc or scanf, but are there any other possibilities or functions I could use?

Answer:
Instead of scanf(), use the getche() function which is defined in. For example:

#include #include int main(){ char userdata; printf("enter your choice: "); userdata = getche(); // "Enter" not needed} 

Editor’s Note: This answer has been changed from the original.

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Engineering Leaders Spot Weak Proposals

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.