473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

C Program - Escape Key as an input string

hello fellow programmers, Im having problems regarding this code, if this is impossible please let me know.

What i have here is a code that inputs as string. If the user press the "escape key" the program will go to a user-defined function.

Expand|Select|Wrap|Line Numbers
  1. #include "maidcafe.h" ///user-defined..it has all the header files i can imagine xD///
  2. .....
  3. if (strcmp(string, (escape key)) == 0){
  4. fn_menu(); // this will make the program go back to the menu (user-defined function)
  5. }
  6. else{
  7. /////do nothing
  8. }
  9.  
Can you guys tell me what should i put in the "(escape key)"

Thanks in Advance,
AnimeProgrammer :)
Feb 1 '14 #1

✓ answered by weaknessforcats

The first problem you have is that scanf requires you press the enter key so you can't leave scanf by pressing ESC.

This applies to all keyboard input. Your program stops and the keyboard code executes until you press enter. What you see in your variable is the enter key as the last key pressed.

Just have the user enter a character and test that character to make your jump to another function.

The only way out is to write your own keyboard code that does not rely on the enter key. Everybody has done that. The scanf sort of thing is just to get you started.

3 4794
weaknessforcats
9,208 Expert Mod 8TB
The first problem you have is that scanf requires you press the enter key so you can't leave scanf by pressing ESC.

This applies to all keyboard input. Your program stops and the keyboard code executes until you press enter. What you see in your variable is the enter key as the last key pressed.

Just have the user enter a character and test that character to make your jump to another function.

The only way out is to write your own keyboard code that does not rely on the enter key. Everybody has done that. The scanf sort of thing is just to get you started.
Feb 1 '14 #2
mikele
6
Do like this and it will work (at least with gcc)
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     if(getche() == '\e' )
  4.                fn_menu(); // if 1 instruction, no need for brackets
  5.     else; // do nothing
  6.     return 0;
  7. }
  8.  
You don't even need to press enter :D
May 6 '14 #3
donbock
2,426 Expert 2GB
getche is not a standard function. Your code is more portable if you use getchar.

Whether you need to press enter to get your entry recognized depends not on what get function you call, but on whether or not the input stream is buffered. Whether or not stdin is buffered is implementation-dependent. Refer to setvbuf for details.
May 6 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mahmood Ahmad | last post by:
Hi, I am getting the error 'Input String was not in correct format' in the following C# program (indicated line): using System; namespace CF7 { class CalcArea {
3
by: dan | last post by:
I am using VB.NET 2003 and SQL Server 2000. The program uses ADO.NET . The following instruction: Me.cd_insertDataRecord.ExecuteNonQuery() throws this exception: ">>>ProcessDataRecord/...
10
by: Badis | last post by:
Hi Guys, I'm trying to retrieve data defined as Numeric in Cache database and display it in a textbox but it's giving me this error: "Input string was not in a correct format" Cheers.
13
by: Jen | last post by:
One user of my application is experiencing an exception "input string not in correct format". But it makes no sense where it is occurring. It is occurring when a string from a textbox ("172") is...
0
by: sehguh | last post by:
Hiya Folks, I am Currently using windows xp. Also using Visual Web Developer 2005 and Microsoft Sql server 2005. The main page consists of an aspx page and a master page. The page also...
3
by: lucky_therock | last post by:
hey..i ma newbie to c programming...its just my 3rd program..so i wanna know a code in which input string can be matched with strings in another opened file....!!
3
by: satyakarvvk | last post by:
Hi everybody! Please help me to overcome below runtime exception. Actually it is a simple program on basics. I want to print odd nos upto which the user asks and after printing the task,...
7
by: Chuthu | last post by:
I'm writing a C program where the user enters a string and the program aims at finding whether the input string is meaningful English word or not. Comparing each and every input word with the...
1
by: differentsri | last post by:
THIS IS AN ASP.NET 1.1 APPLICATION IAM TRYING TO UPDATE THE FIELD BUT I AM NOT ABLE TO UPDATE IT? CAN U TELL THE REASON ? IT IS GIVING THE FOLLOWING ERROR BELOW I HAVE ALSO GIVEN THE CODE OF...
5
by: rohdej | last post by:
Hello - I have been all over the web and found a few posts that are somewhat related to what I'm trying to do, but none that provided me a concise answer. I want to prompt the user to input the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.