14 Replies - 820 Views - Last Post: 09 December 2016 - 09:21 AM

#1 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-February 13

How to process the transactions?

Posted 08 December 2016 - 08:41 AM

I'm having trouble figuring out what methods are needed to process transactions from a data file, then print them out. I have the print methods working, but have no clue what methods are needed for the actual processing.

What it's supposed to do is go through the transactions file InfileT, then any part numbers that match up with entries in the Parts array have their quantities subtracted from the appropriate entries in NewStock, which is what is displayed through the second print method. Any that don't match get sent to the Invalid Array, which get printed with the Invalid #s heading.

/*---------------------------------------------------------------------------
Program: P09.cpp
Author:  Your Name
---------------------------------------------------------------------------*/

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
#include <assert.h>

using namespace std;
using namespace System;

const char *INFILEM = "P09M.DAT";
const char *INFILET = "P09T.DAT";
const char *AUTHOR = "Joshua Lefelhocz        Assignment 9";
const char *HEAD1 = "OLD INVENTORY";
const char *HEAD2 = "NEW INVENTORY";
const char *HEAD3 = "ITEM NO    QUANTITY";
const char *MESS = "Invalid Part#  ";



void PrintMaster(const int Parts[], const int OldStock[], const int size);

void PrintMaster(const int Parts[], const int OldStock[],
	const int NewStock[], const int size);
// The function, PrintMaster(), is Overloaded

int  FindMatchIndex(const int Parts[], const int size, const int partNum);
// Complete code for this function is provided

void PrintInvalid(const int Invalid[], const int invalidCount);
// Complete code for this function is provided

void Pause();  // Complete code for this function is provided



void main()
{
	int Parts[10], OldStock[10], NewStock[10], Invalid[25];
	int k, partNum, quantity, invalidCount = 0;
	int matchIndex;

	Console::Clear();
	//..... Print Headings (First Screen)


	ifstream InfileM(INFILEM);      //..... Open Master File:
	assert(InfileM);
	for (k = 0; k<10; k++) {
		InfileM >> Parts[k] >> OldStock[k];
		NewStock[k] = OldStock[k];
	}
	//..... Read Master File data and load arrays Parts[] and OldStock[]
	//..... Copy OldStock[] to NewStock[]
	InfileM.close();                //..... Close Master File:


	PrintMaster(Parts, OldStock, 10);
	Pause();

	//..... Print Headings (Second Screen)

	ifstream InfileT(INFILET);      //..... Open Transaction File
	assert(InfileT);

	while (InfileT >> partNum >> quantity)
	{
		//..... Process Transactions
		matchIndex = FindMatchIndex(Parts, k, partNum);
		for (k = 0; k < 25; k++)
		{
			if (partNum != Parts[matchIndex])
				Invalid[k] = Parts[matchIndex];
		}
		
		
	} //while
	InfileT.close();                //....... Close Transaction File

	PrintMaster(Parts, OldStock, NewStock, 10);
	cout << endl << endl;

	PrintInvalid(Invalid, invalidCount);
} //main()

  /*------------------ Function: Pause() ------------------------------------*/
void Pause()
{
	Console::SetCursorPosition(1, 24);
	cout << "Hit any key to continue.......";
	_getch();
	Console::Clear();
} //Pause()

  /*------------------ Function: PrintMaster() ------------------------------*/
void PrintMaster(const int Parts[], const int OldStock[], const int size)
{ 
	cout << AUTHOR << endl << HEAD1 << endl << HEAD3 << endl;
	for (int i = 0; i<size; i++)
	{
		cout << setw(5) << Parts[i] << setw(11) << OldStock[i] << endl;
	}

} //PrintMaster()

  /*------------------ Function: PrintMaster() ------------------------------*/
