Parallel array functions


I am having trouble with my extractData function. First, my code is not recognizing the function definition. I checked the spelling and number of parameters, but don't know what else could be the problem. Also, I am getting the error, "unresolved external symbol" Could someone explain what that means or how to fix it? My code is below:

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
 #include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace::std;

const int ORDER_VALUE = 500;
bool loadArrays(const char fileName[], long idArray[], int storeArray[], int qtyArray[], int & count, int maxCells);
void printArrays(ostream & where, const long idArray[], const int storeArray[], const int qtArray[], int count);
bool extractData(const char newFileName[], int requestId, int baseQty, const long idArray[], const int storeArray[], const int qtArray[], int count, int & newcount);


void main()
{
	const int maxCells = 100;
	const char fileName[100] = "Project3Records";
	const char newFileName[100] = "Project3ExtractedRecords";
	int requestId;
	long idArray[maxCells];
	int storeArray[maxCells];
	int qtyArray[maxCells];
	int count = 0;
	int newCount = 0;

	bool loadResult;
	loadResult = loadArrays(fileName, idArray, storeArray, qtyArray, count, maxCells);
	if (loadResult == false)
	{
		if (count == 0)
		{
			cout << "Could not open input file " << fileName << endl;
			exit(1);
		}
		else
		{
			cout << "Could not load all data from: " << fileName << endl;
			exit(2);
		}
	}

	printArrays(cout, idArray, storeArray, qtyArray, count);
	cout << "Please enter a product ID: " << endl;
	cin >> requestId;

	bool requestResult;
	requestResult = extractData(newFileName, requestId, ORDER_VALUE, idArray, storeArray, qtyArray, count, & newCount);
	if (requestResult == false)
	{
		cout << "Could not open file: " << newFileName << endl;
		exit(3);
	}

	cout << newCount << "lines were extracted." << endl;

}

bool loadArrays(const char name[], long id[], int store[], int qty[], int & cnt, int max) // the constant ones might need to match in name
{
	ifstream in;
	in.open("C:\\Users\\Sophie\\Downloads\\Project3Records.txt");
	if (!in)
	{
		cout << "Can not open file." << endl;
		exit(8);
	}
	int i = 0;
	while (i < max && in >> id[i] >> store[i] >> qty[i])
	{
		i++;
	}
	cnt = i;
	if (in.good()) //still more data in file if good
	{
		in.close();
		return false;
	}
	else
	{
		in.close();
		return true;
	}
}

void printArrays(ostream & where, const long idArray[], const int storeArray[], const int qtArray[], int count)
{
	for (int i = 0; i < 20; i++) // is 20 the right num? should it be the count variable?
	{
		where << idArray[i] << " " << storeArray[i] << " " << qtArray[i] << endl;
	}
	return;
}

bool extractData(const char newFile[], int reqId[], int base, const long idArray[], const int storeArray[], const int qtArray[], int count, int & newCount)
{
	ofstream out;
	out.open("C:\\Users\\Sophie\\Downloads\\Project3ExtractedRecords.txt");
	if (!out) return false;
	newCount = 0;
	for (int i = 0; i < count; i++)
	{
		if (qtArray[i] < base)
		{
			out << reqId[i];
			newCount++;
		}
		out.close();
		return true;
	}

}
Last edited on
closed account (48T7M4Gy)
Please change [\code] to [/code]
done, sorry! First time user.
closed account (48T7M4Gy)
First problem is it should be int main() not void main()
closed account (48T7M4Gy)
Your extractData function needs to be examined very closely because you have a series of compounding errors.

First of all the parameters must match in three lines, namely lines 93, 46 and 10

You will have to decide what you are doing but newCount doesn't match, requestId (or is it requestID[] ? ) doesn't match.
I made both of those corrections and it is still not functioning properly. Would you like me to post the new code?
Also, I am getting the same errors.
closed account (48T7M4Gy)
I think your next step, so we can help you, is to post the assignment question.

The difficulty is that it is not clear (to me at least) what the end target is. Comments help but I think the overall picture is more important otherwise we might just go around in circles. Don't interpret this that your program is a disaster, it's not. All it needs is a bit of a tidy up :)
CS 200
Parallel Project

1. Using an editor create a text file that has around 20 lines of information. Each line of information will include a product id, a store number, and a quantity.

2. Write three C++ functions as described below:

The variables listed below have the following meaning:

fileName - the Windows name of the file created in step 1
newFileName - the Windows file name of the file of extracted records

requestId - the product id number used to extract data
ORDER_VALUE - the quantity value used to extract data, make this
a global constant with a value of 500

idArray - the array of id numbers
storeArray - the array of store numbers
qtyArray - the array of quantities

count - the actual number of cells filled in the arrays
newcount - the number of extracted records written to newFileName


a) bool loadArrays(const char fileName[],long idArray[],
int storeArray[], int qtyArray[], int & count, int maxCells)

Will load the three arrays from data from the disk file and use count for the number of cells loaded. Be certain to check that you do not load more cells than you have dimensioned.
Return true if you were able to load all of the data,
return false otherwise. The variable count will hold the
number of cells loaded.

b) void printArrays(ostream & where, const long idArray[],
const int storeArray[], const int qtArray[], int count)

Will print to the stream where the data in the arrays.
Count tells the number of cells filled.

