Sponsored Content
Top Forums Programming Clear standard input buffer for C program in Linux Post 302912420 by Abhishek_kumar on Friday 8th of August 2014 05:48:55 AM
Old 08-08-2014
Ubuntu Clear standard input buffer for C program in Linux

Hello friends! i am writing a code in which i take inputs (numbers) from user and count the total number of positive, negative and zeros entered. I need to clear my standard input buffer before scanf() command. My compiler is completely ignoring the fflush(stdin) command. its not even showing any error!. I am using Linux(ubunto) platform. Help!
Code:
#include<stdio.h>
int main()
{
	int pos=0,neg=0,zeros=0;
	float num;
	char ans='y';
	while(ans=='y')
	{
		printf("Enter a number : ");
		scanf("%f",&num);
		if(num>0)
		{
			pos++;
		}
		else if(num<0)
		{
			neg++;
		}		
		else
		{
			zeros++;
		}		
		
		printf("Wanna enter another number ? (y/n)\n");
		fflush(stdin);			
		scanf("%c",&ans);			
		if(ans=='n')
		{
		printf("positive number : %d \n Negative numbers : %d \n Zeros : %d",pos,neg,zeros);
		}

	}
return 0;
}


Last edited by Abhishek_kumar; 08-08-2014 at 02:27 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

how to clear/clean mbufs (network buffer space)?

When I worked with client-server (socket) programming, I encountered "the socket error# 10055" which means "No buffer space available". This might be a symptom of one or more applications that didn't return system resources (like memory) properly. Temporary solution was to reboot the machine to... (7 Replies)
Discussion started by: dipti
7 Replies

2. Shell Programming and Scripting

standard input

how can i redirect standard input? i dont remember :/, though could you redirec not from a command? i mean, to redirect always stdin and stout (1 Reply)
Discussion started by: Jariya
1 Replies

3. Programming

How to clear the content of a pipe (STDIN) after it is written to another program?

PROGRAM A <-> PROGRAM B PROGRAM A sends data as STDIN ro PROGRAM B and when PROGRAM B is executed from PROGRAM A, it sends output back to PROGRAM A. This is implemented using 2 pipes (fd1 & fd2). The above process happens in a loop and during the second run, the previous data that had been... (10 Replies)
Discussion started by: vvaidyan
10 Replies

4. Programming

Clear Serial Port Buffer Using iclear, iflush

Hello, I am having trouble clearing the serial port buffer using the iclear and iflush commands. The code runs without errors being returned, but when I check the buffer again there is still data. The only way I have so far is to read until there is nothing left in the buffer. Shouldn't one... (1 Reply)
Discussion started by: sammy-e
1 Replies

5. Solaris

standard input

Please give me any example for standard input in Solaris. (6 Replies)
Discussion started by: karman0931
6 Replies

6. UNIX for Dummies Questions & Answers

invoking non-standard program in linux

I have a program R installed on Ubuntu under /usr/bin/R. I also have a different version installed under /home/user/R. I would like to invoke the locally installed program temporarily. How can I do this automatically by running a script and then switching to the default program? thanks, SM (2 Replies)
Discussion started by: smeme
2 Replies

7. HP-UX

How to clear keyboard buffer

Hi, How can i clear the command. Suppose using esc k i retrieved teh command, but for some reason I want to clear and type fresh one... how can i do that. thx (2 Replies)
Discussion started by: bang_dba
2 Replies

8. AIX

Need to clear the keyboard buffer

I'm running a ksh script that requires user interaction, and said users (myself among them) may get a little trigger happy and get ahead of ourselves and accidentally key in the wrong responses to future prompts in the script. Naturally, I'd like to prevent that, so how does one clear that... (0 Replies)
Discussion started by: Michael Mullig
0 Replies

9. UNIX for Advanced & Expert Users

Input buffer and READ

Hello everyone, Can someone please explain the input buffer behaviour for the read command in ksh93 on AIX? I have 'googled' for weeks now, and did not find a satisfactory answer or solution to my dilemma. I have the following code: STTY=$(stty -g) if ;then stty -echo -icanon time 0 min... (1 Reply)
Discussion started by: gio001
1 Replies

10. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies
frexp(3M)						  Mathematical Library Functions						 frexp(3M)

NAME
frexp, frexpf, frexpl - extract mantissa and exponent from a floating-point number SYNOPSIS
cc [ flag... ] file... -lm [ library... ] #include <math.h> double frexp(double num, int *exp); float frexpf(float num, int *exp); long double frexpl(long double num, int *exp); DESCRIPTION
These functions break a floating-point number into a normalized fraction and an integral power of 2. They store the integer exponent in the int object pointed to by exp. RETURN VALUES
For finite arguments, these functions return the value x, such that x is a double with magnitude in the interval [1/2, 1) or 0, and num equals x times 2 raised to the power *exp. If num is NaN, NaN is returned and the value of *exp is unspecified. If num is +- 0, +- 0 is returned and the value of *exp is 0. If num is +-Inf, num is returned and the value of *exp is unspecified. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
isnan(3M), ldexp(3M), modf(3M), attributes(5), standards(5) SunOS 5.10 16 Aug 2004 frexp(3M)
All times are GMT -4. The time now is 02:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy