Uh... Could someone please help?

Could someone help me understand this? I'm really new to c++ and have no idea where to even start so here is the problem my teacher has given me.

"XML is a standard for representing documents and data stores in a textual format. XML is very web friendly and is becoming a defacto standard for transmitting data between programs using http or https. XML is also often used as an intermediate format for translating data from disparate systems. Note that many languages provide "toolkits" to manipulate xml and databases as a datatype unto itself. We will not be using "XMLReader" and "XMLWriter" toolkits, but rather produce brute force XML in a text file as output. Brute force XML will still be a valid XML file.

For example, there is little coding difference between

Outputfile << "The price is " << price << "dollars." << endl;

and

Outputfile << " <Price>" << price << " </Price>"<< endl;

Both produce text output in a file, but the later can be interpreted as XML, That is brute force XML.

Write a C++ program which uses filestreams to read data from two text sequential data files and writes a XML file combining data from both files as described below. but rather produce brute force XML in a text file as output. Since we are not doing any math with the input values, conversion of file input values to numeric data types will not be required.


The file ITEMS.DAT is a fixed field length text file with the following format:

Columns Description
1- 8 Part Number
6 Color code imbedded in part number
9-26 Item description
27-34 Price in format 9999.99
36-45 Available Date in format 12/31/2016

46 end of line marker


Color codes are 1=Red, 2=Blue, 3=White, 4=Black

You will need to convert the color code into a color name for the XML file.


The CUSTOMER.pip file is a pipe delimited text file containing

lastname|firstname|partnumber|TransactionDate


the TransactionDate is in the form MM/DD/YYYY, for example 10/21/2016


Read data from ITEMS.DAT and CUSTOMER.pip to create a file named Transactions.XML with the structures detailed below. Sample input files should produce the XML output below. Your program should react to any input files and produce an XML file appropriate to the input data. Note that leading or trailing spaces on the extracted input data are acceptable at this point in the course.


ITEMS.DAT
PC12K2RTUsed Car 02350.00m412/31/2016


CUSTOMER.pip
Bright|Rich|PC12K2RT|10/21/2011

Your program must generate the following Transactions.XML file.

<Transaction>
<Customer>
<Lastname>Bright</Lastname>
<Firstname>Rich</Firstname>
</Customer>
<TransactionDate>10/21/2016</TransactionDate>
<Items>
<Item>
<PartNumber>PC12K2RT<PartNumber>
<PartColor>Blue<PartColor>
<PartDescription>Used Car</PartDescription>
<PartPrice>2350.00</PartPrice>
</Item>
</Items>
</Transaction>


Read only a single data record from each input file to produce a XML file containing a single transaction data record.


Submit your Program2.cpp source file to the Programming Project #2C dropbox by the due date."


any and all help would be great and, thank you in advance
What help do you need?

We're not going to write your program for you. Make an attempt and post your code with any questions you have.
I would start with creating a struct Item and read it from the file items.dat.
Then I would create a struct Customer and read it from customer.zip.
Finally you write some code to output Item and Customer to XML and write it to transactions.xml
I don't mean write it for me. I can't learn anything that way. I don't understand what XML files are. do I have to make an XML file and if so how do I do that.
closed account (48T7M4Gy)
https://www.w3schools.com/xml/

The job is pretty much what your brief says. Read some date from the file(s) which are in a set format, convert the data read in to xml tagged lines and save those to another file.

Your first job is just getting the program to read in and coordinate the two files.
Once you've got control over those strings you add tags <> etc to the front and back of the strings, then save them to the output file.

The data is structured so, as said above, struct's are ideal.
xml files are just text files with tags, like HTML.

the tags look like this:

<tag> data of any sort </tag>

where /tag means close the tag, without opens it.

think of them as {} in C++ ... begin/end markers of a sort. They can be nested more or less indefinitely.

you do need to write an xml file, this appears to be the desired output of your code project. But don't be afraid of it, its just a formatted text file with a goofy name.

notepad++ is one of many tools that can check simple xml files and help you read them and validate your work. You may need to install an extension but its all free and works pretty well.

Need help on this as well. I have no idea where to start. If you figure it out PLEASE let me know!
Last edited on
closed account (48T7M4Gy)
Start by opening the two files and reading each line by line, and then printing out each line.

Use the tutorials here if you don't know how to do that. The sample data is given to you above. Use notepad to make the files. Maybe write a few extra sample lines following the pattern of the samples in each case.

Write the code for that and show us then we can discuss the next step. Up to you.

http://www.cplusplus.com/doc/tutorial/files/
here is what I have so far. Not sure if i need a log file or anything but I really do not understand any of this so this is my attempt of getting the code ready then moving on to the files


// Program2.cpp : Defines the entry point for the console application.
//4/6/17


#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
//declare file variables
ifstream InfileI; //for item
ifstream InfileC; //for customer
ofstream Outfile; //XML
ofstream Logfile; //Log

//declare constants and variables
string item;
string customer;

//open files
Logfile.open("d:\\Program2.log");
if (Logfile)
{
cout << "Log file created.\n\n";
}

else
{
cout << "FAIED to open Program2.log\n\n";
}

////////////////////////////////////////////////////////////////////

InfileI.open("d:\\ITEMS.dat");
if (InfileI)
{
Logfile << "ITEMS.dat opend sucessfully,\n\n";
}

else
{
Logfile << "FAILED to open ITEMS.dat,\n\n";
}

//////////////////////////////////////////////////////////////////////

InfileC.open("d:\\CUSTOMER.pip");
if (InfileC)
{
Logfile << "CUSTOMER.pip opend sucessfully,\n\n";
}

else
{
Logfile << "FAILED to open CUSTOMER.pip,\n\n";
}

////////////////////////////////////////////////////////////////////

Outfile.open("d:\\Transactions.XML");
if (Outfile)
{
Logfile << "Transactions.XML opend sucessfully.\n\n";
}
else
{
Logfile << "FAILED to open Transactions.XML\n\n";
}


//Get input from files
InfileI >> item;

InfileC >> customer;


//output to screen
cout << item << ", " << customer;
Outfile << item << ", " << customer;


//close files and pause screen before exiting
InfileI.close();
InfileC.close();
Logfile.close();
Outfile.close();
cin.get();

//freeze screen
system("PAUSE");

return (0);
}
closed account (48T7M4Gy)
Your program appears to be working.

If you don't understand what's going on then that indicates to me you need to spend some study time on the question you are given and having a look in the tutorials here, your class notes etc.

By now you should have a plan. If you haven't then the best thing to do is write one with 5 or 6 steps so you know where you are going. You have enough information to do it. Don't write more code until the plan is done.

Keep up the good work.
closed account (48T7M4Gy)
Here's a bit more of a start. It is by no means complete and you will have to find a way to break up the lines in the data files into meaningful parts and cross-referencing items vs customers. 'delimiters' and sub-strings are useful things to find out about in breaking down the lines. The assignment brief gives relevant details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <fstream>

int main(){
    
    // SETUP FILES TO READ FROM
    std::ifstream items_file;
    items_file.open("ITEMS.dat");
    
    if (items_file.is_open()){
        std::cout << "ITEMS.dat successfully opened\n";
    }
    else{
        std::cout << "FAILED to open items_file.dat\nProgram closing\n";
        return -99;
    }
    
    std::ifstream customers;
    customers.open("CUSTOMERS.pip");
    if (customers.is_open()){
        std::cout << "CUSTOMERS.pip successfully opened\n";
    }
    else{
        std::cout << "FAILED to open CUSTOMERS.pip\nProgram closing\n";
        return -99;
    }
    
    // READ FROM FILE & PROCESS
    // FILE TO WRITE TO
    std::ofstream XML_write;
    XML_write.open("Transactions.XML");
    
    std::string item;
    while( items_file >> item ){
        XML_write << "<ITEM>" << item << "</ITEM>\n";
    }
    XML_write.close();
    
    //CLEANUP
    items_file.close();
    customers.close();
    XML_write.close();
    
    return (0);
}


Transactions.xml
<ITEM>PC12K2RTUsed</ITEM>
<ITEM>Car</ITEM>
<ITEM>02350.00m412/31/2016</ITEM>
<ITEM>RS27M7LTWindow</ITEM>
<ITEM>02410.00m512/23/2017</ITEM>


ITEMS.dat
PC12K2RTUsed Car 02350.00m412/31/2016
RS27M7LTWindow   02410.00m512/23/2017


CUSTOMERS.pip (not really used here)
Bright|Rich|PC12K2RT|10/21/2011
Smith|Mary|RS27M7LT|03/04/2012

thank you all for helping me! after a few hours of work (and some help from a friend) here is what I came up with
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Program2.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

void Pause() {
    string junk;  
    cout << "Press any to continue ... ";
    cin.ignore();
    getline(cin, junk);
}

