Only reading the first line of input

Hello. I am working on a project for my cs250 class and I cannot figure out where I am going wrong. The program is meant to read in a set number of URL's and count the hits. After counting all of the hits, it prints them out to a file. But When I try to run the program. It seems that it only does the first line. I must use an array of structs in order to do this. The struct include both the URL and the number of hits for the said URL. It also says that I may need to make my own addInOrder and binarySearch Methods. I am pretty sure that my binary search method works, but I have concern over my addInOrder method.


Here is my code:

counthits.cpp(main driver program)
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 <cstdlib>
#include <iostream>
#include <string>
#include <fstream>


#include "histogram.h"


using namespace std;


// Read log entries from the provided input stream, extracting the request
// portion from each. If the request is a GET, extract the locator (throwing
// away any part after a '?') and update a count of how many times that locator
// has appeared.
//
// Write the resulting wlocators and counts (in alphabetic order by locator)
//  in CSV format to the output stream.
//
// - Assume that the input contains a maximum of MaxPages distinct locators.
//   If more distinct locators than this are actually
//   encountered, write nothing to the output stream but print an error
//   message on the standard error stream.
void histogram(const int MaxPages, istream& input, ostream& output);



int main (int argc, char** argv)
{
  if (argc != 4)
    {
      cerr << "Usage: " << argv[0] << " MaxPages inFileName outFileName" << endl;
      return -1;
    }

  // Get MaxPages from the command line
  int MaxPages = atoi(argv[1]);

  // Take input and output file names from the command line
  ifstream in (argv[2]);
  ofstream out (argv[3]);
  histogram (MaxPages, in, out);
  in.close();
  out.close();

  return 0;
}



histogram.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "histogram.h"
//#include "arrayUtils.h"
#include <string>


using namespace std;




struct CountedLocations {
    string Location;
    int numHits;
};








// Extract the quoted request from a line containing a log entry
string extractTheRequest(string logEntry)
{
  string::size_type requestStart = logEntry.find('"');
  string::size_type requestEnd = logEntry.rfind('"');
  if (requestStart != requestEnd)
    {
      return logEntry.substr(requestStart+1, requestEnd-requestStart-1);
    }
  else
    return "";
}


template <typename T>
void addElement (T* array, int& size, int index, T value)
{
  // Make room for the insertion
  int toBeMoved = size - 1;
  while (toBeMoved >= index) {
    array[toBeMoved+1] = array[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  array[index] = value;
  ++size;
}




int binarySearch(const CountedLocations list[], int listLength, string searchItem)
{
    int first = 0;
    int last = listLength - 1;
    int mid;
    string midFind;


    bool found = false;


    while (first <= last && !found)
    {
        mid = (first + last) / 2;
        midFind = list[mid].Location;
        if (midFind == searchItem)
            found = true;
        else
            if (searchItem < midFind)
                last = mid - 1;
            else
                first = mid + 1;
    }


    if (found)
        return mid;
    else
        return -1;
}








// Check to see if this is a GET request
bool isAGet (string request)
{
  return request.size() > 3 && request.substr(0,4) == "GET ";
}




// Extract the locator part of a GET request
string extractLocator (string request)
{
  // strip off the GET
  string::size_type locatorStart = 3;


  // Skip any blanks
  while (request[locatorStart] == ' ')
    ++ locatorStart;


  // The locator ends at the first blank or ? after that.
  string::size_type locatorEnd = locatorStart;
  while (request[locatorEnd] != ' ' && request[locatorEnd] != '?')
    ++ locatorEnd;


  string locator = request.substr(locatorStart, locatorEnd-locatorStart);
  return locator;
}


//template <typename T>
int addInOrder (CountedLocations array[], int& size, string value)
{
  // Make room for the insertion
  int toBeMoved = size - 1;
  while (toBeMoved >= 0 && value < array[toBeMoved].Location) {
    array[toBeMoved+1] = array[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  array[toBeMoved+1].Location = value;
  ++size;
  return toBeMoved+1;
}






// Read log entries from the provided input stream, extracting the request
// portion from each. If the request is a GET, extract the locator (throwing
// away any part after a '?') and update a count of how many times that locator
// has appeared.
//
// Write the resulting wlocators and counts (in alphabetic order by locator)
//  in CSV format to the output stream.
//
// - Assume that the input contains a maximum of MaxPages distinct locators.
//   If more distinct locators than this are actually
//   encountered, write nothing to the output stream but print an error
//   message on the standard error stream.
void histogram(const int MaxPages, istream& input, ostream& output)
{
    // Step 1 - set up the data
    // Data should be stored in two parallel arrays, locators and counts.
    // locators[i] will be the i_th locator string and counts[i] will be the
    // count of how many times that particular locator has been requested.

    CountedLocations C [MaxPages];
    int nLocators = 0;


    // Step 2 - read and count the requested locators
    string logEntry;
    getline (input, logEntry);
    while (input)
    {
        
        string request = extractTheRequest(logEntry);
        if (isAGet(request))
        {
            string locator = extractLocator(request);
            int position = binarySearch (C, nLocators, locator);
            if (position >= 0)
            {
                // We found this locator already in the array.
                // Increment its count
                C[position].numHits+=1;
            }
            else
            {
                if (nLocators < MaxPages)
                {
                    // This is a new locator. Add it.
                    position = addInOrder (C, nLocators, locator);
                    // And add a count of one at the corresponding position
                    // in the counts array
                    //int nCounts = nLocators - 1;
                    C[position].numHits = 1;
                    //addElement (CountedLocations, nCounts, position, 1);
                }
                else
                {
                    // Not enough room in the arrays
                    ++nLocators;
                }
            }
        }
        getline (input, logEntry);
    }


    // Step 3 - write the output report
    string Location;
    int Hits;
    if (nLocators <= MaxPages)
    {
        for (int i = 0; i < nLocators; ++i)
        {
            Location = C[i].Location;
            Hits = C[i].numHits;
        }
        output << "\"" << Location << "\"," << Hits << endl;
    }
    else
    {
        cerr << "Input file contains more than " << MaxPages << " locators." << endl;
    }
  // Step 4 - cleanup
}



This is my input:
70.161.31.80 - - [30/Mar/2008:23:31:21 -0500] "GET /cocoon/~cs330web/forum/show/_axle/private.gif HTTP/1.1" 302 -
70.161.31.80 - - [30/Mar/2008:23:31:21 -0500] "GET /cocoon/~cs330web/forum/show/_axle/feed-icon16x16.png HTTP/1.1" 302 -
70.161.31.80 - - [30/Mar/2008:23:31:21 -0500] "GET /axle/Forum/_axle/private.gif HTTP/1.1" 304 -
70.161.31.80 - - [30/Mar/2008:23:31:21 -0500] "GET /axle/Forum/fckeditor/editor/fckeditor.html?InstanceName=text&Toolbar=ForumToolbar HTTP/1.1" 304 -
70.161.31.80 - - [30/Mar/2008:23:31:21 -0500] "GET /axle/Forum/fckeditor/fckconfig.js HTTP/1.1" 304 -
127.0.0.1 - - [30/Mar/2008:23:31:28 -0500] "GET / HTTP/1.0" 200 2499
127.0.0.1 - - [30/Mar/2008:23:31:29 -0500] "GET / HTTP/1.0" 200 2499
Last edited on
Topic archived. No new replies allowed.