LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2016, 12:15 AM   #1
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 63

Rep: Reputation: Disabled
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 ?
 
Old 02-22-2016, 02:40 AM   #2
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
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.
 
1 members found this post helpful.
Old 02-22-2016, 02:43 AM   #3
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
It looks like you defined c as a single character but scanf is expecting a character array.
 
Old 02-22-2016, 05:47 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316
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/
 
1 members found this post helpful.
Old 02-22-2016, 11:52 PM   #5
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ramurd View Post
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.
 
Old 02-23-2016, 12:05 AM   #6
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by psionl0 View Post
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
 
Old 02-23-2016, 12:19 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316Reputation: 7316
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.
 
Old 02-23-2016, 01:20 AM   #8
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by hardikgohil1988 View Post
Actually I want to allow backspace so user input can avoid typo errors.
Quote:
Originally Posted by hardikgohil1988 View Post
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.
 
Old 02-23-2016, 02:17 AM   #9
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by psionl0 View Post
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
 
Old 02-23-2016, 02:42 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

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

Last edited by NevemTeve; 02-23-2016 at 02:50 AM.
 
Old 02-23-2016, 05:01 AM   #11
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by hardikgohil1988 View Post
This is my code
&c is incorrect since c is already an address (of the start of an array).
 
Old 02-23-2016, 07:13 AM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by psionl0 View Post
&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 View Post
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.
 
Old 02-23-2016, 10:27 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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'

Last edited by NevemTeve; 02-23-2016 at 10:30 AM.
 
Old 02-25-2016, 03:22 AM   #14
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by psionl0 View Post
&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.
 
Old 02-25-2016, 03:22 AM   #15
hardikgohil1988
Member
 
Registered: Sep 2014
Posts: 63

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
backspace problem with scanf hardikgohil1988 Linux - Embedded & Single-board computer 2 02-22-2016 09:05 AM
[SOLVED] scanf problem sree_ec Programming 10 05-13-2011 10:12 AM
problem with scanf fssengg Programming 3 03-29-2005 04:27 AM
scanf() problem. LUB997 Programming 3 12-19-2004 08:03 PM
backspace problem moschi Linux - Newbie 4 03-23-2004 05:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration