Hey guys, I don't know where to get started in this. I pretty much only have the header file and I don't even know if that is correct. I don't know what the question is asking other than it is asking me to open up a text file. Any suggestions?

  1. Basic Concepts
    Definition 1. A tag in XML is what is written between angled brackets e.g. <name>.
    Definition 2. An XML element is a logical data structure (piece of data) within an XML file. It consists of:
    • Start (opening) tag (the beginning of an element), e.g. <name>;
    • End (closing) tag (the end of an element), e.g. </name>;
    • The information between the tags (the contents of the element).
    So, an XML element is an opening and a closing tag and what comes in between.
    Examples:
    <point>
    <x>10</x>
    <y> 20 </y>
    </point>
    or
    <point><x>10 </x> <y> 20</y></point>
    Definition 3. An XML tag name is a string including letters, digits, underscores, hyphens, and periods and may not include white spaces.
    Note: The XML data is correctly constructed (nested).
  2. Problem Statement
    3.1. Write a C++ class named Points with the following functionality:
    Points (char [] fileName); // Constructor
    size_t counter() const; // Returns the number of points
    void print () const; // Prints out a list of points in the format:
    // (x1, y1), (x2, y2), (x3, y3) . . . , (xn, yn)
    Hint:
    (1) Use a C++ vector to store the points.
    (2) The constructor should reads the XML text file (named it “input.txt”), extract the coordinates of all points and store them in a C++ vector.
    3.2. Write a C++ main function to test the Points class

Recommended Answers

All 2 Replies

So far, this is my header file.

#ifndef POINTS_H
#define POINTS_H

#include <fstream>
#include <iostream>
using namespace std;

class Points
{
public:
    Points (char [] filename); // Constructor
    size_t_counter() const; // Returns the number of points
    void print() const; // Prints out a list of points


private:

};

#endif

So, your c++ code needs to open and read the XML file, and then prints out the points in the specified format. To do this, you need to either write your own parser, or you need to use a standard XML parser that can read the file and put the data into the appropriate format. A popular C++ xml parsing library is xerces. I have written generic xml parsers numerous times in the past, and I still do that when I need the fastest possible processing speed and code simplicity, though mostly I reuse what I have written in the past.

So far, you have only the basic class definition for Points, but no functionality nor member variables. You need to add a Point class and a vector<Point> member variable in Points. Example:

class Point
{
private:
    int m_x;
    int m_y;
public:
    Point(int x, int y) : m_x(x), m_y(y) {}
    int getX() const { return m_x; }
    int getY() const { return m_y; }
};

class Points
{
private:
    vector<Point> m_points;

    Point readPoint( ifstream& strm ) const; // Reads a point from the xml file.
    void readPoints( ifstream& strm ); // Reads opened file and populates the vector m_points.
                                       // repeatedly calling readPoint(strm) until strm.eof() is reached.
public:
    Points( const char* filename ); // This is better than a non-const array
    size_t counter() const { return m_points.size(); }
    void print() const;
};

I will leave you the task of implementing the constructor (open file, call readPoints()), and the other functions - I implemented the counter() function for you... :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.