Solved

scanf and new line character

Posted on 2000-03-11
9
2,041 Views
Last Modified: 2008-03-17
scanf("%i", &n);

 where n has been declared: int n;

 my problem is that when it asks me to enter an integer, it reads the newline character as well. Since it doesn't match it puts it back fot the next scanf..
 How can avoid this without using another variable just to read the newline character?
0
Comment
Question by:anemos
9 Comments
 
LVL 1

Expert Comment

by:Hardaway
ID: 2607712
The task is about getting integer numbers one-by-one in a loop? So that you would like to avoid the newline character?

Have you tried gets()? It was more useful in most cases. However, gets only support characters, you might need to do some conversion for getting integers.
0
 

Author Comment

by:anemos
ID: 2607777
how can I convert then the string into an integer??
I spse that means accesing each indvidual character.. there must be a more simple way..
0
 
LVL 84

Expert Comment

by:ozo
ID: 2607789
What's the next scanf?  You can supress assignment with %*
0
 
LVL 3

Expert Comment

by:arnond
ID: 2607818
if every line contains only one int followed by '\n', why not use:
char eol;
int n;
.......
.......
.......
scanf ("%i %c",&n,%eol);
......
......
......


and just ignore the eol.

Arnon David.

0
Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

 
LVL 1

Expert Comment

by:ntdragon
ID: 2607929
in scanf for integer try "%d"
or try to flash the input buffer
about the string to integer there is a func named atoi(...);
0
 

Author Comment

by:anemos
ID: 2608070
how do I flush the input buffer??
0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2608122
i didn't try to do that so you"ll have to check the help

but try to use _flushall();
0
 

Accepted Solution

by:
Alien_SM earned 50 total points
ID: 2608566
There is an easy way to flush the input buffer using
int fflush(FILE *stream);

in the stdio.h unit

fflush(stdin);
works fine for flushing the input buffer of the keyboard
0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2610198
good that there is ppl with a memory better then mine:>
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Way to improve it 16 61
How to design, and implement simple interface 5 134
Global Keyboard Hooks Blocked 4 70
gdb doesn't stop on breakpoint 2 48
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now