Lottery Program -still looking for someone to review latest post

Pages: 12
Ok ive finished the coding, but have 1 issue im trying to get around. because i'm calling on the function numGen 2 times for the console display and the file export its generating 2 different outputs. how can i reformat some of my code to get exactly what im doing across but with the same output to the textfile as the console.

It's also outputting to he console the ticket frame perfectly but then a second set of numbers from the ticketFrameFile function calling on numGen again, and if you look in the text file after running the program the ticket frame is there perfectly without the lottery numbers....


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
/* 
   Lottery Ticket Generator
   Objective: To Replicate a NJ pick 6 Lotto ticket with 6 randomly generated 
   numbers.  Program will output ticket to console and to a file called lotterynums.dat
*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <cstdlib>

using namespace std;

void ticketFrameConsole();
void ticketFrameFile();
void numGen();


int main()
{
    
    srand(time(NULL));
    ticketFrameConsole();
    ticketFrameFile();
    
    system("PAUSE");
    
    return 0;
        

}

void numGen()
{
     int counter,number;
     ofstream outFile;
     
     counter=0;
     
     while (counter<6)
     {
           number=rand()%49+1;
           cout << setw(5) << number;
           outFile << setw(5) << number;
           counter++;
     }
           
     return;
     
}

void ticketFrameConsole()
{
     
     cout << endl;
     cout << setw(27) << "New Jersey Lottery\n";
     cout << setw(22) << "Pick-6\n\n";
     cout << setw(27) << "****************\n\n";
     cout << "A.  "; 
     numGen();
     cout << endl << endl;
     cout << setw(27) << "****************\n\n";
     cout << setw(27) << "1 draw(s)    $1.00" << endl << endl;
     cout << setw(20) << "Ticket#:  " << rand()%5000000+1;
     cout << endl << endl << endl;
     return; 
     
}
void ticketFrameFile()
{
     ofstream outFile;
     outFile.open("lotterynums.dat");

     outFile << endl;
     outFile << setw(27) << "New Jersey Lottery\n";
     outFile << setw(22) << "Pick-6\n\n";
     outFile << setw(27) << "****************\n\n";
     outFile << "A.  "; 
     numGen();
     outFile << endl << endl;
     outFile << setw(27) << "****************\n\n";
     outFile << setw(27) << "1 draw(s)    $1.00" << endl << endl;
     outFile << setw(20) << "Ticket#:  " << rand()%5000000+1;
     outFile << endl << endl << endl;

     outFile.close();
     
     return;
}     
The outfile within numGen is not the same filestream. You are trying to write data to a file stream that is not connected to the file. You'll have to pass the file stream to numGen as a reference. Also when you call ticketFrameConsole() you are also outputting data to cout and the file stream which you never opened any file with. Each file stream object is a completely different object in your program. It is hard for me to tell you how to fix this since you are so limited in what you are allowed to do. I would make numGen a template function personally.
I changed your program in some places. I'd like to write it from the beginning myself, because these version gets more complicated, because of your mistakes with calling numGen twice. On the other hand, I don't want to write whole program for you, because it'd be my way of thinking. This is a little crappy for me :P

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
/* 
   Lottery Ticket Generator
   Objective: To Replicate a NJ pick 6 Lotto ticket with 6 randomly generated 
   numbers.  Program will output ticket to console and to a file called lotterynums.dat
*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <cstdlib>

using namespace std;

int ticketFrameConsole();
void ticketFrameFile(int,int);
void numGen();


int main()
{
    numGen();
    
    system("PAUSE");
    
    return 0;
}

void numGen()
{
     int counter=0,number,ticket;

	 cout << setw(27) << "New Jersey Lottery\n";
	 cout << setw(22) << "Pick-6\n\n";
	 cout << setw(27) << "****************\n\n";
	 cout << "A.  ";
     
     while (counter<6)
     {
           number=rand()%49+1;
           cout << setw(5) << number;
		   ticketFrameFile(number,counter);
           counter++;
     } 
	 ticket=ticketFrameConsole();
	 ticketFrameFile(ticket,counter);
	 cout << endl << endl;
	 cout << setw(27) << "****************\n\n";
	 cout << setw(27) << "1 draw(s)    $1.00" << endl << endl;
	 cout << setw(20) << "Ticket#:  " << ticket;
	 cout << endl << endl << endl;
}

int ticketFrameConsole()
{
	 int ticket;
	 ticket=rand()%5000000+1;

	 return ticket;
}
void ticketFrameFile(int number,int c)
{
	ofstream outFile;

	 if(c==0)
	 {
		outFile.open("lotterynums.txt");
		outFile << endl;
		outFile << setw(27) << "New Jersey Lottery\n";
		outFile << setw(22) << "Pick-6\n\n";
		outFile << setw(27) << "****************\n\n";
		outFile << "A.  "; 
		outFile.close();
	 }

	 if(c<6)
	 {
		outFile.open("lotterynums.txt",ios::app);
		outFile<< setw(5) << number;
	 }

	 if(c==6)
	 {
		outFile.open("lotterynums.txt",ios::app);
		outFile << endl << endl;
		outFile << setw(27) << "****************\n\n";
		outFile << setw(27) << "1 draw(s)    $1.00" << endl << endl;
		outFile << setw(20) << "Ticket#:  " << number;
		outFile << endl << endl << endl;
		outFile.close();
	 }
}   


If you're going to write programs in the future, at the beginning you should think about sectioning them into functions more clever. Every function should have its own task. One to print sth, another to make loops, etc.
Ok. I think we are looking good now. It seems to work as planned. Anything else jump off the page that is out of the ordinary? Thanks for your help Kempofighter and mtweeman


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
/* 
   Lottery Ticket Generator
   Objective: To Replicate a NJ pick 6 Lotto ticket with 6 randomly generated 
   numbers.  Program will output ticket to console and to a file called lotterynums.dat
*/

