Reading in two data files...but not getting an output.... | Newbie | | Join Date: Oct 2006 Location: South Florida
Posts: 10
| |
This program is supposed to read in a text file with values of altitudes ranging from 0-100,000 increasing by 1000, with these altitude values there are corresponding pressure, temperature, and density values. The program also has to read in another text file with the following altitude values 8250,15360,34840,55685,72470,94522. The purpose of the program is to use the first text file to produce pressure, temperature, and density values for the altitudes of the second text file. I wrote the program and it is giving me an output but no numerical values for z, p, t, and r. Can someone take a look at it? - # include<fstream.h>
-
# include<iostream.h>
-
# include<stdio.h>
-
# include<math.h>
-
# include<stdlib.h>
-
-
-
main()
-
{
-
double alt[105],temp[105],rho[105],press[105],alt1[7],z,t,r,p;
-
int i,j,a,ie,n;
-
z=0;
-
FILE*fo;
-
fo=fopen("project4.dat","w");
-
fprintf(fo,"Atmospheric Conditions\n");
-
fprintf(fo,"--------------------------------------------------\n");
-
fprintf(fo,"Alt(ft)\tPress(lb/in^2)\tTemp(F)\tDensity(lb/in^3)\n");
-
fprintf(fo,"--------------------------------------------------\n\n");
-
-
ifstream inf;
-
inf.open("atm7.txt",ios::in);
-
inf>>ie;
-
for(i=1; i<=ie; i++)
-
{
-
inf>>alt[i]>>temp[i]>>rho[i]>>press[i];
-
}
-
inf.close();
-
-
for(j = 1; (j <=100000) && (alt[j]<=z); j++);
-
{
-
ifstream fin;
-
fin.open("altproject4.txt",ios::in);
-
fin>>n;
-
for(a=0; alt1[a]<=n; a++)
-
{
-
t= ((temp[j] +(z-alt1[a]))*(temp[j+1]-temp[j]))/(alt[j+1]-alt1[a]);
-
r= ((rho[j] +(z-alt1[a]))*(rho[j+1]-rho[j]))/(alt[j+1]-alt[i]);
-
p= ((press[j] +(z-alt1[a]))*(press[j+1]-press[j]))/(alt[j+1]-alt1[a]);
-
fprintf(fo,"z=%6.0f ft\t\tt=%7.2f F\t\tr=%8.5f lb/in^3\t\tp=%8.5flb/^in2\n",z,t,r,p);
-
}
-
fin.close();
-
-
}
-
-
return 0;
-
-
}
-
|  | Expert | | Join Date: Oct 2006
Posts: 306
| | | re: Reading in two data files...but not getting an output.... Quote:
Originally Posted by swtstrawberry This program is supposed to read in a text file with values of altitudes ranging from 0-100,000 increasing by 1000, with these altitude values there are corresponding pressure, temperature, and density values. The program also has to read in another text file with the following altitude values 8250,15360,34840,55685,72470,94522. The purpose of the program is to use the first text file to produce pressure, temperature, and density values for the altitudes of the second text file. I wrote the program and it is giving me an output but no numerical values for z, p, t, and r. Can someone take a look at it?
# include<fstream.h>
# include<iostream.h>
# include<stdio.h>
# include<math.h>
# include<stdlib.h>
main()
{
double alt[105],temp[105],rho[105],press[105],alt1[7],z,t,r,p;
int i,j,a,ie,n;
z=0;
FILE*fo;
fo=fopen("project4.dat","w");
fprintf(fo,"Atmospheric Conditions\n");
fprintf(fo,"--------------------------------------------------\n");
fprintf(fo,"Alt(ft)\tPress(lb/in^2)\tTemp(F)\tDensity(lb/in^3)\n");
fprintf(fo,"--------------------------------------------------\n\n");
ifstream inf;
inf.open("atm7.txt",ios::in);
inf>>ie;
for(i=1; i<=ie; i++)
{
inf>>alt[i]>>temp[i]>>rho[i]>>press[i];
}
inf.close();
for(j = 1; (j <=100000) && (alt[j]<=z); j++);
{
ifstream fin;
fin.open("altproject4.txt",ios::in);
fin>>n;
for(a=0; alt1[a]<=n; a++)
{
t= ((temp[j] +(z-alt1[a]))*(temp[j+1]-temp[j]))/(alt[j+1]-alt1[a]);
r= ((rho[j] +(z-alt1[a]))*(rho[j+1]-rho[j]))/(alt[j+1]-alt[i]);
p= ((press[j] +(z-alt1[a]))*(press[j+1]-press[j]))/(alt[j+1]-alt1[a]);
fprintf(fo,"z=%6.0f ft\t\tt=%7.2f F\t\tr=%8.5f lb/in^3\t\tp=%8.5flb/^in2\n",z,t,r,p);
}
fin.close();
}
return 0;
} By non-numerical values you something like nan, inf, -inf and the like?
Check (print out) the values you are using for the calculation of t, r, and p, i.e. temp[j], alt1[a] and the like. You may divide by zero, which yields an undefined result in the case of floating point arithmetic.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|