user2371621
user2371621

Reputation: 29

array not reading data from file in c++

I have a program that uses a 2d array to store some values. I have 2 questions. First My program isnt reading the data from my text file correctly. When it prints out the numbers in the array, I get all Zeros. What is wrong with my code?

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

using namespace std;

int ary [][13];

ofstream OutFile;
ifstream infile("NameAvg.txt");

//function prototype
void ReadIt (int [][13]); // Function call  ReadIntoArrayByRows(ary);
void WritePrintOutArray (int [][13]);

int main()
{
    //open/creates file to print to
    OutFile.open ("MyOutFile.txt");

    // Title and Heading 
    OutFile << "\nName and grade average\n";
    cout << "Name and grade average.\n\n";

    // Open and Reads .txt file for array
    ReadIt(ary);
    OutFile<<"\n-----------------------------"<<endl;
    cout<<"\n-----------------------------"<<endl;

    WritePrintOutArray(ary);
    OutFile<<"\n-----------------------------"<<endl;
    cout<<"\n-----------------------------"<<endl;

    //closes .txt file
    OutFile.close();
    cin.get();
    cin.get();
    return 0;
}

void WritePrintOutArray(int ary[][13])
{
    int col,
        row;
    for(row=0;row<2;row++)
    {
    for(col=0;col<8;col++)
    {
        ary[2][13];
        cout<<ary[row][col];
        OutFile<<ary[row][col];
    }
    cout<<endl;
    OutFile<<endl;
    }
 }

 void ReadIt(int ary[][13])
 {
    int col,
        row=0;

    while(infile>>ary[row][0])
    {
    for(col=1;col<13;col++)
    {
        infile>>ary[row][col];
        row++;
    }
    infile.close();
    }
 }

My second question is can a single 2d array hold both a char data type and an int type? Or will I have to get all the data in the .txt file as a char and then convert the numbers into ints?

an example of how to do this would be greatly appreciated.

Upvotes: 0

Views: 759

Answers (2)

LihO
LihO

Reputation: 42083

There is no memory associated with the following array:

int ary [][13];

and I wonder why your compiler doesn't complain with something like "storage size of ‘ary’ isn’t known". Anyway you should use std::vector instead. As to: "can a single 2d array hold both a char data type and an int type?" ~> you could define a custom type or alternatively you might use:

std::vector< std::vector< std::pair<char, int> > > grades;

Although while looking at this matrix of pairs... something seems to be wrong and I'm sure that whatever is that you're trying to accomplish, there is a simpler way.

Also try to avoid using global variables. The way you use them makes the decomposition of your code into functions a bit useless. Your code is way too procedural for C++.

Upvotes: 0

Mark Ransom
Mark Ransom

Reputation: 308196

First the bug: your declaration of ary isn't reserving any space. You must give a number for both dimensions.

Second, you can make an array of two different things by putting those things in a structure.

struct It
{
    char c;
    int i;
};

It ary [MAX_ROWS][13];

Upvotes: 1

Related Questions