int main()
{

    //declare file variables
    ifstream ItemsFile, CustomerFile;
    ofstream logFile;

    //declare getline variables
    string getlineForItems = "no line was stored", getlineForCustomers = "no line was stored";

    //declare variables for the Customer.dat file
    string firstName = "no first name", LastName = "no last name",
        date = "no date", Color = "no color", colorCode="0";


    //declare variables for the Items.cvs file
    string partNumber = "no part number";
    string details = "no details", Price = "$0";


    //open files for Logging, itemsFile, CustomerFile:
    logFile.open("d://Program2.log");
    if (logFile)
    { cout << "Logging Started..." << endl;
    }
    else {
     cout << "LogFile open FAILED" << endl;
     Pause();
    }
    ItemsFile.open("d://ITEMS.DAT");
    if (ItemsFile)
    { logFile << "Items file OPENED successfully" << endl;
    }
    else {
     logFile << "Items file open FAILED" << endl;
    }
    CustomerFile.open("d://CUSTOMER.pip");
    if (CustomerFile)
    { logFile << "Customers file OPENED successfully" << endl;
    }
    else {
     logFile << "Customers file open FAILED" << endl;
    }

        //set the values for the getline variabels
        getline(ItemsFile, getlineForItems);
        getline(CustomerFile, getlineForCustomers);

		//chop up the strings saved
		partNumber = getlineForItems.substr(0, 8);
		details = getlineForItems.substr(8, 8);
		Price = getlineForItems.substr(27, 7);

		LastName = getlineForCustomers.substr(0,6);
		firstName = getlineForCustomers.substr(7,4);
		colorCode = partNumber.substr(5,1);
		date = getlineForCustomers.substr(21,10);

        //cascading if for checking colorCode so you can store partColor
        if(colorCode == "1")
        {
           Color = "Red";
        }
        else if (colorCode == "2")
        {
            Color = "Blue";
        }
        else if (colorCode == "3")
        {
            Color = "White";
        }
        else if (colorCode == "4")
        {
            Color = "Black";
        }
        else
        {
            Color = "Invalid color code in part number.";
        }

        /*

         //* *****     DEBUG CODE
        logFile << "Line stored for Items=" << getlineForItems << endl;
        logFile << "Line stored for Customer=" << getlineForCustomers << endl;
        logFile << "Last name=" << LastName << endl;
		logFile << "First name=" << firstName << endl;
        logFile << "Date=" << date << endl;
        logFile << "Partnumber=" << partNumber << endl;
        logFile << "Part Color=" << Color << endl;
        logFile << "Color code=" << colorCode << endl;
        logFile << "Details=" << details << endl;
        logFile << "Price=" << Price << endl;
        //***** */
   
        //Add the XML to the logfile
        logFile << "<Transaction>"<< endl
            << " <Customer>" << endl
            << "  <LastName>" << LastName << "</LastName>" << endl
            << "  <firstName>" << firstName << "</firstName>"<< endl
            << " </Customer>" << endl
            << " <TransactionDate>" << date << "</TransactionDate>" << endl
            << " <Items>" << endl
            << "  <Item>" << endl
            << "   <PartNumber>" << partNumber << "</PartNumber>" << endl
            << "   <Color>" << Color << "</Color>" << endl
            << "   <PartDescription>" << details << "</PartDescription>" << endl
            << "   <PartPrice>" << Price << "</PartPrice>" << endl
            << "  </Item>" << endl
            << " </Items>" << endl
            << "</Transaction>" << endl << endl;

        //Display the info to the console
        cout << "Details that were wrote to the Transction.XML file: "<< endl << endl
            << "<Transaction>"<< endl
            << " <Customer>" << endl
            << "  <Lastname>" << LastName << "</Lastname>" << endl
            << "  <Firstname>" << firstName << "</Firstname>"<< endl
            << " </Customer>" << endl
            << " <TransactionDate>" << date << "</TransactionDate>" << endl
            << " <Items>" << endl
            << "  <Item>" << endl
            << "   <PartNumber>" << partNumber << "</PartNumber>" << endl
            << "   <PartColor>" << Color << "</PartColor>" << endl
            << "   <PartDescription>" << details << "</PartDescription>" << endl
            << "   <PartPrice>" << Price << "</PartPrice>" << endl
            << "  </Item>" << endl
            << " </Items>" << endl
            << "</Transaction>" << endl << endl;

    //close files
    ItemsFile.close();
    CustomerFile.close();
    logFile.close();

    //freeze screen.
    Pause();
    return 0;
}
Last edited on
Okay, I am also in the class. And I have most of the code done, but I am running into a "Unhandled exception at at 0x776FA882 in ConsoleApplication25.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0093EDF4." Could someone help me figure out just where my problem is, and how to go about solving it? The project is due tonight, and I would have posted sooner but have been solving little problems here and there. Thanks so much!

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

