Subtract values in a data file from elements in an array

I have an array called NewStock containing quantities of parts in inventory. I want to take input from a data file called P09T.DAT and take the values from the second column (also quantities, but this time items sold) and subtract it from the quantity value of each element in the array.

Basically I want this:

Part # Quantity <-(what's supposed to change)
245 27
147 12
356 29
238 34
495 18
362 29
225 42
715 65
821 24
625 39


To be this:
245 11
147 5
356 7
238 16
495 18
362 4
225 5
715 10
821 24 <-
625 39 <- These don't change.
I have an array called NewStock containing quantities of parts in inventory.

Does your array also contain the Part # ? Do you use two arrays, or a class/struct?
How does your code look.
The three arrays I use are: OldStock, NewStock, and Invalid. The first two contain part #'s and quantities of parts in stock. The third one contains all part #'s in the Transaction data file that did not match in the NewStock array.

Ok, here's my code so far:


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
/*---------------------------------------------------------------------------
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, 10, partNum);
		for (k = 0; k < 25; k++)
		{
			if (partNum == matchIndex)
			{
				quantity = NewStock[k] - quantity;
			}
				
			else
			{
				InfileT >> Invalid[k] >> partNum;
			}
				
			InfileT >> quantity >> NewStock[k];
			
			
			while (invalidCount < 5)
				invalidCount = invalidCount++;

			
		}


	} //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 ---------------------------------*/
Last edited on
Thank you for the code. I tried to run it (with a few trivial changes to suit my compiler environment)

At first, my input file didn't exist, so the run failed. Then I created an empty file "P09M.DAT".

Here there was an error in the program logic. Even when the file is empty, the program assumes that 10 items were read from the file.

So I changed it like this:
1
2
    const int SIZE = 10;
    int Parts[SIZE], OldStock[SIZE], NewStock[SIZE];

Then to read from the file
1
2
3
4
5
    int count = 0;
    for (count = 0; k<SIZE && InfileM >> Parts[count] >> OldStock[count]; count++)
    {    
        NewStock[count] = OldStock[count];
    }

The variable count will contain the actual number of records read from the file.

Later, when calling other functions, instead of the number 10, use count instead.
 
    PrintMaster(Parts, OldStock, count);


That's all so far.
Do you have a sample of the transactions file "P09T.DAT"?

edit:
The processing of transactions seems wrong. It starts very well, reading from the file and searching the array for a match - that's good. Then what happens next? Well, if the part was not found the function FindMatchIndex() returns a value of -1. If it is found, it returns the correct array index.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    while (InfileT >> partNum >> quantity)
    {
        //..... Process Transactions
        
        matchIndex = FindMatchIndex(Parts, count, partNum);
        
        if (matchIndex >= 0)
        {
            NewStock[matchIndex] -= quantity;
        }
        else
        {
            
        }
    } 


Last edited on
Thanks. I covered part of the code in my previous post. The rest of the code goes in the empty else { } block. I think you need to check that invalidCount < 25, if it is, place the part number in the Invalid array and add 1 to invalidCount.
Topic archived. No new replies allowed.