Solved

How do I write a C++ program (coursework)

Posted on 2011-09-28
20
98 Views
Last Modified: 2015-06-23
How do I write a C++ program to compare two delimited text files, and apply the new entries and the changes to the main file

I have to write this program in C++ or VB which requires the following:

1.There are two delimited text files, both have the same format, one is the master file, the other is weekly file
2. Sample will be:
ID|firstname|middleInitial|lastName|Address1|Address2|City|State|Zip|Zip4|SSN#
2345|Andy|L|FEDELI|123 Street|Apt3|Springtown|FL|35107|1234|393381124

3.So, both files are in the same format
4.The program will check each of the lines by the ID, if it finds a match, it has to update the whole line to the master text file
5. If there is no ID match, add the whole line to the master file, which means we have a new customer added to the master file
0
Comment
Question by:soniaN310
  • 4
  • 3
  • 3
  • +3
20 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 36814358
You would do well to import this data into a database and do the updates with SQL.

This question is currently in the C++ Builder zone.  You said that you could also use VB.  What program development tools are at your disposal?
0
 

Author Comment

by:soniaN310
ID: 36818029
Microsoft Visual Studio 2005
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 36818487
Is this a homework assignment?
0
 
LVL 32

Expert Comment

by:sarabande
ID: 36889938
soniaN310, do you have any programming experience with either c++ or vb?

if yes, the task should be done with the language you like better.

in c++ you would create a structure (struct or class) which has all attributes as members (string or int members). then read master file record by record, parse each record and add it to a map using the id for key. then you could read the weekly file same way and merge the records from that into the map, which finally could be written back to a file what gives the new masterfile.

Sara
0
 

Author Comment

by:soniaN310
ID: 36892527
sarabande,
Thank you for your reply,
I don't have much experience with C++, just taking a C++ class for the first time.
Would you be able to help me with some code please?
Thank you,
soniaN310
0
 
LVL 32

Expert Comment

by:sarabande
ID: 36909000
i could only help with code when you firstly posted some own attempts to solve the question.

so, i would suggest you create a new c++ project  (win32 - console) and try to specifiy the structure i mentioned in my previous post. it is like

struct Address
{
// add here the members according to the telegram
};

Open in new window


then, add a main function and try to add code following the brief "design" i made.

when you need help, post your current work and ask.

Sara
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:soniaN310
ID: 36919066
Please help me from here



#include <iostream>
#include <fstream> 
#include <string>
#include <sstream>
#include<iomanip>
#include <algorithm>
#include <vector>
#include <iterator> 
#include <limits>


using namespace std;


 int main () 
{

	int customer_id;
                     string line; 
                     string customer_Fname, customer_Mname, customer_Lname, customer_Address1, customer_Address2, customer_City, customer_State, customer_Zip, Zip4, customer_SSN;

   
	ifstream newData("test.txt");
	vector<string>data1;
	if (newData.is_open())
	{
		string line;
		while(getline(newData, line))
		{
			data1.push_back (line);
		}
	}
	else
	{		
		cout << "Unable to open file test.txt";
		return 1;	
	}

    ifstream storeData("master.txt");
	if (storeData.is_open())
	{
		string line;
		while (getline(storeData, line))
		{
			for(size_t i = 0; i < data1.size(); ++i)
			{				
				if (line == data1[i])					
					data1.erase(data1.begin()+i);
			}
		}
	}
	else
	{
		cout << "Unable to open file master.txt";		
		return 1;
	}



    ofstream resultData("result.txt");
	if (resultData.is_open())
	{
		for (size_t i = 0; i < data1.size(); ++i)
		{			
			resultData << data1[i] << endl;		
		}	
	}	
	else
	{		
		cout << "Unable to open file result.txt";		
		return 1;	
	}	
	return 0;

Open in new window

0
 
LVL 23

Accepted Solution

by:
Michael74 earned 250 total points
ID: 36922427
To solve this I would

1/ Read the contents of the master file into a map. This would be very slow with large files but for this example would work fine. Use the ID as the key and the entire row as the value

Here is some info on C++ maps
http://www.cplusplus.com/reference/stl/map/

2/ The iterate through the weekly file and for each line
   - Extract the ID
   - Use the map find function to see if the ID exists.
      - If ID exists replace the value for that key with the contents of the line
      - If the ID does not exist then append line and key to the map

3/ Once completed overwrite the master file with the contents of the map by iterating through the map and adding the contents of each value as a line of the file. Remember that each value already contains the new line character

As noted this may not be the most efficient method but for the purpose of the assignment will work fine

Michael

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 36922580
do you know the either file is sortesd by id?

If so all youve got to do is iterate down both files at the same time reading a line at a time.  check the updates line for id number (string handling), read down the main file until you either hit the update id or greater writing each line out to a temp file.  if you hit the same number or dont find it then you write that update line to the master file and carry on to the next update line.

If not in order then consider sorting them, bring them into a databse structure or recording the ids from update file in an array.  read each line of master file, if id matches one in array then replace with that and mark it used / remove from array.  if anything left at end from updates add them on the end.

Sorry that was a bit rambling in the end... Was clear in my head when i started writing it!
0
 
LVL 32

Expert Comment

by:sarabande
ID: 36922694
to continue with your approach first switch reading of newdata and master. that way you could add only those records to the vector data1 which were new.

then write code to extract the id from a record.

it is to find the position of the first separator with line.find('|') and then assign the substring from 0 to that position - what is line.substr(0, pos) - to a new string.

for the master.txt push the id to a new vector of strings.

for the newdata.txt also extract the id from line but then search the id in the id vector. you could do that by using std::find but for simplicity i would suggest to start a new loop and simply compare the id with all elements of the id vector. if you find the id you need to replace the record of same index in data1 vector with the new line from newdata. if the id doesn't match you simply add the new line to the data1 by push_back and also add the new id to the id vector. the latter would also prevent you from copying duplicate ids which were in the newdata.txt.

the final storing then would store the merged data to a new file.

Sara
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36922730
reason i  suggested reading the updates is I would assume you may  have, say, 10k customer records but only 50 are updated say..... But I guess it doesnt make a lot of difference either way unless the record sets are huge
0
 
LVL 32

Expert Comment

by:sarabande
ID: 36922772
to get rid of the tabs in your code and were using visual studio, you could select all text (ctrl+a)  and choose menu edit - advanced... there you find option to remove tabs from selection.

to permanently get spaces in your code instead of tabs you may change the settings of the editor(s) below tools - option.

Sara
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36947960
Don't forget to accept answer(s) then!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now