Lotto Numbers

When I turn on the programe, the dropping window says that programe is dissapear because it caused cessation of correct work of the programe.

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <istream>
#include <cstdlib>
using namespace std;
class Loto {
int *m_br;
int m_rezervni_broj;
public:
Loto() {
int *m_br = new int [6];
SetNumber();
}
~Loto() { delete [] m_br;}
void SetNumber(); // postavi brojeve na
// nasumične vrijednosti
int *GetArrayOfNumber() const {return m_br;}; // vrati pokazivač na niz
int GetReserveNumber() const { return m_rezervni_broj;}; // vrati rezervni broj
// const iza funkcije znači da ove dvije funkcije ne mijenjaju
// članske varijable klase
friend ostream& operator << (ostream& s, const Loto& L);
};
void Loto::SetNumber() {
for ( int i=0;i<6;i++) {
m_br[i] = rand() %39;
}
m_rezervni_broj = rand() %39;
}
ostream& operator << (ostream& s, const Loto& L) {
int *H;
H = L.GetArrayOfNumber();
for(int i=0;i<6;i++) {
s << H[i] << ",";
}
s<< (L.GetReserveNumber());
return s;
}
int main()
{ Loto K;
cout << K;
return 0;
}

Please use code tags.

1
2
3
4
Loto() {
  int *m_br = new int [6];
  SetNumber();
}

should just be
1
2
3
4
Loto() {
  m_br = new int [6];
  SetNumber();
}

Don't include the type int * here or you will declare a LOCAL array m_br that will hide the member data of the same name.

Here is your code in tags (and without the language that I can't translate):
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

class Loto
{
   int *m_br;
   int m_rezervni_broj;
public:
   Loto()
   {
      m_br = new int[6];
      SetNumber();
   }
   ~Loto() { delete [] m_br; }
   void SetNumber(); 
   int *GetArrayOfNumber() const { return m_br; };
   int GetReserveNumber() const { return m_rezervni_broj; }
   friend ostream& operator << ( ostream& s, const Loto& L );
};


void Loto::SetNumber()
{
   for ( int i = 0; i < 6; i++ ) m_br[i] = rand() % 39;
   m_rezervni_broj = rand() % 39;
}


ostream& operator << ( ostream& s, const Loto& L )
{
   for ( int i = 0; i < 6; i++ ) s << L.m_br[i] << ",";
   s << L.GetReserveNumber();
   return s;
}


int main()
{
   srand( time( 0 ) );
   Loto K;
   cout << K;
}

34,4,16,36,11,28,21


Topic archived. No new replies allowed.