Reading a CSV file into an array and looping on a special character

Hey guys/gals,

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

Row 1 2 1
Row 2 4 5
etc. etc.



What is have is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#define max_rows 3 
#define max_columns 2 
using namespace 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.

Any guidance would be appreciated.

Thanks
Last edited on
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?
Topic archived. No new replies allowed.