Question

I'm working a bit more with arrays and reading from files to try and get a deeper understanding of them, so I apologize if I ask a lot of questions in regards to that.

I currently have a program that is supposed to read characters from a file and then store those characters as strings into a 2D array. As an example, this file contains a header number and a list of names:

5
Billy
Joe
Sally
Sarah
Jeff

So the 2D array in this case will have 5 rows and x number of columns (one row per names). The program reads the file one char at a time. I think what I'm having problem with is actually inserting the null terminator at the end of each row to indicate that it's the end of that string, but overall, I'm not sure what's going wrong. Here is my code:

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

const int MAX_NAME_LENGTH = 50;

void printNames(char [][MAX_NAME_LENGTH + 1], int);

int main(void)
{
    ifstream inputFile;
    string filename;
    int headernum, i = 0, j;
    const int MAX_NAMES = 10;
    char ch;
    char names[1][MAX_NAME_LENGTH + 1];

    cout << "Please enter the name of your input file: ";
    cin >> filename;

    inputFile.open(filename.c_str());

    if (inputFile.fail())
    {
        cout << "Input file could not be opened. Try again." << endl;
    }

    inputFile >> headernum;

    if (headernum > MAX_NAMES)
    {
        cout << "Maximum number of names cannot exceed " << MAX_NAMES << ". Please try again." << endl;
        exit(0);
    }

    inputFile.get(ch);

    while (!inputFile.eof())
    {
        for (i = 0; i < headernum; i++)
        {
            for (j = 0; j < MAX_NAME_LENGTH; j++)
            {
                if (ch == ' ' || ch == '\n')
                {
                    names[i][j] = '\0';
                }

                else
                {
                    names[i][j] = ch;
                }
            }
        }

        inputFile.get(ch);
    }

    cout << names[0] << endl;
    //printNames(names, headernum);

    return 0;
}

void printNames(char fnames[][MAX_NAME_LENGTH + 1], int fheadernum)
{
    int i;

    for (i = 0; i < fheadernum; i++)
    {
        cout << fnames[i] << endl;
    }
}

It compiles, and here is the output: http://puu.sh/7pyXV.png

So obviously something is very wrong here! I'm incline to say the specific problem lies with my if (ch = ' ' etc) statement, but it could be much more than that, I'm sure. I'm just having trouble figuring out where the problem lies. As always, help and/or guidance is very much appreciated!

Was it helpful?

Solution

Now that you have some feedback on your initial code. Here is a much simpler way of doing this (and a bit more c++ like):

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char **argv)
{
  ifstream inputFile;
  string filename;

  cout << "Please enter the name of your input file: ";
  cin >> filename;

  inputFile.open(filename.c_str());

  if (inputFile.fail())
  {
      cout << "Input file could not be opened. Try again." << endl;
      return 1;
  }

  int headerNum = 0;
  inputFile >> headerNum;
  if(inputFile.eof()) {
      cout << "Error reading input file contents." << endl;
      return 1;
  }

  string *names = new string[headerNum];
  for(int i = 0; i < headerNum; i++)
    inputFile >> names[i];

  for(int i = 0; i < headerNum; i++)
    cout << names[i] << endl;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top