question on c++ code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    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_Entr y(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_Entr y> 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(cons t vector<Log_Entr y> &);

    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(cons t vector<Log_Entr y> &);

    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(ve ctor<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" " "

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <sstream>
    #include "log_view.hpp"
    
    #ifdef CS2_STRING
      #include "string.hpp"
      using cs33001::string;
    #else
      #include <string>
      using std::string;
    #endif
    
    
    using namespace std;
    
    
    vector<Log_Entry> parse(const string&)
    {
    
      string line;
      vector<Log_Entry> _line;
      ifstream myfile ("name.txt");
      if(!myfile)
      {
            cout << "Couldn't open file";
      }
    
      if (myfile.is_open())
      {
        while (! myfile.eof())
        {
    
          getline (myfile,line);
    
          Log_Entry _name(line);
          _line.push_back(_name);
    
        }
            myfile.close();
    
        }
        return _line;
    
     }
    
    
    vector<string> split(const string& entries, char x)
    {
    
      string entry;
      vector<string> _entry;
      int index;
      for(int i = 0, j = 0; i < entries.size(); i++)
      {
    
            index = entries.find(x,i);
    
            if (index == entries.size())
            {
                break;
            while (j != index)
            {
               entry += entries[j];
               j++;
            }
    
            i = i + j;
            _entry.push_back(entry);
    
            }
    
    //      if(j != 9)
    //      _entry[9] = 0;
    
    }
    
              return _entry;
      }
    
    
    int byte_count(const vector<Log_Entry> &_name)
    {
    
       int bytes = 0;
       for (int i = 0; i < _name.size(); i++)
       {
           bytes += _name[i].get_number_of_bytes();
       }
    
    return bytes;
    
    }
    
    void output_hosts(vector<Log_Entry> _name)
    {
    
        for(int i = 0; i < _name.size(); i++)
        {
            string a = _name[i].get_host();
            cout << "hostd: " << a[0] << endl;
        }
    }
    
    Log_Entry::create_Log_Entry(const string&)
    {
    
        vector<string> entries = split(entry, ' ');
        _host = entries[0];
    
        vector<string> dates = split(entries[3], '/');
        _date.day = dates[0];
    
        _date.month = dates[1];
    
        vector<string> times = split(dates[2], ':');
    
        istringstream my(times[0].c_str());
        int time = 0;
        my >> time;
        _date.year = time;
    
        istringstream my2(times[1].c_str());
        my2 >> time;
        _time.hour = time;
    
        istringstream my3(times[2].c_str());
        my3 >> time;
        _time.minute = time;
    
        istringstream my4(times[3].c_str());
        my4 >> time;
        _time.second = time;
    
        _request = entries[5];
        _status = entries[8];
    
    
        istringstream my5(entries[9].c_str());
        my5 >> time;
        _number_of_bytes = time;
    
    
    }
    
    
    
    
    void output_all(const vector<Log_Entry> &entries)
    {
     for(int i = 0; i < entries.size(); i++)
     {
      cout << "***********************************************" << endl;
      cout << "Date Day:" << entries[i].get_date().day << endl;
      cout << "Month   :" << entries[i].get_date().month << endl;
      cout << "Year    :" << entries[i].get_date().year << endl;
      cout << "Host    :" << entries[i].get_host() << endl;
      cout << "------------------------------------------------" << endl;
      cout << "Time Hours:" << entries[i].get_time().hour << endl;
      cout << "Time Min  :" << entries[i].get_time().minute << endl;
      cout << "Time Sec  :" << entries[i].get_time().second << endl;
      cout << "Request   :" << entries[i].get_request() << endl;
      cout << "Status    :" << entries[i].get_status() << endl;
      cout << "Bytes     :" << entries[i].get_number_of_bytes() << endl;
      cout << "***********************************************" << endl;
    }
    return;
    }
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    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_Entr y(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.

    Comment

    • gdarian216
      New Member
      • Oct 2006
      • 57

      #3
      these are the error messages I got.

      Code:
      log_view.cpp: In function âstd::vector<Log_Entry, std::allocator<Log_Entry> > parse(std::string)â:
      log_view.cpp:38: error: no matching function for call to âLog_Entry::Log_Entry(std::string&)â
      log_view.hpp:41: note: candidates are: Log_Entry::Log_Entry()
      log_view.hpp:40: note:                 Log_Entry::Log_Entry(const Log_Entry&)
      log_view.cpp: In function âint byte_count(const std::vector<Log_Entry, std::allocator<Log_Entry> >&)â:
      log_view.cpp:90: error: âconst struct Log_Entryâ has no member named âget_number_of_bytesâ
      log_view.cpp: In function âvoid output_hosts(std::vector<Log_Entry, std::allocator<Log_Entry> >)â:
      log_view.cpp:102: error: âstruct Log_Entryâ has no member named âget_hostâ
      log_view.cpp: At global scope:
      log_view.cpp:107: error: ISO C++ forbids declaration of âcreate_Log_Entryâ with no type
      log_view.cpp:107: error: no âint Log_Entry::create_Log_Entry(const std::string&)â member function declared in class âLog_Entryâ
      log_view.cpp: In member function âint Log_Entry::create_Log_Entry(const std::string&)â:
      log_view.cpp:110: error: âentryâ was not declared in this scope
      log_view.cpp: In function âvoid output_all(const std::vector<Log_Entry, std::allocator<Log_Entry> >&)â:
      log_view.cpp:156: error: âconst struct Log_Entryâ has no member named âget_dateâ
      log_view.cpp:157: error: âconst struct Log_Entryâ has no member named âget_dateâ
      log_view.cpp:158: error: âconst struct Log_Entryâ has no member named âget_dateâ
      log_view.cpp:159: error: âconst struct Log_Entryâ has no member named âget_hostâ
      log_view.cpp:161: error: âconst struct Log_Entryâ has no member named âget_timeâ
      log_view.cpp:162: error: âconst struct Log_Entryâ has no member named âget_timeâ
      log_view.cpp:163: error: âconst struct Log_Entryâ has no member named âget_timeâ
      log_view.cpp:164: error: âconst struct Log_Entryâ has no member named âget_requestâ
      log_view.cpp:165: error: âconst struct Log_Entryâ has no member named âget_statusâ
      log_view.cpp:166: error: âconst struct Log_Entryâ has no member named âget_number_of_bytesâ

      Comment

      Working...