Ye Olde General C++ Discussion/Advice Thread

To pause a console app, just add
#include <conio.h>
and in the code before your final return
_getch();

Don't get too hung up on console apps, since in "the real world" these days everything with a frontend is usually GUI based.

Damn, you really got my hopes up, but nope! This program has exactly the same crashing issue, although I replaced what I thought was causing the issue with your snippet.

This is *really* weird, because everything in the program is meant to have finished by the time you get to that end point, yet somehow closing it is re-activating something in order to make it crash.

Any clues? Here's the updated code with the suggestion above. It compiles and runs with no problem (apart from the crashing at the end lol):

Code:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <conio.h>

using namespace std;

int main() 
{ // START OF MAIN PROGRAM

	cout << "WELCOME TO THE SUPER-HONEST LOTTERY GENERATOR" << endl;
	cout << "V2.0 February 2015" << endl;
	cout << endl;
	
	int lottoNums[6][1];
	int totalDraws = 1;
	string userRequest;

	cout << "How many lines of lottery numbers do you want? (enter in digits)" << endl;
	getline(cin, userRequest);
	int linesNeeded = atoi(userRequest.c_str());
	cout << endl;

	cout << "Here we go, for " << linesNeeded << " lines. Good luck!" << endl;
	cout << endl;

	srand(time(NULL));
	
	while (linesNeeded > 0) {  // MAIN WHILE LOOP. RUNS WHILE LINES ARE STILL NEEDED.

		int ballNumber = 1; // Tracks the ball number
		int drawNumberCounter; //Counts down through array after synchronising with number of draws
		int newBall = 0; //Holds the new random number
		bool noDuplicates = true;
		cout << "Lottery set " << totalDraws << endl;
		cout << endl;
	
		for (ballNumber; ballNumber < 7; ballNumber++)  { // START OF DRAWING SIX NUMBERS
			
			drawNumberCounter = ballNumber;
			newBall = (rand() % 48) + 1;

			while (drawNumberCounter >= 0) { //START OF UNIQUE NUMBER CHECKER

				
				if (lottoNums[(drawNumberCounter - 1)][totalDraws] != newBall) {
						noDuplicates = true;
						lottoNums[(ballNumber - 1)][totalDraws] = newBall;
						drawNumberCounter--;
				} else {
				cout << "CHECK: Number drawn is a duplicate" << endl;
				cout << "Duplicate number: " << newBall << endl;
				ballNumber--;
				noDuplicates = false;
				cout << endl;
				break;
				}
	
			} // END OF UNIQUE NUMBER CHECKER

			if (noDuplicates) { // ACTION ONCE UNIQUE NUMBER IS SAVED
				cout << "Lucky ball number " << ballNumber << "." << endl;
				cout << "Ball result: " << lottoNums[(ballNumber-1)][totalDraws] << endl;
				cout << endl;
				noDuplicates = true; //resets Duplicate status

			} // END OF ACTION ONCE UNIQUE NUMBER IS SAVED
			
		}  // END OF DRAWING SIX NUMBERS
		
		

		// FINAL ACTIONS IN WHILE LOOP, DECREASING LINES STILL NEEDED AND INCREASING TOTAL DRAW NUMBER
		linesNeeded--;
		totalDraws++;
		cout << endl;

		cout << "Your final lucky numbers for Line " << totalDraws-1 << " are: | ";
		for (int resetArray = 0; resetArray < 6; resetArray++)  { //Prints out and resets array after each draw
			cout << lottoNums[resetArray-1][totalDraws] << " | ";
			lottoNums[resetArray-1][totalDraws] = 0;
		}
		cout << endl;
		cout << endl;

	} //END OF MAIN WHILE LOOP

	cout << endl;
	
	cout << "Press Enter again to exit." << endl;
	_getch();

	return 0;

} // END OF MAIN PROGRAM

EDIT: For the record, the debugger gave me this error again after the program crashed:
"Unhandled exception at 0x0084241D in HelloWorld.exe: Stack cookie instrumentation code detected a stack-based buffer overrun."
 
Last edited:
You're writing past the end of lottNums ( it looks like you will access [5][1] ) - try make the array [6][2].

Edit: actually it looks like you will blow past 1 for the second operand eventually too.

Damn, you really got my hopes up, but nope! This program has exactly the same crashing issue, although I replaced what I thought was causing the issue with your snippet.

This is *really* weird, because everything in the program is meant to have finished by the time you get to that end point, yet somehow closing it is re-activating something in order to make it crash.

Any clues? Here's the updated code with the suggestion above. It compiles and runs with no problem (apart from the crashing at the end lol):

Code:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <conio.h>

using namespace std;

int main() 
{ // START OF MAIN PROGRAM

	cout << "WELCOME TO THE SUPER-HONEST LOTTERY GENERATOR" << endl;
	cout << "V2.0 February 2015" << endl;
	cout << endl;
	
	int lottoNums[6][1];
	int totalDraws = 1;
	string userRequest;

	cout << "How many lines of lottery numbers do you want? (enter in digits)" << endl;
	getline(cin, userRequest);
	int linesNeeded = atoi(userRequest.c_str());
	cout << endl;

	cout << "Here we go, for " << linesNeeded << " lines. Good luck!" << endl;
	cout << endl;

	srand(time(NULL));
	
	while (linesNeeded > 0) {  // MAIN WHILE LOOP. RUNS WHILE LINES ARE STILL NEEDED.

		int ballNumber = 1; // Tracks the ball number
		int drawNumberCounter; //Counts down through array after synchronising with number of draws
		int newBall = 0; //Holds the new random number
		bool noDuplicates = true;
		cout << "Lottery set " << totalDraws << endl;
		cout << endl;
	
		for (ballNumber; ballNumber < 7; ballNumber++)  { // START OF DRAWING SIX NUMBERS
			
			drawNumberCounter = ballNumber;
			newBall = (rand() % 48) + 1;

			while (drawNumberCounter >= 0) { //START OF UNIQUE NUMBER CHECKER

				
				if (lottoNums[(drawNumberCounter - 1)][totalDraws] != newBall) {
						noDuplicates = true;
						lottoNums[(ballNumber - 1)][totalDraws] = newBall;
						drawNumberCounter--;
				} else {
				cout << "CHECK: Number drawn is a duplicate" << endl;
				cout << "Duplicate number: " << newBall << endl;
				ballNumber--;
				noDuplicates = false;
				cout << endl;
				break;
				}
	
			} // END OF UNIQUE NUMBER CHECKER

			if (noDuplicates) { // ACTION ONCE UNIQUE NUMBER IS SAVED
				cout << "Lucky ball number " << ballNumber << "." << endl;
				cout << "Ball result: " << lottoNums[(ballNumber-1)][totalDraws] << endl;
				cout << endl;
				noDuplicates = true; //resets Duplicate status

			} // END OF ACTION ONCE UNIQUE NUMBER IS SAVED
			
		}  // END OF DRAWING SIX NUMBERS
		
		

		// FINAL ACTIONS IN WHILE LOOP, DECREASING LINES STILL NEEDED AND INCREASING TOTAL DRAW NUMBER
		linesNeeded--;
		totalDraws++;
		cout << endl;

		cout << "Your final lucky numbers for Line " << totalDraws-1 << " are: | ";
		for (int resetArray = 0; resetArray < 6; resetArray++)  { //Prints out and resets array after each draw
			cout << lottoNums[resetArray-1][totalDraws] << " | ";
			lottoNums[resetArray-1][totalDraws] = 0;
		}
		cout << endl;
		cout << endl;

	} //END OF MAIN WHILE LOOP

	cout << endl;
	
	cout << "Press Enter again to exit." << endl;
	_getch();

	return 0;

} // END OF MAIN PROGRAM

EDIT: For the record, the debugger gave me this error again after the program crashed:
"Unhandled exception at 0x0084241D in HelloWorld.exe: Stack cookie instrumentation code detected a stack-based buffer overrun."
 
Last edited:
You're writing past the end of lottNums ( it looks like you will access [5][1] ) - try make the array [6][2].

Edit: actually it looks like you will blow past 1 for the second operand eventually too.

You're onto something here cguy ... I played around with the second box of the array, and discovered some interesting symptoms for my program:
Current:
Code:
int lottoNums[6][1];
For referencing below:
Code:
into lottoNumbs[a][b];
1: If the number in the box b < the number of draws the user selects (userRequest), the program crashes!
2: I then tried upping the number in box b to 20, and although the program now closes without crashing, it does NOT print out the results of the array as expected
E.g. typical result:
code cap.PNG
3: The only way to return the code back to the expected behaviour (printing out the 'correct' draw results) was to revert back to [6][1] in the array. Of course, the crashing at the end returns.

Conclusion: I'm using the array in a way that produces the correct results I need, but in a way that breaks it and causes a buffer over-run, preventing the program from closing.

Explanation: I'm willing to admit that I have a very crude understanding of multidimensional arrays, and that has lead to this fault. The *reason* I've chosen [6][1] is because I figured that for every new lottery draw of six numbers, I'd need one unique set of six random numbers, and another one integer that records the draw number (e.g. the 1st draw, the 5th draw etc). That enables me (as it does in the program) to print out the results of each draw in a simple if loop.

So why is this happening? Am I requiring more memory than an integer multidimensional array can give me, or am I causing the whole thing to loop into infinity somewhere with a bad array reference in my while loops?

I still don't understand why a broken system would return expected results though ... that's the most dangerous kind of 'broken' in programming, I guess!
 
Drop the idea of a multiple dimensional array completely, for your purpose it is completely irrelevant and understanding when to use it is just as important as knowing how, many times I have seen bug after bug from fixing a bug leading to a bug because someone over complicated code.

int lottoNums[6];

lottoNums[0]....lottoNums[5]

Those are your index's, 0-5. To iterate it you start a loop as such

For (int i = 0; i < 6; ++i)
{
int pewpew = lottoNums;
}

Bonus points for having a constant for your array size.

Get your program working properly like this and I will give you a valid multidimensional array excercise.
 
Drop the idea of a multiple dimensional array completely, for your purpose it is completely irrelevant and understanding when to use it is just as important as knowing how, many times I have seen bug after bug from fixing a bug leading to a bug because someone over complicated code.

int lottoNums[6];

lottoNums[0]....lottoNums[5]

Those are your index's, 0-5. To iterate it you start a loop as such

For (int i = 0; i < 6; ++i)
{
int pewpew = lottoNums;
}

Bonus points for having a constant for your array size.

Get your program working properly like this and I will give you a valid multidimensional array excercise.


I deliberately didn't want to do that, because at the end of the program I'd have no way of recalling what all the numbers drawn were (because they'd have been printed out once and over-written for the next set).

My goal was to have a system that would populate an array, and at the end of it I'd have the option to use that info however I wanted to. Your method won't get me there, so there's no point, is there?
 
realbigdreamer: I see that you deleted your spoiler. :) Without giving it away, thanks - I learned something new.

Glad you saw it while I had it up. I realised that I might have been too trigger-happy to give the answer so quickly, so I deleted the post.
 
To simplify things create 2 methods.

Method 1 will generate random numbers and insert them into the lottNums array. Method 2 will loop through the lottNums array and output them. Method 2 can later be modified to output to a file and a third method can read them if you wish. Make sure lottNums array is global.

Both method 1 and 2 will use the same for loop syntax.

Once you have this basic one working, you can learn to modify the code so that lottNums is not global but is passed into methods as parameters.

It's easiest to get small little bits working and then build on it. That way when something goes wrong, you know it must be your last code changes that messed it up. As tempting as it is dont try to build the final version from the start.
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X