void PrintMaster(const int Parts[], const int OldStock[],
	const int NewStock[], const int size)
{ 
	cout << AUTHOR << endl << HEAD2 << endl << HEAD3 << endl;
	for (int i = 0; i<size; i++)
	{
		cout << setw(5) << Parts[i] << setw(11) << NewStock[i] << endl;
	}
} //PrintMaster()

  /*------------------ Function: FindMatchIndex() ---------------------------*/
int FindMatchIndex(const int Parts[], const int size, const int partNum)
{
	for (int k = 0; k<size; k++)  if (Parts[k] == partNum)  return k;
	return  -1;
} //FindMatchIndex()

  /*------------------ Function: PrintInvalid() -----------------------------*/
void PrintInvalid(const int Invalid[], const int invCount)
{
	for (int k = 0; k<invCount; k++) cout << endl << setw(45) << MESS << Invalid[k];
}

/*------------------ End of File: P09.cpp ---------------------------------*/



This post has been edited by darthvader45: 08 December 2016 - 08:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to process the transactions?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5830
  • View blog
  • Posts: 19,876
  • Joined: 05-May 12

Re: How to process the transactions?

Posted 08 December 2016 - 09:45 AM

Moving to C++/CLI.
Was This Post Helpful? 0
  • +
  • -

#3 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-February 13

Re: How to process the transactions?

Posted 08 December 2016 - 09:46 AM

Thank you. Awaiting any help with this.

This post has been edited by Skydiver: 08 December 2016 - 09:56 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2329
  • View blog
  • Posts: 9,359
  • Joined: 03-December 12

Re: How to process the transactions?

Posted 08 December 2016 - 10:02 AM

Quote

I'm having trouble figuring out what methods are needed to process transactions from a data file,


What aren't you sure of?

Quote

any part numbers that match up with entries in the Parts array have their quantities subtracted from the appropriate entries in NewStock


That looks like a method or 2.

Quote

Any that don't match get sent to the Invalid Array


That looks like another method, or decision structure at least.
Was This Post Helpful? 0
  • +
  • -

#5 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-February 13

Re: How to process the transactions?

Posted 08 December 2016 - 10:06 AM

What kind of method would I need in order to do the subtracting? Also, what decision structure would be needed to send non-matching part numbers to the invalid array?

Ok, I've got the subtraction working, but some of the values output are slightly off.

My code:
/*---------------------------------------------------------------------------
Program: P09.cpp
Author:  Your Name
---------------------------------------------------------------------------*/

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
#include <assert.h>

using namespace std;
using namespace System;

const char *INFILEM = "P09M.DAT";
const char *INFILET = "P09T.DAT";
const char *AUTHOR = "Joshua Lefelhocz        Assignment 9";
const char *HEAD1 = "OLD INVENTORY";
const char *HEAD2 = "NEW INVENTORY";
const char *HEAD3 = "ITEM NO    QUANTITY";
const char *MESS = "Invalid Part#  ";



void PrintMaster(const int Parts[], const int OldStock[], const int size);

void PrintMaster(const int Parts[], const int OldStock[],
	const int NewStock[], const int size);
// The function, PrintMaster(), is Overloaded

int  FindMatchIndex(const int Parts[], const int size, const int partNum);
// Complete code for this function is provided

void PrintInvalid(const int Invalid[], const int invalidCount);
// Complete code for this function is provided

void Pause();  // Complete code for this function is provided



