# Help with Programming Assignment

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 11-07-2005
JHaney
I went back to the if (!tunnel.eof()) because I was not able to get a while statement reading the range. I'm going to talk to other people in my class tomorrow and see if I can figure something out.
• 11-07-2005
7stud
Have you been able to read a single value from your file?

See if you can get the following program to work. It requires a file called data.txt to be in the folder: C:\TestData, and containing your data.
Code:

```#include <iostream> #include <fstream> using namespace std; int main() {         ifstream tunnel("C:\\TestData\\data.txt");         if(!tunnel)         {                 cout<<"couldn't open file"<<endl;                 return 1;         }                 double angle = 0;         double lift = 0;                 tunnel>>angle>>lift;         cout<<angle<<" "<<lift<<endl;         return 0; }```
• 11-07-2005
JHaney
Here is what I ended up with. It does all of the correct things that are supposed to happen with the code that the teacher wanted it to output. It does in between values but if I put in say -4 or -2 or 0 or someother angle that is in the data file it tries to compute the in between value.

I'm not sure about how to do a flight-path angle that is equal to one of the numbers in the data file. I've tried doing:
Code:

```if (b = range[j]} {       a = range[j];       f_a = coef[j]; }```
But it will cout "Lift coefficient for -2 degrees -0.056" for any number entered. Any suggestions how to to get it to cout "Lift coefficient for (any degree entered that is in data file) degrees (coefficient in data file).

Code:

```#include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() {         // Declare variables         double range[17], a, b, c;         double coef[17], f_a, f_b, f_c;         char repeat, N, n;         ifstream tunnel;         int j;         // open data file "tunnel.txt"         tunnel.open("tunnel.txt");         if (tunnel.fail())        // Check to see if file opened         {                 cout << "Error opening data file 'tunnel.txt'" << endl;         }         else        // If opened successfully, continue with code         {                 for (int i = 0; i <= 16; i++)                 {                         tunnel >> range[i] >> coef[i];                 }                 cout << "Range of flight-path angles:" << range[0] << " to " << range[16] << " degrees" << endl;                 cout << endl;                 do                 {                         cout << "Please enter a flight-path angle in degrees: ";                                 cin >> b;        // Get flight path angle from user                         if (b < range[0] || b > range[16]) // Test to see if angle is outside flight-range                         {                                 cout << "**** Sorry, " << b << " is not within data range ****" << endl;                                 cout << endl;                                 cout << "Would you like to enter another value (Y/N)?";                                         cin >> repeat;                                 cout << endl;                         }        // If angle is inside flight-range                         else                         {                                 for (j = 0; j < 17; j++)        // Find values of a and f_a                                 {                                         tunnel >> range[j] >> coef[j];        // Read data into two dimensional array                                         if(b > range[j])                                         {                                                 a = range[j];                                                 f_a = coef[j];                                         }                                 }                                 for (j = 17; j > 0; j--)        // Find values of c and f_c                                 {                                         tunnel >> range[j] >> coef[j];        // Read data into two dimensional array                                                                                 if(b < range[j])                                         {                                                 c = range[j];                                                 f_c = coef[j];                                         }                                 }                                 f_b = f_a + (((b - a) / (c - a)) * (f_c - f_a));        // Computer lift coef with use of linear interpolation                                 cout << endl;                                 cout << "Lift coeffiecient for " << b << " degrees: " << f_b << endl;                                 cout << "Would you like to enter another value (Y/N)?";                                         cin >> repeat;                                 cout << endl;                         }                 }                 while (!(repeat == 'N' || repeat == 'n'));        // Conditon for do loop         }         tunnel.close();                // Closes "tunnel.txt"         return 0; // Program ended successfully }```
• 11-08-2005
7stud
Your code may work but it has a potential to crash if there's a stream error while reading from the file. I explained the reasons why in earlier posts. Your program still suffers from those deficiencies, but to your credit you've managed to cobble together a Rube Goldberg-esque program that apparently works.

```if (b = range[j]} {       a = range[j];       f_a = coef[j]; }```