LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-29-2005, 05:11 PM   #1
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Rep: Reputation: 15
How to use the scanf( ) int C ?


When I want to input a char, is scanf("%c", &ch) right?

I used it, but it doesn't work well.

So I tried it eight times,like :
Code:
scanf("%c", &ch);
scanf("%c", &ch);
scanf("%c", &ch);
scanf("%c", &ch);
scanf("%c", &ch);
scanf("%c", &ch);
scanf("%c", &ch);
scanf("%c", &ch);
and the 2ed, 4th, 6th, 8th line worked. That is, I can only input 4 chars.

Why?
 
Old 11-29-2005, 06:30 PM   #2
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Try using getchar().
Actually the i/p should fail during 2,4,... since \n supplied after entering the required character will remain in the buffer and will be accepted as the next character.
Anyway, u also try fflush(stdin) before scanf statements.
 
Old 11-29-2005, 06:31 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
how are you testing it? scanf reads the enter key as an input, so if you test by running & then typing a character, followed by an enter key, you're actually giving it two inputs. try this:

Code:
  scanf("%c", &ch);
  printf("%c\n",ch);
  scanf("%c", &ch);
  printf("%c\n",ch);
  scanf("%c", &ch);
  printf("%c\n",ch);
  scanf("%c", &ch);
  printf("%c\n",ch);
  scanf("%c", &ch);
  printf("%c\n",ch);
  scanf("%c", &ch);
  printf("%c\n",ch);
  scanf("%c", &ch);
  printf("%c\n",ch);
  scanf("%c", &ch);
  printf("%c\n",ch);
run it, then type give it the input "1234567890"<enter> - it will give you back 1 through 8. If you give it:
1<enter>
2<enter>
...

it will give you back a 1, then a newline, then a 2 then a newline, etc. through 4
 
Old 11-29-2005, 08:36 PM   #4
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Original Poster
Rep: Reputation: 15
But how can I get 1 char every time ?

Last edited by naihe2010; 11-29-2005 at 08:42 PM.
 
Old 11-29-2005, 10:51 PM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Place the terminal in raw mode. Have a look at man cfmakeraw.
 
Old 11-30-2005, 12:43 AM   #6
spaaarky21
Member
 
Registered: Jun 2004
Location: Omaha, NE
Distribution: Ubuntu 7, Fedora Core 4
Posts: 192

Rep: Reputation: 30
Quote:
Originally posted by naihe2010
But how can I get 1 char every time ?
Well, the code you posted seems a little "generic" so it's hard to tell what you are trying to accomplish but to read a char 8 times, why not try something like this:
Code:
for(int i=0; i<8; i++)
{
    scanf("%c", &ch);
    while(ch=='\n')  // If the char was a linebreak, keep scanning until you get one that isn't
        scanf("%c", &ch);

    // Do something with the char here
}
I'm not really sure what you are after but hopefully this might help. If you only want to read numbers of letters, ctype.h contains a function isalnum() that will tell you if a character is either a number of letter. You could use that in the while loop above to skip everything else, including white space.

HTH
Brandon
 
Old 11-30-2005, 12:59 AM   #7
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Original Poster
Rep: Reputation: 15
In my whole program, I wrote lots of scanf().
But some scanf() doesn't work well. That is, they get the '\n' from the last scanf() .
It 's so disgusting.
I want only get a char, but doesn't influence the next scanf() to get a char narmally.
 
Old 11-30-2005, 02:35 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally posted by naihe2010
It 's so disgusting.
Write your own scanf routine if you don't like it. I don't use scanf functions for user input because of this 'unpredictable' behaviour. I always use fgets(buffer,size,stdin) and do the conversions myself. That way, the behaviour is always predictable (the newline is included in the buffer).

PS when you analyse the behaviour, it's probably not as unpredictable as it looks.
 
  


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
problem with scanf fssengg Programming 3 03-29-2005 04:27 AM
scanf question jnusa Programming 4 01-07-2005 09:04 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM
scanf blackzone Programming 1 08-04-2004 01:50 AM
scanf help homerz Programming 2 11-11-2003 05:48 AM

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

All times are GMT -5. The time now is 01:53 PM.

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