Connecting Tech Pros Worldwide Help | Site Map

question on c++ code

Member
 
Join Date: Oct 2006
Posts: 57
#1: Jun 25 '09
I am writting a program that has three different files and is compiled by a Makefile. The goal is to take a file of text and split it up in different sections and stored in vectors. Then it outputs the information in a standard format. The main is fine and my function declarations are good also I am just having trouble writing the functions. Below are the function names and what they need to do. I have been trying some different ways to write the code and I keep coming up with errors I am new at c++. I will also attach the code I have so far if any can help point me in the right direction.

Log_Entry create_Log_Entry(const string&);

This function does the following:
The parameter is a log entry line.
Create a Log_Entry object.
Break the line into fields and subfields using split(). Several splits will be necessary.
Fill the data members with values.
Split on ' ' (space character) first, if the split does not give 10 strings set the number of bytes to 0 and return. (Some lines in the file may be bad.)
For string to int conversion use string streams.

vector<Log_Entry> parse(string);

This function does the following:
Open a log file specified by the parameter name (File I/O).
Read lines from the opened file.
Create a Log_Entry object passing the line just input.
Push each Log_Entry object onto a vector.
Return the vector of Log_Entrys.


void output_all(const vector<Log_Entry> &);

This function does the following:
Loop through the vector provided as the argument and output all information for each Log_entry object.
Format the output neatly in some manner, example output.


int byte_count(const vector<Log_Entry> &);

This function does the following:
Loop through the vector provided as the argument and sum the number_of_bytes members. The sum is returned.


void output_hosts(vector<Log_Entry>);

This function does the following:
Loop through the vector provided as the argument and output the host member of each Log_Entry object in the vector one per line.


vector<string> split(const string&, char);

This function does the following:
Takes a character parameter, uses the character as a separator in the string, and returns a vector of strings.
Ex. If the split function is given "abc:12345:xx" and ':' it will return a vector of 3 strings: "abc", "12345", and "xx".
One approach, a loop goes through the string, if the current character is not the splitter accumulate it into a string using string += char, if the current character is the splitter then push_back the string that holds the accumulated characters and clear the accumulator string variable.
There is a test function for split that can be run from the makefile. To run, for example:

./split abcx24567xqqq x
./split "abc 24567 qqq" " "

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <sstream>
  6. #include "log_view.hpp"
  7.  
  8. #ifdef CS2_STRING
  9.   #include "string.hpp"
  10.   using cs33001::string;
  11. #else
  12.   #include <string>
  13.   using std::string;
  14. #endif
  15.  
  16.  
  17. using namespace std;
  18.  
  19.  
  20. vector<Log_Entry> parse(const string&)
  21. {
  22.  
  23.   string line;
  24.   vector<Log_Entry> _line;
  25.   ifstream myfile ("name.txt");
  26.   if(!myfile)
  27.   {
  28.         cout << "Couldn't open file";
  29.   }
  30.  
  31.   if (myfile.is_open())
  32.   {
  33.     while (! myfile.eof())
  34.     {
  35.  
  36.       getline (myfile,line);
  37.  
  38.       Log_Entry _name(line);
  39.       _line.push_back(_name);
  40.  
  41.     }
  42.         myfile.close();
  43.  
  44.     }
  45.     return _line;
  46.  
  47.  }
  48.  
  49.  
  50. vector<string> split(const string& entries, char x)
  51. {
  52.  
  53.   string entry;
  54.   vector<string> _entry;
  55.   int index;
  56.   for(int i = 0, j = 0; i < entries.size(); i++)
  57.   {
  58.  
  59.         index = entries.find(x,i);
  60.  
  61.         if (index == entries.size())
  62.         {
  63.             break;
  64.         while (j != index)
  65.         {
  66.            entry += entries[j];
  67.            j++;
  68.         }
  69.  
  70.         i = i + j;
  71.         _entry.push_back(entry);
  72.  
  73.         }
  74.  
  75. //      if(j != 9)
  76. //      _entry[9] = 0;
  77.  
  78. }
  79.  
  80.           return _entry;
  81.   }
  82.  
  83.  
  84. int byte_count(const vector<Log_Entry> &_name)
  85. {
  86.  
  87.    int bytes = 0;
  88.    for (int i = 0; i < _name.size(); i++)
  89.    {
  90.        bytes += _name[i].get_number_of_bytes();
  91.    }
  92.  
  93. return bytes;
  94.  
  95. }
  96.  
  97. void output_hosts(vector<Log_Entry> _name)
  98. {
  99.  
  100.     for(int i = 0; i < _name.size(); i++)
  101.     {
  102.         string a = _name[i].get_host();
  103.         cout << "hostd: " << a[0] << endl;
  104.     }
  105. }
  106.  
  107. Log_Entry::create_Log_Entry(const string&)
  108. {
  109.  
  110.     vector<string> entries = split(entry, ' ');
  111.     _host = entries[0];
  112.  
  113.     vector<string> dates = split(entries[3], '/');
  114.     _date.day = dates[0];
  115.  
  116.     _date.month = dates[1];
  117.  
  118.     vector<string> times = split(dates[2], ':');
  119.  
  120.     istringstream my(times[0].c_str());
  121.     int time = 0;
  122.     my >> time;
  123.     _date.year = time;
  124.  
  125.     istringstream my2(times[1].c_str());
  126.     my2 >> time;
  127.     _time.hour = time;
  128.  
  129.     istringstream my3(times[2].c_str());
  130.     my3 >> time;
  131.     _time.minute = time;
  132.  
  133.     istringstream my4(times[3].c_str());
  134.     my4 >> time;
  135.     _time.second = time;
  136.  
  137.     _request = entries[5];
  138.     _status = entries[8];
  139.  
  140.  
  141.     istringstream my5(entries[9].c_str());
  142.     my5 >> time;
  143.     _number_of_bytes = time;
  144.  
  145.  
  146. }
  147.  
  148.  
  149.  
  150.  
  151. void output_all(const vector<Log_Entry> &entries)
  152. {
  153.  for(int i = 0; i < entries.size(); i++)
  154.  {
  155.   cout << "***********************************************" << endl;
  156.   cout << "Date Day:" << entries[i].get_date().day << endl;
  157.   cout << "Month   :" << entries[i].get_date().month << endl;
  158.   cout << "Year    :" << entries[i].get_date().year << endl;
  159.   cout << "Host    :" << entries[i].get_host() << endl;
  160.   cout << "------------------------------------------------" << endl;
  161.   cout << "Time Hours:" << entries[i].get_time().hour << endl;
  162.   cout << "Time Min  :" << entries[i].get_time().minute << endl;
  163.   cout << "Time Sec  :" << entries[i].get_time().second << endl;
  164.   cout << "Request   :" << entries[i].get_request() << endl;
  165.   cout << "Status    :" << entries[i].get_status() << endl;
  166.   cout << "Bytes     :" << entries[i].get_number_of_bytes() << endl;
  167.   cout << "***********************************************" << endl;
  168. }
  169. return;
  170. }
