Character in struct

Line 15: I need to compare the length of the two new integers formed and specify if the length of the odd digits integer is Longer, Shorter or Equal than that of the even digits integer. Kindly advise on how to do it?

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;


struct Number
{
	int no;
	int oddDigits;
	int evenDigits;
	int sumDigits;
	int noDigits;
	char status [10];  // will take value "Longer or "Shorter" or "Equal"
};

void constructInfile (fstream&, char []);
int fileToArray (fstream&, char [], Number []);
void getNumbersInfo (Number [], int);
void arrayToFile (ofstream&, char [], Number [], int);

int NumType (int type);

const int MAX = 10;

int main ()
{

	fstream afile;
	
	Number numbers [MAX];
	int size;
	
	srand (time (NULL));
	
	constructInfile (afile, "infile.txt");
	
	size = fileToArray (afile, "infile.txt", numbers);
	
	ofstream outfile;
	
	arrayToFile (outfile, "outfile.txt", numbers, size);
	
	return 0;
}	// main

// Construct infile.txt with 2-10 numbers of integers
// not more than 100000
void constructInfile (fstream& afile, char fileName [])
{
	int size = rand () % 9 + 2;
	
	afile.open (fileName, ios::out);
	
	if (!afile)
	{
		cout << "File Status: Open for writing failed" << endl;
		exit (1);
	}
	
	for (int i = 1; i <= size; i++)
	{
		afile << rand () * rand () % 10000
			  << "\t //Number " << i << endl;
	}
	
	afile.close ();
}	// constuctInfile

// Read infile.txt, constructs an array from the numbers
// and get the info from the numbers
int fileToArray (fstream& afile, char fileName [], Number numbers [])
{
	int size = 0;
	int number;
	
	afile.open (fileName, ios::in);
	
	while (afile >> number)
	{
		afile.clear ();
		afile.ignore (100, '\n');
		
		numbers [size].no = number;
		
		getNumbersInfo (numbers, size);
		
		size ++;
	}
	
	cout << "File Status: ";
	
	if (!afile.eof ())
		cout << "End of file not hit" << endl << endl;
	else
		cout << "End of file hits" << endl << endl;
	
	afile.close ();
	
	return size;
}	// fileToArray

// Get info from the numbers in the array
void getNumbersInfo (Number numbers [], int size)
{
	numbers [size].oddDigits = 0;
	numbers [size].evenDigits = 0;
	numbers [size].sumDigits = 0;
	numbers [size].noDigits = 0;
	
	int number = numbers [size].no;
	
	while (number > 0)
	{
		// Find number of odd/even digits
		if (number % 2)
			numbers [size].oddDigits ++;
		else
			numbers [size].evenDigits ++;
		
		// Find the sum of digits
		numbers [size].sumDigits += number % 10;
		
		// Find the number of digits
		numbers [size].noDigits ++;
		
		number /= 10;
	}
}	// getNumbersInfo

// Use the array numbers and info and construct
// an outfile.txt printed with the required info
void arrayToFile (ofstream& outfile, char fileName[],
				  Number numbers [], int size)
{
	outfile.open (fileName);
	
	if (!outfile)
	{
		cout << "File Status: Incorrect file, ensure that you have correct file" << endl;
		exit (1);
	}
	
	cout << "No\tOdd\tEven\tSum\tDigit" << endl;
	
	outfile << "No\tOdd\tEven\tSum\tDigit" << endl;
	
	for (int i = 0; i < size; i ++)
	{
		cout << numbers [i].no << "\t"
			 << numbers [i].oddDigits << "\t"
			 << numbers [i].evenDigits << "\t"
			 << numbers [i].sumDigits << "\t"
			 << numbers [i].noDigits << endl;
			 
		outfile << numbers [i].no << "\t"
				<< numbers [i].oddDigits << "\t"
				<< numbers [i].evenDigits << "\t"
				<< numbers [i].sumDigits << "\t"
				<< numbers [i].noDigits << endl;
	}
	
	outfile.close ();
}	// arrayToFile
Last edited on
Explain, what is the length of digits. Give and example, please.
Hi melkiy, something like this: -


No      Odd  Even  Status
1234   2      2        Equal
5673   3      1        Longer
3422   1      3        Shorter


Will it be more easy to do it when we declare the following enum:

enum Length {Equal, Longer, Shorter}; //Newly Added


struct Number
{
int no;
int oddDigits;
int evenDigits;
int sumDigits;
int noDigits;
Length lengthType; //Newly Added
};
Last edited on
Anyone can advise me?
No, I don't think it would help. In fact, I don't think you should have any of those variables at all really...I would just have functions that return the oddDigits, evenDigits, etc, and just have a print() function that does everything (comparing, printing) for you to print the information out.
But I know it can be done. How do I do it?
You could, just make a function that takes a Number as a parameter, then sets lengthType to whatever it needs to be.
Topic archived. No new replies allowed.