0

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++;
      }



 }
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by TArrington21
0

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

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

Edited by vidit_X: n/a

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.