Array values

closed account (9hUqko23)
Hi,

I'm trying to write a quick program to output a data string that looks something like "PCB #A-1111 PF". I imported all of the prefixes, dashes, and numbers into an array; but when I wrote the data out it looked like this...
PCB #A-2852 --
PCB #-2852A
PCB #2852A-
PCB #852A-2
PCB #52A-28
PCB #2A-286
PCB #A-2862 --
PCB #-2862A
PCB #2862A-
PCB #862A-2
PCB #62A-28
PCB #2A-287 etc.
only the two lines indexed by two dashes on the right side are correct.
Would someone give me direction on this.

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

using namespace std;

int main(void)
{
	int i;
	ofstream myfile("breaker number array output.csv"); //outputting the data to a .csv file
	if (myfile.is_open()) //making sure the file opened properly.
	{
		char breakerdata[3612];
		ifstream input("Breaker-data.txt");
		for (int i = 0; i < 3612; i++)
		{
			input >> breakerdata[i];
			myfile << breakerdata[i] << endl;
			cout << breakerdata[i] << endl;
		}
		ofstream myfile2("MEAG Breaker Alarm Names");
		if (myfile2.is_open())
		{
			for (int i = 0; i < 602; i++)
			{
				myfile2 << "PCB #" << breakerdata[i] << breakerdata[i+1] << breakerdata[i+2] <<breakerdata[i+3]
					<< breakerdata[i+4] << breakerdata[i+5] << endl;
			}
			
		}
		else
		{
			cout << "unable to open file" << endl;
		}
		//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		//cout << breakerdata[2] << endl;
		//cin >> A >> B;
		//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	}
	else
	{
		cout << "Something is wrong with your output file." <<
			" Data could not be stored" << endl;
	}
	return 0;
}
The loop control in your second for loop is causing issues...

The breakerdata[] array looks something like this:
1
2
3
index | 0123456789...
___________________
value | A-2852--A-2...


So the first iteration of that loop prints characters 0 through 5, the second iteration prints characters 1 through 6 etc.

Do you need to do this character by character? How about using the getline function and a string... like this:

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

using namespace std;

int main(void)
{
	int i;
	ofstream myfile("breaker number array output.csv"); //outputting the data to a .csv file
	if (myfile.is_open()) //making sure the file opened properly.
	{
		string breakerdata[3612];
		ifstream input("Breaker-data.txt");
		for (int i = 0; i < 3612; i++)
		{
			getline(input, breakerdata[i]);
			myfile << breakerdata[i] << endl;
			cout << breakerdata[i] << endl;
		}
		ofstream myfile2("MEAG Breaker Alarm Names");
		if (myfile2.is_open())
		{
			for (int i = 0; i < 602; i++)
			{
				myfile2 << "PCB #" << breakerdata[i]  << endl;
			}
			
		}
		else
		{
			cout << "unable to open file" << endl;
		}
		//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		//cout << breakerdata[2] << endl;
		//cin >> A >> B;
		//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	}
	else
	{
		cout << "Something is wrong with your output file." <<
			" Data could not be stored" << endl;
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.