Having trouble trying to read characters from a file into a 2D array. I know this topic has been discussed before but your help will be greatly appreciated. I know there is more efficient ways of writing the entire code but im not that familiar with programing. Any suggestions are welcome.Getting an error at both the getline calls.

#include <cstdlib>
 #include <iostream>
 #include <fstream>
 #include <cmath>
 using namespace std;

  char filename[50];
  ifstream file;
  char inf [22] [80];
  int rows = 0;
  int cols = 0;


 int main()
 {
   cout<< "Enter the name of the file: ";
    cin.getline(filename,50);
    file.open(filename);

    if (!file.is_open()){
        exit(EXIT_FAILURE);
    }
    while(!file.eof() && rows<22 && cols<80) {

     getline(file,inf[rows][cols]);
      if(cols == 80)
      {
          cols=0;
          ++rows;
    getline(file,inf[rows] [cols]);
      }
       cols++;
      }



 }

Recommended Answers

All 2 Replies

You are passing the wrong second parameter to getline(), it expects a string. You are passing it a char..

getline(file,inf[rows][cols]);

You are passing the wrong second parameter to getline(), it expects a string. You are passing it a char..

getline(file,inf[rows][cols]);

Thank you, it works now.

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.