void main()
{
	int Parts[10], OldStock[10], NewStock[10], Invalid[25];
	int k, partNum, quantity, invalidCount = 0;
	int matchIndex;

	Console::Clear();
	//..... Print Headings (First Screen)


	ifstream InfileM(INFILEM);      //..... Open Master File:
	assert(InfileM);
	for (k = 0; k<10; k++) {
		InfileM >> Parts[k] >> OldStock[k];
		NewStock[k] = OldStock[k];
	}
	//..... Read Master File data and load arrays Parts[] and OldStock[]
	//..... Copy OldStock[] to NewStock[]
	InfileM.close();                //..... Close Master File:


	PrintMaster(Parts, OldStock, 10);
	Pause();

	//..... Print Headings (Second Screen)

	ifstream InfileT(INFILET);      //..... Open Transaction File
	assert(InfileT);

	while (InfileT >> partNum >> quantity)
	{
		//..... Process Transactions
		int quantity2;
		for (k = 0; k < 25; k++)
		{
			if (partNum = Parts[k])
				quantity2 = NewStock[k] - quantity;
			quantity = quantity2;
			InfileT >> quantity >> NewStock[k];
		}


	} //while
	InfileT.close();                //....... Close Transaction File

	PrintMaster(Parts, OldStock, NewStock, 10);
	cout << endl << endl;

	PrintInvalid(Invalid, invalidCount);
} //main()

  /*------------------ Function: Pause() ------------------------------------*/
void Pause()
{
	Console::SetCursorPosition(1, 24);
	cout << "Hit any key to continue.......";
	_getch();
	Console::Clear();
} //Pause()

  /*------------------ Function: PrintMaster() ------------------------------*/
void PrintMaster(const int Parts[], const int OldStock[], const int size)
{
	cout << AUTHOR << endl << HEAD1 << endl << HEAD3 << endl;
	for (int i = 0; i<size; i++)
	{
		cout << setw(5) << Parts[i] << setw(11) << OldStock[i] << endl;
	}

} //PrintMaster()

  /*------------------ Function: PrintMaster() ------------------------------*/
void PrintMaster(const int Parts[], const int OldStock[],
	const int NewStock[], const int size)
{
	cout << AUTHOR << endl << HEAD2 << endl << HEAD3 << endl;
	for (int i = 0; i<size; i++)
	{
		cout << setw(5) << Parts[i] << setw(11) << NewStock[i] << endl;
	}
} //PrintMaster()

  /*------------------ Function: FindMatchIndex() ---------------------------*/
int FindMatchIndex(const int Parts[], const int size, const int partNum)
{
	for (int k = 0; k<size; k++)  if (Parts[k] == partNum)  return k;
	return  -1;
} //FindMatchIndex()

  /*------------------ Function: PrintInvalid() -----------------------------*/
void PrintInvalid(const int Invalid[], const int invCount)
{
	for (int k = 0; k<invCount; k++) cout << endl << setw(45) << MESS << Invalid[k];
}

/*------------------ End of File: P09.cpp ---------------------------------*/


'

My Output So far for the new inventory:

Joshua Lefelhocz        Assignment 9
NEW INVENTORY
ITEM NO    QUANTITY
  245          4
  147         16
  356         10
  238         12
  495         12
  362         20
  225         15
  715         25
  821         10
  625          3




C:\Users\joshl\Documents\Visual Studio 2015\Projects\Program 9\Debug>

The intended output for New Inventory:
Joshua Lefelhocz        Assignment 9
NEW INVENTORY
ITEM NO    QUANTITY
  245         11
  147          5
  356          7
  238         16
  495         18
  362          4
  225          5
  715         10
  821         24
  625         39




C:\Users\joshl\Documents\Visual Studio 2015\Projects\Program 9\Debug>

This post has been edited by Skydiver: 08 December 2016 - 09:54 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2329
  • View blog
  • Posts: 9,359
  • Joined: 03-December 12

Re: How to process the transactions?

Posted 08 December 2016 - 10:37 AM

You need to step thru the application as it runs. Watch the variable values. I don't know what is being subtracted from what to be able to say one thing or another.


Quote

Also, what decision structure would be needed to send non-matching part numbers to the invalid array?

How many decision structures do you know of?
Was This Post Helpful? 1
  • +
  • -

#7 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-February 13

Re: How to process the transactions?

Posted 08 December 2016 - 10:46 AM

What I have for the subtraction is this:

