Solved

scanf and new line character

Posted on 2000-03-11
9
2,050 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

730 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