Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading in two data files...but not getting an output....

Newbie
 
Join Date: Oct 2006
Location: South Florida
Posts: 10
#1: Oct 21 '06
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?

Expand|Select|Wrap|Line Numbers
  1.  # include<fstream.h> 
  2. # include<iostream.h>
  3. # include<stdio.h>
  4. # include<math.h>
  5. # include<stdlib.h>
  6.  
  7.  
  8. main()
  9. {
  10.     double alt[105],temp[105],rho[105],press[105],alt1[7],z,t,r,p;
  11.     int i,j,a,ie,n;
  12.     z=0;
  13.     FILE*fo;
  14.     fo=fopen("project4.dat","w");
  15.     fprintf(fo,"Atmospheric Conditions\n");
  16.     fprintf(fo,"--------------------------------------------------\n");
  17.     fprintf(fo,"Alt(ft)\tPress(lb/in^2)\tTemp(F)\tDensity(lb/in^3)\n");
  18.     fprintf(fo,"--------------------------------------------------\n\n");
  19.  
  20.     ifstream inf;
  21.     inf.open("atm7.txt",ios::in);
  22.     inf>>ie;
  23.     for(i=1; i<=ie; i++)
  24.     {
  25.     inf>>alt[i]>>temp[i]>>rho[i]>>press[i];
  26.     }
  27.     inf.close();
  28.  
  29.     for(j = 1; (j <=100000) && (alt[j]<=z); j++);
  30.     {
  31.     ifstream fin;
  32.     fin.open("altproject4.txt",ios::in);
  33.     fin>>n;
  34.     for(a=0; alt1[a]<=n; a++)
  35.     {
  36.     t= ((temp[j] +(z-alt1[a]))*(temp[j+1]-temp[j]))/(alt[j+1]-alt1[a]);
  37.     r= ((rho[j] +(z-alt1[a]))*(rho[j+1]-rho[j]))/(alt[j+1]-alt[i]);
  38.     p= ((press[j] +(z-alt1[a]))*(press[j+1]-press[j]))/(alt[j+1]-alt1[a]);
  39.     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);
  40.     }
  41.     fin.close();
  42.  
  43.     }
  44.  
  45. return 0;
  46.  
  47. }
  48.  

arne's Avatar
Expert
 
Join Date: Oct 2006
Posts: 306
#2: Oct 21 '06

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.
Reply