ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
First of all; enclose your code with [ code ] tags. It makes it much more readable for the rest of us.
Then; I think your issue is with atoi actually; I don't think atoi can translate backspace to integer :-)
Since a char is easily also an small integer form (don't shoot me on this, but I recall it as an 8-bit integer, containing the values 0-255); you could -of course- instead do switch(c) instead... only with different values; you'd have to figure out what values correspond to which number.
First of all; enclose your code with [ code ] tags. It makes it much more readable for the rest of us.
Then; I think your issue is with atoi actually; I don't think atoi can translate backspace to integer :-)
Since a char is easily also an small integer form (don't shoot me on this, but I recall it as an 8-bit integer, containing the values 0-255); you could -of course- instead do switch(c) instead... only with different values; you'd have to figure out what values correspond to which number.
Actually I want to allow backspace so user input can avoid typo errors.
Last edited by hardikgohil1988; 02-22-2016 at 11:54 PM.
As I wrote you you cannot solve it that way. You need to catch all the keys pressed and act on that (so handle backspace, print letters ...). scanf just reads the keyboard buffer (more or less) which contains the keys pressed one by one.
Actually I want to allow backspace so user input can avoid typo errors.
Quote:
Originally Posted by hardikgohil1988
Yes I have changed from char c to char c[3]; and tried still i cannot use backspace to avoid typo errors
Unless you also changed scanf as well, your program still won't work as written (you are passing a pointer to a pointer to the first character in the array). I'm surprised your program doesn't segfault.
Even if you are only interested in the first character in the array, having only just enough room for two characters (plus the null) in the character array is just begging for a segfault if the user types more than 3 characters.
Unless you also changed scanf as well, your program still won't work as written (you are passing a pointer to a pointer to the first character in the array). I'm surprised your program doesn't segfault.
Even if you are only interested in the first character in the array, having only just enough room for two characters (plus the null) in the character array is just begging for a segfault if the user types more than 3 characters.
Code:
char c[10];
printf("Enter Your Choice :");
scanf("%s",&c);
i = atoi(&c);
switch(i){
}
&c is incorrect since c is already an address (of the start of an array).
For one, exactly this. Use just 'c' not the address of the array
Quote:
Originally Posted by pan64
As I wrote you you cannot solve it that way. You need to catch all the keys pressed and act on that (so handle backspace, print letters ...). scanf just reads the keyboard buffer (more or less) which contains the keys pressed one by one.
Also exactly this, you CANNOT process backspace using scanf(), I suggest getchar(3) and a loop until you see CR or LF.
Terminal-drivers are usually able to handle backspace key (you have to use stty(1) or termios(2) to prevent this), for example pressing keys 1 2 4 BkSpc 3 Enter should result "123" in varible 'c'
Terminal-drivers are usually able to handle backspace key (you have to use stty(1) or termios(2) to prevent this), for example pressing keys 1 2 4 BkSpc 3 Enter should result "123" in varible 'c'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.