HELP!

I am a beginner and need help getting the data from the file 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
What exactly are you having issues with?

Also you dont need to declare filename if youre hardcoding it into your program.
1
2
  ifstream  file;
  file.open("police.txt");


you really only need file.open( filename.c_str() );
when youre asking the user for a filename.

Last edited on
I guess im not sure how to read the file. How would i read the rows to calculate the number of crimes or calculate the other information. thank you for the help in advance, im struggling with this subject. The reason i declared the filename is because the professor likes us to do that as a baby step. I see what you mean tho.
Last edited on
sky3 wrote:
Also you dont need to declare filename if youre hardcoding it into your program.


Actually the way he is doing it is much better then having a "magic number" variable as you suggested. It is good practice to have things like the filename stored in a variables instead of just a string literal.

The reason for this is because lets say you use that filename 5 other times in the application you would need to write it out every time if it was a string literal. Which might not seem to bad but what about 10 times or 20 or 100? This opens up a large opportunity for bugs to happen.

So to clarify
1
2
3
filename = "police.txt";

file.open( filename.c_str() );


Is better then
1
2
ifstream  file;
file.open("police.txt");


I guess im not sure how to read the file. How would i read the rows to calculate the number of crimes or calculate the other information.


To read rows of data you can use the std::getline() function which will return a line from the file.

For example this will read line by line from a file and print it to the console

1
2
3
4
5
6
7
std::ifstream file(filename);
if (file.is_open())
{
    string line;
    while (std::getline(file, line))
        std::cout << line << std::endl;
}

http://www.cplusplus.com/reference/string/string/getline/

So std::getline() will grab line by line from a file then you will need to parse the information you need from each line and do your calculations.

Here is a quick example that will read your file format into a vector of strings which you can then use to parse the data and do your calculations.

Just a note in this example I don't use the first line to denote the number of entries that follow.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::vector<std::string> lines;

const std::string filename = "police.txt";
std::ifstream file(filename);

if (file.is_open())
{
    std::string line;
    while (std::getline(file, line))
    {
        lines.push_back(line);
    }
}

// At this point the vector lines contains each line in the text file. 


Hope this helps give you a starting block to work off of.
is there a way to read the file without using arrays and vectors? We have not learned how to code using arrays. I pretty much need help with the simplest way of completing this assignment. I can post the whole assignment if that will help give me pointers.

is there a way to do this by using a void statement and then calling it in int main()? I do not know how to proceed from here...

void police(int&year, int & crimes, int&arrests, int& thefts, int&assts, int&robberies, int&homicides)
{

file>>year;

}

thanks!
Last edited on
The assignment is to write a C++ program to analyze a small file of Chicago crime data. The
course web page contains an example input file “police.txt” for you to test against (download by
right-clicking link and selecting “Save As”, and save on your local machine). The format of this file is as follows:


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 the number of rows of crime data in the file; in the example
above, the value for N is 6, 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 (e.g. 2004, 2005, 2006, …, 2009). Your assignment is
to write a C++ program that inputs Chicago crime data
from a file named “police.txt”, 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


Notice your program should end by outputting 3 averages: (1) average # of crimes per year, (2) average # of
arrests per year, and (3) average # of homicides per year. Define and use functions in your solution
Topic archived. No new replies allowed.