I'm attempting to write a program that has a declared array that it displays the output of (easy part).
Then it has to open a file and read each row of the file which is comma separated filling the array until it hits a special character. At which point it starts printing the loop again with the next set of data. For example
data file looks like this:
1,3
4,5
6,7
-1
2,1
4,5
6,7
-1
etc.
the output then would look like this
Row 1 1 3
Row 2 4 5
Row 3 6 7
Total even numbers = 2
#include <iostream>
#include <fstream>
#define max_rows 3
#define max_columns 2
usingnamespace std;
int even (int A[3][2], int length, int width);
int main()
{
int A[max_rows][max_columns]= {{3,2},{4,5},{2,2}};
int length=1;
int width=1;
int row=0;
int column;
ifstream inFile;
string filemessage;
string field;
inFile.open ("inFilePgm2A.dat");
/*If file is opened do the following*/
if (inFile.is_open ())
{
filemessage = "File Succesfully Open";
}
/*Else if it didn't open display the following*/
else
{
filemessage = "***Error Opening File ****";
filemessage = "***Input File Does not exist***";
}
cout<<filemessage<<endl;
cout << " Column Column" << endl;
for(int i=0; i<3; i++) //This loops the rows.
{
cout<< "Row number:"<< row++ << " ";
for(int j=0; j<2; j++) //This loops the columns
{
cout << " "<< A[i][j] << " ";
}
cout << endl;
}
cout<<"Total Even Numbers = "<<even(A, length, width)<< endl;
/*while not at the end of the file do the following*/
while (!inFile.eof ())
{
}
return 0;
}
int even(int A[3][2],int length, int width)
{
int counter=0;
for (length=0; length<3; length++)
for (width=0; width<2; width++)
{
int even=A[length][width]%2;
if(even==0)
{
counter++;
}
}
return counter;
}
I know it's at the "While ! EOF" that I need the loop, i'm just not sure how to read each integer into the array and then clear the whole array once it detects a -1.
Then it has to open a file and read each row of the file which is comma separated filling the array until it hits a special character. At which point it starts printing the loop again with the next set of data.
The psuedo code would look something like:
1 2 3 4
open file stream
repeat
done = read(file stream, array)
until done
Does that make sense to you? Can you write the two pieces?