Issues Combining/Reading Arrays

Hello,
I'm trying to create a program that reads 2 input files, assigns the two input files to two arrays, then combines them into sequential order, and outputting them into an output file. I cannot seem to get my two input files to output/read correctly. The output file displays very large negative numbers. Does anyone see the issue that is causing this?
Thanks!

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

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
using namespace std;

void getInput(string prompt, ifstream& fileVariable);
void getOutput(string prompt, ofstream& fileVariable);





int main()
{
	ifstream inputFile1;
	ifstream inputFile2;
	ofstream outputFile;

	int array1[20];
	int array2[20];
	int finalArray[40];
	int arraySize = 40;
	int array1elements = 0;
	int array2elements = 0;
	int array1index = 0;
	int array2index = 0;



	getInput("What is the name of the first input file? ", inputFile1);

	do
	{
		inputFile1 >> array1[array1elements];
		array1elements++;
	} while (!inputFile1.eof());

	getInput("What is the name of the second input file? ", inputFile2);

	do
	{
		inputFile2 >> array2[array2elements];
		array2elements++;
	} while (!inputFile2.eof());

	getOutput("What is the name of the output file? ", outputFile);

		do
		{
			if (array1elements = -1)
			{
				outputFile << array2[array2index] << " ";
				array2index++;
				array2elements--;
			}
			else if (array2elements = -1)
			{
				outputFile << array1[array1index] << " ";
				array1index++;
				array1elements--;
			}
			else if (array1[array1index] == array2[array2index])
			{
				outputFile << array1[array1index] << " ";
				array1index++;
				array2index++;
				array1elements--;
				array2elements--;
			}
			else if ((array1[array1index]) > (array2[array2index]))
			{
				outputFile << array2[array2index] << " ";
				array2index++;
				array2elements--;
			}
			else
			{
				outputFile << array1[array1index] << " ";
				array1index++;
				array1elements--;
			}
		} while ((array1elements >= 0) || (array2elements >= 0));




	inputFile1.close();
	inputFile2.close();
	outputFile.close();
    return 0;
}

void getInput(string prompt, ifstream& fileVariable)
{
	string fileName(" ");
	do
	{
		cout << prompt;
		cin >> fileName;

		fileVariable.open(fileName);

		if (fileVariable.fail())
		{
			cout << "Error, incorrect file name. Please try again..." << endl;
		}
	} while (fileVariable.fail());
}

void getOutput(string prompt, ofstream& fileVariable)
{
	string fileName(" ");

	do
	{
		cout << prompt;
		cin >> fileName;

		fileVariable.open(fileName);

		if (fileVariable.fail())
		{
			cout << "Error, please try again..." << endl;
		}
	} while (fileVariable.fail());
}


Input 1
 
9 10 12 15 19 30 36 39 48 49 52 60 65 

Input 2
 
7 9 13 17 20 26 30 40 43 44 50 54 60 


This is what I get
Output
 
7 9 13 17 20 26 30 40 43 44 50 54 60 -858993460 -858993460 


This is what I want
 
7 9 12 13 15 17 19 20 26 30 36 39 40 43 44 48 49 50 52 54 60 65
Last edited on
Topic archived. No new replies allowed.