Pulling information from files

Im not asking anyone to write this for me, I am a beginner and need help getting the data for the columns so i can perform my function to output the answers. I only know how to do the basic stuff so all the fancy code on this site will unfortunately not help.


ASSIGNMENT ; write a C++ program to analyze a small file of data called (police.text).

This is the file:

6
2004 469250 144628 95372 28822 15970 454
2005 453516 140800 85428 27035 16036 453
2006 447845 135298 86119 25907 15959 478
2007 436685 131767 84998 26254 15443 448
2008 425781 119785 88161 25431 16699 515
2009 392097 111796 80254 22596 15942 461

The first line contains an integer N denoting 6 rows of data. Each row that follows denotes one year of crime data,
and contains 7 integers:

1. Year
2. Total number of crimes reported that year
3. Total number of arrests made that year
4. Total number of thefts that year
5. Total number of assaults that year
6. Total number of robberies that year
7. Total number of homicides that year

The file is always in order by consecutive, increasing year. I have to analyzes this data, and outputs the following information for each year:

 Total # of crimes reported
 Percentage of crimes for which arrests were made
 Total # of homicides
 In comparison to previous year, is the # of homicides UP or DOWN, and by how much.



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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{

double crime, column, value;


  string    filename;
  ifstream  file;

  filename = "police.txt";

  file.open( filename.c_str() );

  if ( !file.good() )  // Reads to see if file will open
  {
     cout << "**ERROR: unable to open file!" << endl;
     exit(-1);   
  }


  file >> crime;  // total # of rows in the file
  i = 1;

  while (i <= crime)  // count through each year of data:
  {
		
    		for (int o = 1; o <= 7; o = o + 1)// COUNTING EACH column in the rows              
    		{
      		file >> column;  // Each column in every row
       		value = column; // INITIALIZING
       	    	
			
			
    		}

    i = i + 1;
  }

  system("PAUSE");
 return EXIT_SUCCESS;
}
Last edited on
Topic archived. No new replies allowed.