C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 24th, 2009, 10:40 PM
gdarian21682 gdarian21682 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 1 gdarian21682 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 24 sec
Reputation Power: 0
Syntax errors - Question on c++ code project

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" " "


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;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Syntax errors - Question on c++ code project


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
Stay green...Green IT