LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   backspace problem with scanf (https://www.linuxquestions.org/questions/programming-9/backspace-problem-with-scanf-4175572878/)

hardikgohil1988 02-22-2016 12:15 AM

backspace problem with scanf
 
Hello all,

I have written a Linux application program which tests the various peripherals on-board runs on ARM Board.

This menu driven application takes input from scanf as code snippet below

printf("Enter Your Choice :");
scanf("%s",&c);
i = atoi(&c);

switch (i) {
case 1:
while(1){
//test one
}
case 2:
break;
-
-
-
case 14:
break;
}

when I execute on this and try for back space it gives some garbage
as
--------------------------
13.Automatic Test
14.Exit

Enter Your Choice :fffffff

---------------------------

Anyone guide on this ?

Ramurd 02-22-2016 02:40 AM

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.

psionl0 02-22-2016 02:43 AM

It looks like you defined c as a single character but scanf is expecting a character array.

pan64 02-22-2016 05:47 AM

I think you cannot catch backspace that way. You need to read keycodes before they will be evaluated
http://www.linuxquestions.org/questi...d-in-c-150067/

hardikgohil1988 02-22-2016 11:52 PM

Quote:

Originally Posted by Ramurd (Post 5504108)
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.

hardikgohil1988 02-23-2016 12:05 AM

Quote:

Originally Posted by psionl0 (Post 5504111)
It looks like you defined c as a single character but scanf is expecting a character array.

Yes I have changed from char c to char c[3]; and tried still i cannot use backspace to avoid typo errors

pan64 02-23-2016 12:19 AM

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.

psionl0 02-23-2016 01:20 AM

Quote:

Originally Posted by hardikgohil1988 (Post 5504619)
Actually I want to allow backspace so user input can avoid typo errors.

Quote:

Originally Posted by hardikgohil1988 (Post 5504622)
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.

hardikgohil1988 02-23-2016 02:17 AM

Quote:

Originally Posted by psionl0 (Post 5504652)
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){
}

This is my code

NevemTeve 02-23-2016 02:42 AM

incomplete.
And what is the problem, exactly? What's happening, when you press <BkSpc] key?

psionl0 02-23-2016 05:01 AM

Quote:

Originally Posted by hardikgohil1988 (Post 5504665)
This is my code

&c is incorrect since c is already an address (of the start of an array).

rtmistler 02-23-2016 07:13 AM

Quote:

Originally Posted by psionl0 (Post 5504702)
&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 (Post 5504631)
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.

NevemTeve 02-23-2016 10:27 AM

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'

hardikgohil1988 02-25-2016 03:22 AM

Quote:

Originally Posted by psionl0 (Post 5504702)
&c is incorrect since c is already an address (of the start of an array).

Sorry mistaken code posted

Code:

char c[10];

printf("Enter Your Choice :");
scanf("%s",c);
i = atoi(c);
switch(i){
}

example I type

Enter your choice:123backspace

should be 12 but prints invalid option as defined at default switch case.

hardikgohil1988 02-25-2016 03:22 AM

Quote:

Originally Posted by NevemTeve (Post 5504851)
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'

How can I use this in my code.

Any example code


All times are GMT -5. The time now is 11:25 PM.