If a part number in the transactions file (P09T.DAT) matches a part number in the Parts array, then the quantity value of that element in the Parts array gets subtracted from the quantity gotten from the transactions file. That's what this code is doing:

while (InfileT >> partNum >> quantity)
	{
		//..... Process Transactions
		int quantity2;
		for (k = 0; k < 25; k++)
		{
			if (partNum = Parts[k])
				quantity2 = NewStock[k] - quantity;
			quantity = quantity2;
			InfileT >> quantity >> NewStock[k];
		}


	}

This post has been edited by Skydiver: 08 December 2016 - 09:55 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#8 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-February 13

Re: How to process the transactions?

Posted 08 December 2016 - 11:36 AM

By the way, I know of only two decision structures: if statements and switch statements.

This post has been edited by Skydiver: 08 December 2016 - 09:55 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#9 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2329
  • View blog
  • Posts: 9,359
  • Joined: 03-December 12

Re: How to process the transactions?

Posted 08 December 2016 - 11:49 AM

It should be pretty obvious that an if statement would be needed. If the partNum exists, call the subtraction function, else call the invalidate function.
Was This Post Helpful? 0
  • +
  • -

#10 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-February 13

Re: How to process the transactions?

Posted 08 December 2016 - 11:58 AM

Can I see an example, using my code?

This post has been edited by Skydiver: 08 December 2016 - 09:55 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#11 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2329
  • View blog
  • Posts: 9,359
  • Joined: 03-December 12

Re: How to process the transactions?

Posted 08 December 2016 - 12:01 PM

HAHAHA no.


The pseudo code is in the sentence.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5830
  • View blog
  • Posts: 19,876
  • Joined: 05-May 12

Re: How to process the transactions?

Posted 08 December 2016 - 09:53 PM

darthvader45: I already told you all the way back in October that there is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area. All you are doing is wasting screen real estate and bandwidth. You are not helping people who are trying to follow along a thread while on a mobile device what have both limited screen real state and bandwidth. Even for folks on regular laptops and desktop machines with bigger screens, it's still a lot of scrolling that needs to happen to get to the meat of a thread.

If want to quote because you want to discuss a particular point, then pick out those relevant pieces. See astonecipher's examples above.
Was This Post Helpful? 0
  • +
  • -

#13 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-February 13

Re: How to process the transactions?

Posted 09 December 2016 - 08:40 AM

Well, I have the subtraction code in the if statement. How would I change my subtraction code to give output that is more like what is intended?
Was This Post Helpful? 0
  • +
  • -

#14 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2329
  • View blog
  • Posts: 9,359
  • Joined: 03-December 12

Re: How to process the transactions?

Posted 09 December 2016 - 08:54 AM

Debug. You are using visual studio, it has awesome debugging tools at your disposal. Step thru the code and watch your creation work. This will tell you what it read in, and what the outcome of the subtraction is, while it is happening.
Was This Post Helpful? 0
  • +
  • -

#15 darthvader45  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 21-February 13

Re: How to process the transactions?

Posted 09 December 2016 - 09:21 AM

Ok, I've got the output really close to what I need, even with the invalid part #'s printing. Here is my current code:
/*---------------------------------------------------------------------------
Program: P09.cpp
Author:  Your Name
---------------------------------------------------------------------------*/

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <fstream>
#include <assert.h>

using namespace std;
using namespace System;

const char *INFILEM = "P09M.DAT";
const char *INFILET = "P09T.DAT";
const char *AUTHOR = "Joshua Lefelhocz        Assignment 9";
const char *HEAD1 = "OLD INVENTORY";
const char *HEAD2 = "NEW INVENTORY";
const char *HEAD3 = "ITEM NO    QUANTITY";
const char *MESS = "Invalid Part#  ";



void PrintMaster(const int Parts[], const int OldStock[], const int size);

void PrintMaster(const int Parts[], const int OldStock[],
	const int NewStock[], const int size);
// The function, PrintMaster(), is Overloaded

