print my output ".dat"

after development my coding i have fully finish my project but when I look the instruction, my teacher require us take the output to ".dat" and I can find my any syntax in my notes. need help for that as my last step!

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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>


int createZipcode();
int extract(int zip, int location);
int correction(int zipcode);
void displaycode(int digit);
void displaybarcode(int digit, int location);

void displaybarcode(int zipcode, int location)
{
	printf("             |");
	for (location = 1; location <= 5; location++)
	{
		displaycode(extract(zipcode, location));
	}

	printf("|\n");



}

void displaycode(int zipcode)
{
	switch (zipcode)
	{
	case 0:
		printf("||:::");
		break;
	case 1:
		printf(":::||");
		break;
	case 2:
		printf("::|:|");
		break;
	case 3:
		printf("::||:");
		break;
	case 4:
		printf(":|::|");
		break;
	case 5:
		printf(":|:|:");
		break;
	case 6:
		printf(":||::");
		break;
	case 7:
		printf("|:::|");
		break;
	case 8:
		printf("|::|:");
		break;
	case 9:
		printf("|:|::");
		break;
	}

}

int createZipcode()
{
	return 10000 + rand() % 99999;

}

int extract(int zip, int location)
{
	while (location <= 4)
	{

		location++;
		zip /= 10;
	}

	return zip % 10;
}
int correction(int zipcode)
{
	int sum = 0;
	sum = extract(zipcode, 1) + extract(zipcode, 2) + extract(zipcode, 3) + extract(zipcode, 4) + extract(zipcode, 5);
	return sum;
}




int main()
{


	printf("zip         code         digit barcode\n");
	{
		int zipcode;
		int digit;


		for (int k = 0; k <= 9; k++)
		{

			zipcode = createZipcode();

			digit = (10 - (correction(zipcode)) % 10)%10;

			printf("%d ", zipcode);
			printf("    %d", digit);
			displaybarcode(zipcode, 1);

		}



	}
	return 0;
}

 
To write data to a file in C++, you'll want to use classes found in the <fstream> header, mainly std::ofstream.

Check out:
http://www.cplusplus.com/reference/fstream/ofstream/
http://www.cplusplus.com/doc/tutorial/files/

Here's a small example that might relate:
1
2
3
4
5
6
7
8
#include <fstream>

int main()
{
    std::ofstream f("output.dat");
    const int n = 5;
    f << n << " squared = " << n*n << "!";
}

This will create and write to a file called "output.dat".

Notice the use of the << operator, it works the same way cout does http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/
Last edited on
Template.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>

int main(){

	std::ofstream output;//declare obj

	output.open("file.dat");//Open file 

	if (output.fail()){//If file didn't create terminate
		std::cerr << "File creation failed - Terminating.\n";
		return 1;
	}

	output << "Writing stuff to file.";//Output to file

	output.close();//Close file before exiting program
	return 0;
}
Last edited on
Oh and since you are sending output from several functions you need to pass the ofstream obj. by reference.

someFunc(std::ofstream& output){}
How could this code be modified to accept input for various zip codes while still outputting bar codes in this same fashion?
Topic archived. No new replies allowed.