?
Solved

scanf and new line character

Posted on 2000-03-11
9
Medium Priority
?
2,054 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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 150 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

Independent Software Vendors: 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

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Suggested Courses

764 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