int  FindMatchIndex(const int Parts[], const int size, const int partNum);
// Complete code for this function is provided

void PrintInvalid(const int Invalid[], const int invalidCount);
// Complete code for this function is provided

void Pause();  // Complete code for this function is provided



void main()
{
	int Parts[10], OldStock[10], NewStock[10], Invalid[25];
	int k, partNum, quantity, invalidCount = 0;
	int matchIndex;

	Console::Clear();
	//..... Print Headings (First Screen)


	ifstream InfileM(INFILEM);      //..... Open Master File:
	assert(InfileM);
	for (k = 0; k<10; k++) {
		InfileM >> Parts[k] >> OldStock[k];
		NewStock[k] = OldStock[k];
	}
	//..... Read Master File data and load arrays Parts[] and OldStock[]
	//..... Copy OldStock[] to NewStock[]
	InfileM.close();                //..... Close Master File:


	PrintMaster(Parts, OldStock, 10);
	Pause();

	//..... Print Headings (Second Screen)

	ifstream InfileT(INFILET);      //..... Open Transaction File
	assert(InfileT);

	while (InfileT >> partNum >> quantity)
	{
		//..... Process Transactions
		
		for (k = 0; k < 25; k++)
		{
			if (partNum == Parts[k])
				quantity = NewStock[k] - quantity;
			else 
				InfileT >> Invalid[k] >> partNum;
				
			
			InfileT >> quantity >> NewStock[k];
			while (invalidCount < 5)
				invalidCount = invalidCount + 1;

			
		}


	} //while
	InfileT.close();                //....... Close Transaction File

	PrintMaster(Parts, OldStock, NewStock, 10);
	cout << endl << endl;

	PrintInvalid(Invalid, invalidCount);
} //main()

  /*------------------ Function: Pause() ------------------------------------*/
void Pause()
{
	Console::SetCursorPosition(1, 24);
	cout << "Hit any key to continue.......";
	_getch();
	Console::Clear();
} //Pause()

  /*------------------ Function: PrintMaster() ------------------------------*/
void PrintMaster(const int Parts[], const int OldStock[], const int size)
{
	cout << AUTHOR << endl << HEAD1 << endl << HEAD3 << endl;
	for (int i = 0; i<size; i++)
	{
		cout << setw(5) << Parts[i] << setw(11) << OldStock[i] << endl;
	}

} //PrintMaster()

  /*------------------ Function: PrintMaster() ------------------------------*/
void PrintMaster(const int Parts[], const int OldStock[],
	const int NewStock[], const int size)
{
	cout << AUTHOR << endl << HEAD2 << endl << HEAD3 << endl;
	for (int i = 0; i<size; i++)
	{
		cout << setw(5) << Parts[i] << setw(11) << NewStock[i] << endl;
	}
} //PrintMaster()

  /*------------------ Function: FindMatchIndex() ---------------------------*/
int FindMatchIndex(const int Parts[], const int size, const int partNum)
{
	for (int k = 0; k<size; k++)  if (Parts[k] == partNum)  return k;
	return  -1;
} //FindMatchIndex()

  /*------------------ Function: PrintInvalid() -----------------------------*/
void PrintInvalid(const int Invalid[], const int invCount)
{
	for (int k = 0; k<invCount; k++) cout << endl << setw(45) << MESS << Invalid[k];
}

/*------------------ End of File: P09.cpp ---------------------------------*/



Also, my current output on the second screen:

Joshua Lefelhocz        Assignment 9
NEW INVENTORY
ITEM NO    QUANTITY
  245         16
  147         12
  356         20
  238         25
  495          3
  362         25
  225         15
  715         20
  821         24
  625         39



                              Invalid Part#  147
                              Invalid Part#  115
                              Invalid Part#  225
                              Invalid Part#  522
                              Invalid Part#  925
C:\Users\joshl\Documents\Visual Studio 2015\Projects\Program 9\Debug>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1