The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Floating Point Exception Mandar123 Linux 3 01-22-2009 09:51 AM
Floating point exception !!! ssk01 Linux 3 12-24-2008 02:20 PM
Floating point error in C Hara High Level Programming 2 06-18-2008 05:43 AM
Rounding off the value of Floating point value damansingh Shell Programming and Scripting 7 05-21-2008 09:46 AM
Floating Point Division gsatch Shell Programming and Scripting 1 07-25-2002 04:03 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 03-11-2009
headrush headrush is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 1
floating point error in linux + C

Here's a program and its pretty simple .It requires file handling and some calculations but on running it I am not getting the required result.It seems that the code outside the file read's outer while loop is not executing e.g the print statement is not being printed.Plz Help!

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main(int argc,char *args[])
{
FILE *fp;
char ch;
int load=0,value=0,weight=0;
int i=0,count=0,p,k=1,j,*cost,line=1,u;
if(argc!=2){
printf("\nInsufficient no. of arguments");
exit(0);
}
fp=fopen(args[1],"r");
if(fp==NULL){
printf("\nFile could not be opened");
exit(0);
}
while((ch=fgetc(fp))!='\n'){
if(isdigit(ch)){
p=ch-'0';
load=load*10+p;
}
}
printf("\nload= %d \n",load);
i=0;
while((ch=fgetc(fp)!=EOF)){
value=0;
weight=0;
k=0;
u=0;
while(ch!='\n'){
// printf("\nline=%d,weight=%d,value=%d",line,weight,value);

if((isalpha(ch)||isdigit(ch))&&k==0){
while(ch!=' '){
ch=fgetc(fp);
}
k++;
}
if(weight==0){
while(ch!=' '){
p=ch-'0';
weight=weight*10+p;
ch=fgetc(fp);
}
}
// printf("\nweight=%d",weight);
if(value==0){
while(ch!='\n'){
p=ch-'0';
value=value*10+p;
ch=fgetc(fp);
}
}
// printf("\nValue=%d",value);
if(ch==' '){
while(ch==' '){
ch=fgetc(fp);
}
}
}

u=value/weight;
printf("line=%d weight=%d,value=%d,cost=%d\n",line,weight,value,u);
line++;
}
// count=i-1;
fclose(fp);
return 0;
}

The text file that I am using is strictly of the following format:
1250
LJS93K 1300 10500
J38ZZ9 700 4750
HJ394L 200 3250
01IE82 75 10250

there is a line-break after each line and the first line(1250) is the "load" value,the first column of every is to be avoided the 2nd column is weight and the 3rd column is value.
One more thing I am using ubuntu distro which uses bash shell!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0