0

I need to create a program that will read numbers from a file and place them into an array. I have most of my program mapped out. I am confused on how to use a loop in my main function to place the values into an array after reading them from the input file. Should I create a boolean or make a pointer?

My input file: There are 6 cols and 4 rows.     

    89 93 23 89 78 99
    95 21 87 92 90 89
    94 88 65 44 89 91
    77 92 97 68 74 82 

Code    

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string> 

using namespace std; 

//Function prototypes
void openInputFile(ifstream &, string str); 
void printArray();
int getTotal(int[], int, int); 
int getAverage(int); 
int getStandardDeviation(); 
int getRowTotal(int[], int); 
int getHighestInRow(int[], int); 
int getColumnTotal(int[], int); 
int getHighestInColumn(int[], int);

const int COLS = 6; 
const int ROWS = 4; 
int main()
{
    //Variables 
    int num; 
    int arr[ROWS][COLS] = {}; 

    ifstream inFile;

    string fileName = "C:\\Users\\Lisa\\Desktop\\a3_data.txt";

    openInputFile(inFile, fileName); //Call to open input file

    //For loop will read each value from file and place it in array
    while (inFile >> num)
    {
        cout << num << " "; 
    }

    //Close input file
    inFile.close(); 

    system("PAUSE");

    return 0;
}
//end of main function 

//To open input file
void openInputFile(ifstream &inFile, string fileName)
{
    //Open the file
    inFile.open(fileName);

    //Input validation
    if (!inFile)
    {
        cout << "Error to open file." << endl;
        cout << endl;
        return; 
    }
}
//end of OpenInputFile
Mary
  • 211
  • 1
  • 8
  • 18
  • I don't understand the question fully. Given that input, what should the array look like? – Anko - inactive in protest Mar 19 '15 at 20:04
  • The array should look like the input file. I am supposed to read numbers from the file that is in that format and place those values into an array. I don't understand or know a method that will allow me to do that. – Mary Mar 19 '15 at 22:09
  • Duplicate of http://stackoverflow.com/q/27485504/1339615 -- unless you can use vectors; if you can, I'm sure there's a duplicate for that too. – Julian Mar 19 '15 at 22:57
  • The array should look like the input file. – Mary Mar 21 '15 at 00:39

2 Answers2

0

Using your example that you have given I have expanded on your code. If you would like to use a different way to read in the file go here: What is the most elegant way to read a text file with c++?

This only works with the file you have given (a3_datat.txt), if the file will vary then you will need more error checking for values and to also make sure the matrix is the proper dimensions.

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

const int COLS = 6;
const int ROWS = 4;

//Function prototypes
void openInputFile(std::ifstream &, std::string str);
void populate(int[][COLS], std::ifstream &);
void printArray(int[][COLS]);

int main()
{
    //Variables
    std::ifstream inFile;
    std::string fileName = "a3_data.txt";
    int array[ROWS][COLS]={0};

    openInputFile(inFile, fileName); //Call to open input file

    populate(array, inFile);

    printArray(array);

    //Close input file
    inFile.close();

    return 0;
}
//end of main function 

//To open input file
void openInputFile(std::ifstream &inFile, std::string fileName)
{
    //Open the file
    inFile.open(fileName.c_str());

    //Input validation
    if (!inFile)
    {
        std::cout << "\nError to open file." << std::endl;
        std::cout << std::endl;

        /*you'll probably want to do something about the file 
        not being found at this point*/

        return;
    }
}
//end of OpenInputFile

/*This will populate the array with 
the contents of your file */
void populate(int a[][COLS], std::ifstream &inFile)
{
        int row = 0;
        std::string line;

        while ( getline (inFile, line) ) {
            std::stringstream iss(line);

            for (int col = 0; col < COLS; col++) {
                iss >> a[row][col];
            }
            row++;
        }
}

void printArray(int a[][COLS])
{ 
    /*Nested loops to traverse the multidimensional array*/
    for (int row = 0; row < ROWS; row++) {
        for (int col = 0; col < COLS; col++) {
           std::cout << a[row][col] << " ";
        }
        std::cout << std::endl; /*at the end of each row start at the next line*/
    }
}

//end of printArray
Community
  • 1
  • 1
Lee L.
  • 1
  • 1
0

I figured it out. I need to create int arr[][] and int tempArray[][] and set it to a 1-D array first and then set it to a 2-D array using an inFilePtr while reading the file.

Mary
  • 211
  • 1
  • 8
  • 18