I am trying to read a file into an array and set the min and max values. At this point in my program, I am trying to just read it into an array and then display that array. I still have more to do in the program, but I am just tyring to make sure that I am at least reading the array correct. This is what I have so far, any guidance would be helpful!

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    // variables
    string filename; 
    ifstream input;
    double angle, coeff;
   
   //prompt user for file
   cout << "Enter input file name: ";
   cin >> filename;
   
   //  Open file and read the first data point.
   input.open(filename.c_str());
   
   // will the file open?  send an error if it doesn't open
   if(!input)
   
   {
      cout << "Error opening input file\n";
   }
    
    else
    {  
    // reading data entries 
    input >> angle >> coeff;    
    }
    
    for(int i = 0; i < angle; i++)
    {
    for(int j = 0; j < coeff; j++ )
	{
	    cin >> input[i][j];
		cout << input[i][j];
	}
}
      
      system ("pause");
      return 0;
}

Recommended Answers

All 5 Replies

You have a great idea -- Add code, test often, move on. And your formatting is really good. And comments! Wow!!!!

When asking for help, a blanket "help me" is not good enough. You need to point out what you need help with and describe what's not working. In detail.

More information would have been helpful. I re-read what I typed up, and yes, I didn't provide any problems with the code. Well here is my problem and this is the error that I keep receiving (in line 39 and 40 in the code above):

no match for 'operator[]' in 'input'

input is not data. It's your ifstream object.
You haven't defined a 2d Matrix to hold the input.

Okay, I have added to the code. I believe I have defined the 2d matrix now, but it still will not display the array that was read from the file. When I enter the file to read from, the program will end and will not display the array that I read from. I also have to set my min and max point from the array because that will be needed to test the angle that the user inputs. Here is my updated code.

// this program reads the wind tunnel data into an appropriate array(s), 
// it prints a message showing the range of angles that are covered in 
// the data file, and then allows the user to enter a flight-path angle
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    // variables
    string filename; 
    ifstream input;
    double angle, coeff;
   
   //prompt user for file
   cout << "Enter input file name: ";
   cin >> filename;
   
   //  Open file and read the first data point.
   input.open(filename.c_str());
   
   // will the file open?  send an error if it doesn't open
   if(!input)
   
   {
      cout << "Error opening input file\n";
   }
    
    else
    {  
    // reading data entries 
    input >> angle >> coeff;    
    }
    
    // don't forget row starts at 0 to 16 >> total of 17 rows
    // and column starts at 0 to 1 >> total of 2 columns
    double x[17][2];
    double min = x[0][0];
    double max = x[16][0];    
    
    for(int i = 0; i < angle; i++)
    {
    for(int j = 0; j < coeff; j++ )
	{
	    cin >> x [i][j];
		cout << x [i][j];
	}
}
      
      system ("pause");
      return 0;
}

1) Why don't you output angle and coeff when you read them in? Make sure they are correct.

2) Why are you setting max and min to matrix entries that have not been read yet? Set them after the matrix is loaded.

3) If the file error message is displayed, why do you continue as if nothing is wrong? The rest of the program should be in the else condition.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.