Hello, I need to take a file filled with numbers and input them into a two-dimensional array. The file would look like:
3 6
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4
where the first two numbers are the vertical and horizontal dimensions of the array. I have to do this by using pointer and new.
Now, I've gotten it partly done, but I've gotten stuck reading the numbers into the array. Each row is the test scores for a student, so each horizontal array is a different student. It says I cannot convert float* to float in assignment, but I don't know exactly know how I can use pointers and what I am doing wrong. Below is my code. And yes, this is homework, but I have looked in my textbook, all over the web, and can't figure out what exactly I am doing wrong. Please help me. Thank you.

#include <iostream>
#include <fstream>
#include <cstdlib>

typedef float* Pointer_f_array;
    
int main ()
{
    using namespace std;
    ifstream in_stream;
    char file_name_input[21];
    int array_size_vertical;
    int array_size_horizontal;
    
    
    cout << "Enter the file name of your input file (maximum of 20 characters):\n";
    cin >> file_name_input;
    in_stream.open(file_name_input);
    if (in_stream.fail( ))
    {
      cout << "Input file opening failed.\n";
      exit(1);
    }
      
    in_stream >> array_size_vertical >> array_size_horizontal;
    
    Pointer_f_array p;
    p = new float[array_size_vertical];
    for (int c = 0; c < array_size_vertical; c++)
    {
      p[c] = new float[array_size_horizontal];
    }
    
    for (int d=0; d < array_size_vertical; d++)
    {  
      in_stream.getline (p[d], 100);//array_size_horizontal);
    } 
     
  delete [ ]p;   
  in_stream.close ( );
  return 0;
}

Shouldn't this

3 5
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

be

3 6
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

Yes, good point. But that is not the problem I was having.

I think you'll find this a better way to read a file of floats..

testfile

3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

testit.cpp

#include <iostream>
#include <fstream>

int main()
{
	std::ifstream		fin("testfile");
	if (fin.fail())
	{
		std::cerr << "Could not open file" << std::endl;
		return 1;
	}

	float value = 0.0;

	while (fin >> value)	
	{
		std::cout << value << std::endl;
	}

	fin.close();
	return 0;
}

p[c] is a memory address. What you want is the value of p at c. Try *p[c]=what you need.

When i put:

for (int c = 0; c < array_size_vertical; c++)
    {
      *p[c] = new float[array_size_horizontal];
    }

I get "invalid type argument of `unary *' ".
And I have to have the top two numbers in the file - that's how I am supposed to determine the size of the array.

I tried your problem but used pointers to pointers for the array...

testfile

3 6
3.9 4.5 2.1 1.0 2.4 4.3
3.1 4.2 5.1 6.2 1.0 2.7
1.2 2.3 3.1 4.2 5.2 6.4

testit.cpp

#include <iostream>
#include <fstream>

int main()
{
	int rows = 0;
	int cols = 0;
	int count = 0;
	int row_count = 0;

	float **farray = (float**)NULL;

	std::ifstream		fin("testfile");

	if (fin.fail())
	{
		std::cerr << "Could not open file" << std::endl;
		return 1;
	}

	fin >> rows >> cols;

	farray = new float*[rows * sizeof(float*)];

	for (int i = 0; i < rows; ++i)
		farray[i] = new float[cols * sizeof(float)];

	float value = 0.0;

	while (fin >> value)	
	{
		farray[row_count][count++] = value;
		if (count >= cols)
		{
			count = 0;
			++row_count;	
		}		
	}

	fin.close();

	for (int i = 0; i < rows; ++i)
	{
		for (int j = 0; j < cols; ++j)
		{
			std::cout << farray[i][j] << " ";
		}
		std::endl(std::cout);
	}
	return 0;
}

The code is pretty rough but you can clean it up and see if it works for you..

Thanks. That should work.
A couple questions, though:
Why do you have double asterisks in the declaration for the array?
Why do you use fin rather than ifstream? Is there an advantage to this?
What is std:cerr?
And please explain the following code. I don't exactly know what you're doing here:

farray = new float*[rows * sizeof(float*)];
 
	for (int i = 0; i < rows; ++i)
		farray[i] = new float[cols * sizeof(float)];

Again, thanks for the help. Once I understand what you are doing, I should be able to write my program (using my own work, not copy+paste,of course). But thanks for your knowledge imparted.

std::cerr is an I/O channel that can be used for errors...Its almost the same as std::cout except its not buffered.

Why two asterisks? Its a pointer to a pointer...so two asterisks.

What's this line?

farray = new float*[rows * sizeof(float*)];

That is how you allocate the memory for three float pointers.

This one?

farray = new float[cols * sizeof(float)];

Here I'm allocating the arrays of floats for each of the three pointers.

and I do use ifstream.

So are you multiplying rows times the size of float* and columns times the size of float? I'm sorry, I have never seen this way of declaring the array before.

Thanks for helping. I finally figured out how you were writing your code and used it to finish my program. The completed program is below:

//Description: Input from a datafile an unknown number of student's tests and input them into a two-dimensional array by using pointers and new. Print out the average test score for each student. Delete the pointer arrays.

#include <iostream>
#include <fstream>
#include <cstdlib>
    
int main ()
{
    using namespace std;
    ifstream in_stream;
    char file_name_input[21];
    int array_size_vertical = 0; //number of rows
    int array_size_horizontal = 0; //number of columns
    int horcounter = 0; //used in the array to determine when to input in the next horizontal array
    int vercounter = 0;
    int testcounter = 0; //counts the number of tests for each student
    int student_number = 1; //variable for identifying each student when averages are outputted
    float student_totalgrade = 0;
    float next = 0.0; //variable for array input
    float **array1;
    
    cout << "Enter the file name of your input file (maximum of 20 characters):\n";
    cin >> file_name_input;
    in_stream.open(file_name_input);
    if (in_stream.fail( ))
    {
      cout << "Input file opening failed.\n";
      exit(1);
    }
      
    in_stream >> array_size_vertical >> array_size_horizontal; //first two numbers inputted as vertical and horizontal size of the array
    
    array1 = new float*[array_size_vertical];//declaring vertical size of the array by use of pointers and new
    for (int b=0; b < array_size_vertical; b++)
        array1[b] = new float[array_size_horizontal];//declaring horizontal size of the array for each vertical array
        
    while (in_stream >> next) //while there is another number to input
    {
          array1[vercounter][horcounter++] = next; //put the number in the current array position
          if (horcounter >= array_size_horizontal) //if the next number is in another row, reset the horizontal counter to 0 and go to the next row
          {
                   horcounter = 0;
                   vercounter++;
          }
    }
          
    cout.setf(ios::fixed); //two decimal places for the outputted test scores
    cout.setf(ios::showpoint);
    cout.precision(2);
        
    for (int c=0; c < array_size_vertical; c++) //for each vertical array
    {
        for (int d=0; d < array_size_horizontal; d++) //for each position in the horizontal array
        {
            student_totalgrade=student_totalgrade + array1[c][d];//adding array position value to previous student's sum of test scores
            testcounter++;
        }
        cout << "Student number " << student_number //each student's score is outputted every time the horizontal array finishes
             << "'s average test score is: "
             << (student_totalgrade/testcounter) <<endl;
        student_totalgrade=0; //reset totalgrade counter so each student starts from zero
        testcounter=0; //reset testcounter so each student starts from zero
        student_number++; //identifies each student's record
    }

  for (int b=0; b < array_size_vertical; b++) //finalizes program before the end
        delete[] array1[b];
  delete [ ]array1;
  in_stream.close ( );

  return 0;
}
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.