array not reading data from file in c++ -
i have program uses 2d array store values. have 2 questions. first program isnt reading data text file correctly. when prints out numbers in array, zeros. wrong 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 print outfile.open ("myoutfile.txt"); // title , heading outfile << "\nname , grade average\n"; cout << "name , grade average.\n\n"; // open , reads .txt file 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 can single 2d array hold both char data type , int type? or have data in .txt file char , convert numbers ints?
an example of how appreciated.
first bug: declaration of ary
isn't reserving space. must give number both dimensions.
second, can make array of 2 different things putting things in structure.
struct { char c; int i; }; ary [max_rows][13];
Comments
Post a Comment