c) bool extractData(const char newFileName[],int requestId, int baseQty,
const long idArray[], const int storeArray[],
const int qtArray[], int count, int & newcount)

Will use the value in requestId to write only the records with that id number that have a quantity below baseQty(the ORDER_VALUE) to the new file. The variable newcount will hold the number of records actually written. The return value will be false if the
file could not be opened, otherwise the return value will be true.

3. Write a main program to test the three functions. Always check to see that a file was successfully opened. If the file was NOT successfully opened, write an error message and exit the program.

4. Turn in:
- C++ source program
- Two execution outputs – one with all data loaded and one
- where there was not room for all data.
- A copy of your file created in step 1
- A copy of the file created by extractData

Thank you for the help!
closed account (48T7M4Gy)
OK thanks. We'll get back. :)
closed account (48T7M4Gy)
Have you got your 20 line data file - just post it here please.
1234 11 567
1236 11 6
1236 13 77
1236 14 1
1238 13 567
1239 16 3
1241 17 2
1241 18 0
1242 19 740
1244 22 2
1244 21 756
1245 22 1
1246 24 2
1246 24 512
1248 25 634
1249 26 3
1250 26 75
1250 28 46
1252 29 967
1253 30 0
1254 30 24
closed account (48T7M4Gy)
OK - be a small while :)
closed account (48T7M4Gy)
Here's a start. Note where I have put // <--
You need to consider counting the number of records.
I've comment out for the moment the bits that need work later on. Don't throw them away. :)

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace::std;

const int ORDER_VALUE = 500;

bool loadArrays(const char[], long[], int[], int[], int &, int); // <--
void printArrays(ostream &, const long[], const int[], const int[], int); // <--
bool extractData(const char[], int, int, const long[], const int[], const int[], int, int&); // <--


int main()// <--
{
    const int maxCells = 100;
    const char fileName[100] = "Project3Records.txt"; // <--
    const char newFileName[100] = "Project3ExtractedRecords.txt"; // <--
    int requestId;
    long idArray[maxCells];
    int storeArray[maxCells];
    int qtyArray[maxCells];
    int count = 0;
    int newCount = 0;
    
    bool loadResult;
    loadResult = loadArrays(fileName, idArray, storeArray, qtyArray, count, maxCells);
    
    if (loadResult == false)
    {
        if (count == 0)
        {
            cout << "Could not open input file " << fileName << endl;
            exit(1);
        }
        else
        {
            cout << "Could not load all data from: " << fileName << endl;
            exit(2);
        }
    }
    
    printArrays(cout, idArray, storeArray, qtyArray, count);
    
    cout << "Please enter a product ID: ";// <--
    cin >> requestId;
    
    
    
    bool requestResult;
    
    requestResult = extractData(
                                newFileName,
                                requestId,
                                ORDER_VALUE,
                                idArray,
                                storeArray,
                                qtyArray,
                                count,
                                newCount // <--
                                );
    
    if (requestResult == false)
    {
        cout << "Could not open file: " << newFileName << endl;
        exit(3);
    }
    
    cout << newCount << "lines were extracted." << endl;
    
    return 0;
}

bool loadArrays(const char name[], long id[], int store[], int qty[], int &cnt, int max)
{
    ifstream in;
    in.open(name); // <---
    if (!in)
    {
        cout << "Can not open file." << endl;
        exit(8);
    }
    
    int i = 0;
    while (i < max && in >> id[i] >> store[i] >> qty[i])
    {
        i++;
    }
    cnt = i;
    
    if (in.good()) //still more data in file if good
    {
        in.close();
        return false;
    }
    else
    {
        in.close();
        return true;
    }
}

void printArrays(ostream & where, const long idArray[], const int storeArray[], const int qtArray[], int count)
{
    
    for (int i = 0; i < count; i++) //<---
    {
        where << idArray[i] << " " << storeArray[i] << " " << qtArray[i] << endl;
    }
    return;
}


bool extractData(const char newFile[], int reqId, int baseQty, const long idArray[], const int storeArray[], const int qtArray[], int count, int & newCount)
{
    ofstream out;
    out.open(newFile);
    if (!out) return false;
    newCount = 0;
    
    for (int i = 0; i < count; i++) // <--
    {
        if (qtArray[i] < baseQty && reqId ???????? ) // <-- Fill in
        {
            out << reqId << ' ' << idArray[i] ????????? << '\n'; // <-- Fill in
            newCount++;
        }
    }
    out.close(); // <--
    return true;
}
Last edited on
closed account (48T7M4Gy)
BTW what is the exact error message you are getting with the unresolved external etc?
closed account (48T7M4Gy)
I re-posted the previous listing. Look carefully at the changes but they weren't monumentally huge.

It now runs and prints to the screen and opens up the send txt file. It's all working just make sure you are looking in the right directories for the output files. Trouble is you aren't getting the request worked on properly so you get no output lines in the second file.

What you now need to do is closely look at the logic within your extract data function.

I assume your process will be something like:

- take the request number
- go through all the items in the id array
- if there's a match
- do the quantity check
- print matched item(s) to file
- keep going to end of array

Come back if u have problems :)

closed account (48T7M4Gy)
It's all good now just fill in the ????'s. It works! :)
Topic archived. No new replies allowed.