myusernotyours's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 168
#2: Jun 25 '09

re: question on c++ code


What errors exactly are you having? Saying you are having problems with some functions is not quite enough...

You may also probably want to make most of those functions members of a Log_Entry class.

E.g the create_Log_Entry(const string&) function would be the constructor and so on. You would do the parsing and all inside the Log_Entry class itself.

Regards,

Alex.
Member
 
Join Date: Oct 2006
Posts: 57
#3: Jun 25 '09

re: question on c++ code


these are the error messages I got.

Expand|Select|Wrap|Line Numbers
  1. log_view.cpp: In function âstd::vector<Log_Entry, std::allocator<Log_Entry> > parse(std::string)â:
  2. log_view.cpp:38: error: no matching function for call to âLog_Entry::Log_Entry(std::string&)â
  3. log_view.hpp:41: note: candidates are: Log_Entry::Log_Entry()
  4. log_view.hpp:40: note:                 Log_Entry::Log_Entry(const Log_Entry&)
  5. log_view.cpp: In function âint byte_count(const std::vector<Log_Entry, std::allocator<Log_Entry> >&)â:
  6. log_view.cpp:90: error: âconst struct Log_Entryâ has no member named âget_number_of_bytesâ
  7. log_view.cpp: In function âvoid output_hosts(std::vector<Log_Entry, std::allocator<Log_Entry> >)â:
  8. log_view.cpp:102: error: âstruct Log_Entryâ has no member named âget_hostâ
  9. log_view.cpp: At global scope:
  10. log_view.cpp:107: error: ISO C++ forbids declaration of âcreate_Log_Entryâ with no type
  11. log_view.cpp:107: error: no âint Log_Entry::create_Log_Entry(const std::string&)â member function declared in class âLog_Entryâ
  12. log_view.cpp: In member function âint Log_Entry::create_Log_Entry(const std::string&)â:
  13. log_view.cpp:110: error: âentryâ was not declared in this scope
  14. log_view.cpp: In function âvoid output_all(const std::vector<Log_Entry, std::allocator<Log_Entry> >&)â:
  15. log_view.cpp:156: error: âconst struct Log_Entryâ has no member named âget_dateâ
  16. log_view.cpp:157: error: âconst struct Log_Entryâ has no member named âget_dateâ
  17. log_view.cpp:158: error: âconst struct Log_Entryâ has no member named âget_dateâ
  18. log_view.cpp:159: error: âconst struct Log_Entryâ has no member named âget_hostâ
  19. log_view.cpp:161: error: âconst struct Log_Entryâ has no member named âget_timeâ
  20. log_view.cpp:162: error: âconst struct Log_Entryâ has no member named âget_timeâ
  21. log_view.cpp:163: error: âconst struct Log_Entryâ has no member named âget_timeâ
  22. log_view.cpp:164: error: âconst struct Log_Entryâ has no member named âget_requestâ
  23. log_view.cpp:165: error: âconst struct Log_Entryâ has no member named âget_statusâ
  24. log_view.cpp:166: error: âconst struct Log_Entryâ has no member named âget_number_of_bytesâ
Reply