Foxhound5366
Honorary Master
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: