Creating a recurring number program from a file (Soon to be more)

Howdy guys,

This is the first time ive posted in cplusplus.com, and im quite glad i've gotten this far (Since learning a new program is fkn hard).

The program i'm trying to build:
- A menu (That shows you what you can do in the program)
1 - Input Numbers into a file (ATM supports 6 numbers) >> Eventually i want to be able to input as many numbers i want and it prints into file.

2 - Show the numbers in the file (May disregard this in the code, when the file eventually supports thousands of numbers).

3 - Find recurring numbers (This is where i need the most help... How do i make my code smaller. How do i only get the most recurring number. And am i somewhat on the right path


What else i really want to know is:
- Is my syntax for c++ correct (or nearly there)
- How do i go from my code, to smaller condensed code
- And any other tips/tricks/examples you could give me would be great

CHEERS

here it is:







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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
/*
* Standard Library Include Files
*/
#include <iostream> // Needed for typing/printing in Console
#include <Windows.h> // Not sure if C version... Where to get C++ version?
#include <fstream> // Need to create, open, close files (File System)
#include <string> //

/*
* Prototypes // This lets me place any function wherever i want in the code
*/
bool inputNumbers();
void mainMenu();
void findRecurringNumber();

/*
* Driver Program (Main Function) Every program needs one
*/
void main()
{
	mainMenu();
}

/*
* Functions Start Here
*/
// This particular function is used to clear the cache of the Output/Input executable screen
void clearScreen()
{
	std::cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
	system("CLS");	// Clears screen input and retries login attempt
	std::cout << std::flush;
	std::cin.clear();
	fflush(stdin);
}

// This function gives a menu (which you can choose) of what you want to do
void mainMenu()
{
	clearScreen();

	std::cout << "What would you like to do?\n"
		<< "Press (0), to exit\n"
		<< "Press (1), to open the numbers document\n"
		<< "Press (2), to input 6 more numbers into the document\n"
		<< "Press (3) to find the most recurring numbers in the document\n"
		<< "Your answer: ";

	// Create variable for user input
	int l_userInput;

	std::cin >> l_userInput;

	// Switch case to the case that is needed.
	switch (l_userInput)
	{
	case 0:
	{
		std::cout << "Goodbye!\n";
		exit(0);
	}
	case 2:
	{
		std::cout << "You have chosen to input 6 more numbers into the file\n";
		Sleep(2500);
		inputNumbers();
		break;
	}
	case 3:
	{
		std::cout << "Find Reccuring Number\n";
		Sleep(2500);
		findRecurringNumber();
	}
	}
}


// Input 6 numbers into the document (HOW DO I MAKE IT BETWEEN 1-6), and numbers above 9+ e.g. 32
bool inputNumbers()
{
	clearScreen();	// Clears screen input and retries login attempt

	std::cout << "Please input the last 6 numbers from the Conquer Lotto game: ";

	// Variables to input the first 6 digits in the exe cache
	int a, b, c, d, e, f;

	std::cin >> a;
	std::cin >> b;
	std::cin >> c;
	std::cin >> d;
	std::cin >> e;
	std::cin >> f;

	std::cout << a << ", " << b << ", " << c << ", " << d << ", " << e << ", " << f << "\n";

	std::cout << std::endl;
	std::cin.clear();

	// If this looks correct... Contiue
	char input;
	std::cout << "If this looks correct please type (y), if not type (n): ";
	std::cin >> input;

	if (input == 'y')
	{
		//inputToFile(a, b, c, d, e, f);
		return true;
	}
	else
	{
		inputNumbers();
		return false;
	}
}


// Find recurring numbers function
// How do i shorten this program to make it more readable?
// How do i implement more numbers... E.g. up to 36.
// How do i only print the most reccurred number?
void findRecurringNumber()
{
	clearScreen();	// Clears screen input and retries login attempt

	int x = 0;

	std::cout << "-----------------------------------" << "\n";
	std::cout << "|---> MOST COMMON NUMBERS ARE <---|" << "\n";
	std::cout << "-----------------------------------" << "\n\n";

	int max_countOf1{ 0 }; // This number will increase as getLine(IN File) finds number 1;
	std::string a{ '1' }; // References to find number 1 in file.
	int max_countOf2{ 0 }; // This number will increase as getLine(IN File) finds number 2;
	std::string b{ '2' }; // References to find number 2 in file.
	int max_countOf3{ 0 }; // This number will increase as getLine(IN File) finds number 3;
	std::string c{ '3' }; // References to find number 3 in file.
	int max_countOf4{ 0 }; // This number will increase as getLine(IN File) finds number 4;
	std::string d{ '4' }; // References to find number 4 in file.
	int max_countOf5{ 0 }; // This number will increase as getLine(IN File) finds number 5;
	std::string e{ '5' }; // References to find number 5 in file.
	int max_countOf6{ 0 }; // This number will increase as getLine(IN File) finds number 6;
	std::string f{ '6' }; // References to find number 6 in file.
	int max_countOf7{ 0 }; // This number will increase as getLine(IN File) finds number 7;
	std::string g{ '7' }; // References to find number 7 in file.
	int max_countOf8{ 0 }; // This number will increase as getLine(IN File) finds number 8;
	std::string h{ '8' }; // References to find number 8 in file.
	int max_countOf9{ 0 }; // This number will increase as getLine(IN File) finds number 9;
	std::string i{ '9' }; // References to find number 9 in file.

						  // Handle negative numbers
	if (x < 0)
	{
		x = -x;
	}

	// Initial Setup
	std::string line;
	std::ifstream numbersFile;
	numbersFile.open("numbers.nbr");

	if (numbersFile.is_open())
	{
		while (getline(numbersFile, line))
		{
			if (line == a) // Refer above... If Number 1 is found... ++ to max_countOf1
			{
				max_countOf1++;
			}
			else if (line == b) // Refer above... If Number 2 is found... ++ to max_countOf1
			{
				max_countOf2++;
			}
			else if (line == c) // Refer above... If Number 3 is found... ++ to max_countOf1
			{
				max_countOf3++;
			}
			else if (line == d) // Refer above... If Number 4 is found... ++ to max_countOf1
			{
				max_countOf4++;
			}
			else if (line == e) // Refer above... If Number 5 is found... ++ to max_countOf1
			{
				max_countOf5++;
			}
			else if (line == f) // Refer above... If Number 6 is found... ++ to max_countOf1
			{
				max_countOf6++;
			}
			else if (line == g) // Refer above... If Number 7 is found... ++ to max_countOf1
			{
				max_countOf7++;
			}
			else if (line == h) // Refer above... If Number 8 is found... ++ to max_countOf1
			{
				max_countOf8++;
			}
			else if (line == i) // Refer above... If Number 9 is found... ++ to max_countOf1
			{
				max_countOf9++;
			}
		}

		// These lines Print the numbers 1-9 (How many were found in the file)
		std::cout << "Number of '1's recorded: " << max_countOf1 << "\n";
		std::cout << "Number of '2's recorded: " << max_countOf2 << "\n";
		std::cout << "Number of '3's recorded: " << max_countOf3 << "\n";
		std::cout << "Number of '4's recorded: " << max_countOf4 << "\n";
		std::cout << "Number of '5's recorded: " << max_countOf5 << "\n";
		std::cout << "Number of '6's recorded: " << max_countOf6 << "\n";
		std::cout << "Number of '7's recorded: " << max_countOf7 << "\n";
		std::cout << "Number of '8's recorded: " << max_countOf8 << "\n";
		std::cout << "Number of '9's recorded: " << max_countOf9 << "\n";
	}
}


i had to remove some code (So it may not work) (EDIT: works now, since i edited) because of the max word limit
Last edited on
closed account (48T7M4Gy)
Assuming that xLotto is 6 numbers out of a possible 40, you can remove a massive amount of your repetitive code by using arrays (or <vector>s) and loop through the relevant array to achieve the same.

For instance a single game is an array of 6 integers. int game[6]{0};

To keep the stats use an array of 40 integers int count[40]{0}; with each element being the number of occurrences. For instance the if the number of times 17 occurs is 247 then the count array element would be count[17] = 247

Remember the first element in an array is array[0], so make it easy and have 7 and 41 elements in the arrays and the 0 element never gets used or referred to.

If you must clear the screen just use system("cls") and delete all the other 'stuff'. There are volumes of reasons for not doing this but don't worry about it. Just do it. Or, the best is just don't bother clearing the screen at all.

Cheers for the guidance, i've managed to fix up my last lines of code (where it was printing the numbers) with:

1
2
3
4
5
6
7
		// This line Prints the numbers from 1-9 (How many were found in the file)
		const std::string countString[40]{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

		for (int each=1; each < 10; ++each)
		{
			std::cout << "Number of '" << countString[each] << "'s recorded: " << count[each] << "\n";
		}


However im using public variables... Anyway to not use the public variables? (Is the only way through calling? or can it be done via pointing)

Also now i have to keep deleteing my .exe file before building it.
Anybody know any fixes (I've tried googling it, and tried admin perms, etc still doesn't work)




[EDIT] Also how would i changed this to a for loop?

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
if (numbersFile.is_open())
	{
		while (getline(numbersFile, line))
		{
			if (line == countString[1]) // Refer above... If Number 1 is found... ++ to max_countOf1
			{
				count[1]++;
			}
			else if (line == countString[2]) // Refer above... If Number 2 is found... ++ to max_countOf1
			{
				count[2]++;
			}
			else if (line == countString[3]) // Refer above... If Number 3 is found... ++ to max_countOf1
			{
				count[3]++;
			}
			else if (line == countString[4]) // Refer above... If Number 4 is found... ++ to max_countOf1
			{
				count[4]++;
			}
			else if (line == countString[5]) // Refer above... If Number 5 is found... ++ to max_countOf1
			{
				count[5]++;
			}
			else if (line == countString[6]) // Refer above... If Number 6 is found... ++ to max_countOf1
			{
				count[6]++;
			}
			else if (line == countString[7]) // Refer above... If Number 7 is found... ++ to max_countOf1
			{
				count[7]++;
			}
			else if (line == countString[8]) // Refer above... If Number 8 is found... ++ to max_countOf1
			{
				count[8]++;
			}
			else if (line == countString[9]) // Refer above... If Number 9 is found... ++ to max_countOf1
			{
				count[9]++;
			}
		}


Tried adding into a for loop, while loop, tried printing against multiple strings... Still can't figure a way of it
Last edited on
closed account (48T7M4Gy)
Instead of strings, why not use numbers?

However to take this any further I think you best bet is as follows:
1. Post a clear statement of what your project is all about. If it's an assignment then post a copy of the question.
2. If there is a sample of a data file then please post it - just a sufficient quantity to show what's going on.
3. A sample of the data the user is typing in. So fa it appears the user types in 6 numbers. What are they, show a sample.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
    #include <sstream>

    int data_2[6]{0};
    std::stringstream iss;
    
    std::string line;
    std::cout << "Please enter 6 numbers: ";// e.g. 1 12 3 56 17 9
    std::getline(std::cin, line);
    
    iss.str(line);
    
    int index = 0;
    while(index < 6 && iss >> data_2[index])
        index++;
Topic archived. No new replies allowed.