Thread: Help with Programming Assignment

  1. #16
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    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.

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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;
    }

  3. #18
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    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
    }

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.

    Here are some other things you should consider. Reading from a file is a relatively slow process, so you want to read from a file as few times as possible. At the top of your program you read all the data into two arrays(which by the way is conceptually the same as using one 2d array, but in your case they just aren't joined at the hip, so to speak). Then later in the program you inexplicably read in all the data again, and if that wasn't enough, you read in all the data a third time! Once you read the data into your arrays the first time, you are done reading from the file. Close it. The data is in your two arrays, and you can access it anytime you want. It's the exact same data as what's in the file.
    -------
    Code:
    if (b = range[j]}
    {
           a = range[j];
           f_a = coef[j];
    }
    There's a difference between the assignment operator(=) and the comparison operator(==). The assignment operator is used to assign a value to a variable, and the comparison operator is used to compare two values.
    Last edited by 7stud; 11-08-2005 at 05:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM