Solved

scanf and new line character

Posted on 2000-03-11
9
2,039 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

744 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

10 Experts available now in Live!

Get 1:1 Help Now