![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
| Display Modes | Rate This Thread |
|
|