#include <iostream>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <cstdlib>

using namespace std;

//Function Prototypes

void numAndTicketGen();


int main()
{
    
    srand(time(NULL));
    numAndTicketGen();
    
    system("PAUSE");
    
    return 0;
        

}

//Function Below Opens a file called lotterynums.dat, to write a lottery ticket to. 
//This function also outputs the same ticket to the console
void numAndTicketGen()
{
     int counter,number,ticketnum;
     
     
     counter=0;
     ticketnum=rand()%5000000+1;
     
     ofstream outFile;
     outFile.open("lotterynums.dat");

//beginning of lottery ticket template for output to file
     outFile << endl;
     outFile << setw(27) << "New Jersey Lottery\n";
     outFile << setw(22) << "Pick-6\n\n";
     outFile << setw(27) << "****************\n\n";
     outFile << "A.  "; 

//beginning of lottery ticket template for output to console     
     cout << endl;
     cout << setw(27) << "New Jersey Lottery\n";
     cout << setw(22) << "Pick-6\n\n";
     cout << setw(27) << "****************\n\n";
     cout << "A.  "; 

//This while loop wil generate the 6 randoms numbers 1-49     
     while (counter<6)
     {
           number=rand()%49+1;
           cout << setw(5) << number;
           outFile << setw(5) << number;
           counter++;
     }

//end of lottery ticket template for output to console          
     cout << endl << endl;
     cout << setw(27) << "****************\n\n";
     cout << setw(27) << "1 draw(s)    $1.00" << endl << endl;
     cout << setw(20) << "Ticket#:  " << ticketnum;
     cout << endl << endl << endl;

//end of lottery ticket template for output to file     
     outFile << endl << endl;
     outFile << setw(27) << "****************\n\n";
     outFile << setw(27) << "1 draw(s)    $1.00" << endl << endl;
     outFile << setw(20) << "Ticket#:  " << ticketnum;
     outFile << endl << endl << endl;

     outFile.close();
     
     return;
         
}
@mtweeman

Looks like we posted new programs pretty much at the same time. I see where you made some changes, i also made some changes of myself to simplify what i had. Hows my new way look before i consider going back and looking further at some of your ideas...?
@majesticmixers,

if you have program in this shape, think about sharing it into pieces yourself, it looks good for me. make function numFrameFile which will save data in file, numTicket which will generate ticket random number, numNumbers which will generate lottery numbers and one or two functions to print some info on the screen and into file, ex. "New Jersey lottery" as you wrote.
Topic archived. No new replies allowed.
Pages: 12