473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

question on c++ code

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. }
Jun 25 '09 #1
2 2606
myusernotyours
188 100+
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.
Jun 25 '09 #2
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â
Jun 25 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
9
by: Howard | last post by:
I am currently looking at the various packages that are available for software protection. I have a particular question that I'd welcome your feedback on. Background info: I work for a company...
11
by: NC Tim | last post by:
Hello, I think the question i have is fairly straightforward, but I can't seem to replicate the old SAS frequency procedure when I try to accomplish this in MS Access. anyway, i have about 10...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
13
by: Eric_Dexter | last post by:
All I am after realy is to change this reline = re.line.split('instr', '/d$') into something that grabs any line with instr in it take all the numbers and then grab any comment that may or may...
37
by: Prafulla T | last post by:
Assume that an integer pointer x is declared in a "C" program as int *x; Further assume that the location of the pointer is 1000 and it points to an address 2000 where value 500 is stored in 4...
12
by: kostas | last post by:
Hi I was asked to propose an interview question for C/C++ programmers (CAD/CAE software) I came up with the following...
2
by: robtyketto | last post by:
Greetings, Within my jsp I have HTML code (see below) which accepts input, one of these fields sequence unlike the others is an Integer. <FORM ACTION="wk465682AddFAQ.jsp" METHOD="POST"> Id:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.