Help with correct use of __fpurge()


 
Thread Tools Search this Thread
Top Forums Programming Help with correct use of __fpurge()
# 1  
Old 12-12-2009
Help with correct use of __fpurge()

I'm having a problem with the output of my code and I'm wondering if __fpurge() might be my solution.

Here is the code:

Code:
#include <stdio.h>

float bonus(char x);

int main()
{
	char	name[10];
	char	level;
	float	b;

	printf("Enter employee name: ");
	fgets ( name, 10, stdin );
	printf("Enter bonus level (0, 1, or 2): ");

	level = getchar();
	b = bonus(level);
	b *= 100;

	printf("%s's bonus is $%.2f.\n", name, b);

	return 0;
}

//	Calculate the bonus

float bonus(char x)
{
	if ( x == '0' ) return 0.33;
	if ( x == '1' ) return 1.50;
	return 3.10;
}

The output is:
Code:
j@j:~/coding$ ./bonus 
Enter employee name: Jason
Enter bonus level (0, 1, or 2): 0
Jason
's bonus is $33.00.

I don't understand why the " 's bonus ... " is being printed on the line below.

Thanks in advance for any suggestions!


J
# 2  
Old 12-13-2009
replace
Code:
fgets ( name, 10, stdin );

with
Code:
gets ( name);

and the resualt wil be like

Code:
root@kf-opensolaris:/tmp# ./a.out 
Enter employee name: myname
Enter bonus level (0, 1, or 2): 1
myname's bonus is $150.00.


fgets:
Reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. The newline character is copied to the string.
gets:
Reads a line from stdin and stores it into the string pointed to by str. It stops when either the newline character is read or when the end-of-file is reached, whichever comes first. The newline character is not copied to the string.

Last edited by ironmask2004; 12-13-2009 at 07:46 AM..
# 3  
Old 12-13-2009
I wouldn't use gets (it's use is not recommended).

The problem is that fgets stores the newline when you press Enter. There's a few ways to remove it:

Code:
#include <stdio.h>
#include <string.h>

float bonus(char x);

int main()
{
        char    name[10];
        char    level;
        float   b;

        printf("Enter employee name: ");
        fgets ( name, 10, stdin );
        name[strcspn ( name, "\n" ) ] = '\0';
        printf("Enter bonus level (0, 1, or 2): ");

        level = getchar();
        b = bonus(level);
        b *= 100;

        printf("%s's bonus is $%.2f.\n", name, b);

        return 0;
}

# 4  
Old 12-13-2009
Thanks for the feedback.

About gets() .. my compile doesn't compile if I use it so I had to learn to use fgets() instead. I thought the error was coming from there which is why I tried to use __fpurge (I've used it with getchar() successfully).
# 5  
Old 12-14-2009
Quote:
Originally Posted by scottn
I wouldn't use gets (it's use is not recommended).
This is because gets() doesn't take a buffer size. Anyone can cause a buffer overflow just by knowing how large your buffer is.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Get correct mail id

Hi Team, I have below data , could you please help to get correct mail id in oracle only. bhatt,rabi rabi.bhatt@n.com, test, mishra test.mishra@n.com, skype, amit skype.amit@n.com, output like :rabi.bhatt@n.com test.mishra@n.com skype.amit@n.com Please use CODE tags as required... (6 Replies)
Discussion started by: Jewel
6 Replies

2. Shell Programming and Scripting

Can some one correct this script

Hi, I tried writing a script and there was a problem with SFTP part can some one correct where is is the mistake Enveronment file #!/bin/bash export HOST_NAME=<> export USER_NAME=<> export PASSWORD=<> export SOURCE_PATH=/u03/informatica/current/server/infa_shared/TgtFiles/mfg export... (4 Replies)
Discussion started by: spradeep86
4 Replies

3. UNIX for Advanced & Expert Users

I was trying this command...am I going correct? other there is better way

I was trying to copy all debs from apt cache to some storage location and I was taking this approach... /var/cache/apt/archives# ls -1 | grep -v jdownloader | fgrep .deb | xargs cp /media/eshant/L-STORE/Softwares/openjdk/an error bla_bla.deb is a not directory stalled me Suggestions please... (9 Replies)
Discussion started by: ezee
9 Replies

4. Shell Programming and Scripting

Please Correct My script

############### #filename.sh ############### CUREENT_DATE=02 log_file_path="$CUREENT_DATE"-"${0##%*/}`|cut -d "." -f1|awk -F "/" '{print $NF}'`"".log" echo $log_file_path ################ #output required 02-filename.log (6 Replies)
Discussion started by: mohitmehral
6 Replies

5. Solaris

in correct drive name

I am new to solaris and I replaced a faulty tape drive sun DLT7000 But, I am getting the follwoing error when system reboots ltid deamon error drive index 1 is not correct, drive name /dev/rmt/2cbn is incorrect no such file or directory. I have two drives the other one is /dev/rmt/0cbn,... (8 Replies)
Discussion started by: latif1958
8 Replies

6. Shell Programming and Scripting

Please correct this

I have input file like this Input file: ABC|abc_etc_passwd XYZ|XYZ_etc_passwd zXY|XYZ_etc_passwd IJK|test_etc_passwd KLM|test_etc_passwd i want to do following in a loop. grep 'ABC' *abc_etc_passwd* grep 'XYZ' *XYZ_etc_passwd* grep 'ZXY' *ZXY_etc_passwd* i have tried this for i... (2 Replies)
Discussion started by: pinnacle
2 Replies

7. Shell Programming and Scripting

can u please confirme the correct

st1=hello st2=world if && || ] ] ..... .... fi (5 Replies)
Discussion started by: mail2sant
5 Replies

8. Shell Programming and Scripting

Count not correct

the count is off ... man ... help please. The Code open (FILE1, "xy1.TXT") or die "$0: Could not open SOURCEFILE.TXT: $!\n"; open (FILE2, "xy2.TXT") or die "$0: Could not open RESULTFILE.TXT: $!\n"; chomp(my @strings = <FILE2>); while (1) { foreach $pattern (<FILE1>) { ... (3 Replies)
Discussion started by: popeye
3 Replies

9. Shell Programming and Scripting

Is the script correct ???

Dear Collegues is the below given is correct ? #!/usr/bin/perl $a = @ARGV; while ($a = @ARGV) { exec "./jagan ../dat/ml_in @ARGV"; } Jagan (0 Replies)
Discussion started by: jaganadh
0 Replies
Login or Register to Ask a Question