void Pause()
{ char junk;
cout << "Press Enter to continue ... ";
cin.ignore();
cin.get(junk);

}

int main()
{

// Declare File Variables and Constants

ifstream InFile;
string inputLine, partNumber, colorCode, itemDescrp, price, availDay, availMonth, availYear, colorString;
int color;
double custBalance;

// Open file 1 for use

InFile.open("C:\\Users\\evanc\\Desktop\\ITEMS.dat");
getline(InFile, inputLine);

//Use substr to extract the parts needed as strings

partNumber = inputLine.substr(0,8);
colorCode = inputLine.substr(5,1);
itemDescrp = inputLine.substr(8,18);
price = inputLine.substr(27,7);
availMonth = inputLine.substr(36,2);
availDay = inputLine.substr(38,2);
availYear = inputLine.substr(40,4);//

//Convert colorCode string into color int

color = stoi(colorCode);

if (color == 1)
colorString = "Red";
if (color == 2)
colorString = "Blue";
if (color == 3)
colorString = "White";
if (color == 4)
colorString = "Black";

cout << partNumber << endl;
cout << colorCode << endl;
cout << itemDescrp << endl;
cout << price << endl;
cout << availMonth << "\\" << availDay << "\\" << availYear << endl;
cout << colorString << endl;

// Open File 2 for use

ifstream InFile2;
string inputLine2, customerInfo;
string lastName, firstName, transactionMonth, transactionDay, transactionYear, customerPartNumber;
int nextPipePosition;

InFile2.open("C:\\Users\\evanc\\CUSTOMER.pip");
getline(InFile2, inputLine2);

// File 2 Display Test

cout << endl << endl << inputLine2 << endl;

// find delimiters and extract info

//nextPipePosition = 0;
nextPipePosition = customerInfo.find("|");
lastName = customerInfo.substr(0,nextPipePosition + 0);
customerInfo=customerInfo.substr(nextPipePosition+0,customerInfo.length()-nextPipePosition+0);

// nextPipePosition = customerInfo.find("|");
// firstName = customerInfo.substr(0,nextPipePosition + 0);
// customerInfo = customerInfo.substr(nextPipePosition+0,customerInfo.length()-nextPipePosition+0);

// nextPipePosition = customerInfo.find("|");
// customerPartNumber = customerInfo.substr(0,nextPipePosition + 0);
// customerInfo = customerInfo.substr(nextPipePosition+0,customerInfo.length()-nextPipePosition+0);

// transactionMonth = customerInfo.substr(0,2);
// transactionDay = customerInfo.substr(3,2);
// transactionYear = customerInfo.substr(5,2);

//Display results for testing

// cout << endl << lastName << endl;
// cout << firstName << endl;
// cout << customerPartNumber << endl;
// cout << transactionMonth << "//" << transactionDay << "//" << transactionYear << endl;

// freeze screen

Pause();
return (0);
}
Last edited on
Okay, I've determined that my problem lie in this line of code, customerInfo=customerInfo.substr(nextPipePosition+0,customerInfo.length()-nextPipePosition+0);

any ideas or suggestions?
closed account (48T7M4Gy)
I suggest you write a short test program with a hard coded source string, devoted to testing just that expression. Print out each variable value for the substring locations etc, comparing what you expect with what you get.

A small observation, why are you adding zero in the expression?
I did just that, and I am able to get the first name with the following,

nextPipePosition = customerInfo.find("|");
lastName = customerInfo.substr(0,nextPipePosition + 0);

but I when I add,

nextPipePosition = customerInfo.find("|");
firstName = customerInfo.substr(0,nextPipePosition + 0);
customer =customerInfo.substr(nextPipePosition+0,customerInfo.length()- nextPipePosition+0);

I keep getting the first name. I have finished the rest of the code. This is just the last part holding me up.
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/string/basic_string/find/

If you keep getting the first name then you need to increment the detected position. You're still adding zero. Why?
because with all the problems i've run into i've developed the, if it ain't broke don't fix it, philosophy. Also, thanks so much for the reference!
Topic archived. No new replies allowed.