Sry, glemte:
#include <vector>
#include <algorithm>
Komplet kode:
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
void Generator(unsigned short Antal_Tal, unsigned short Antal_Raekker, unsigned short Top);
int main()
{
char Aktuelt_Lotto;
unsigned short Raekker;
do
{
cout <<"Velkommen til Lotto Generatoren ..! " <<endl <<endl
<<"\t 1. Onsdags Lotto. " <<endl
<<"\t 2. Lørdags Lotto. " <<endl <<endl;
cout <<"Tast tallet for aktuelle lotto service .. ! : ";
cin >>Aktuelt_Lotto;
cout <<"Hvor mange rækker ønskes ..? : ";
cin >>Raekker;
system("cls");
if (Aktuelt_Lotto == '1')
{
unsigned short Tal = 7, Top = 48;
Generator(Tal, Raekker, Top);
}
else if (Aktuelt_Lotto == '2')
{
unsigned short Tal = 8, Top =36;
Generator(Tal, Raekker, Top);
}
}while (Aktuelt_Lotto != '1' && Aktuelt_Lotto !='2');
return 0;
}
void Generator(unsigned short Antal_Tal, unsigned short Antal_Raekker, unsigned short Top)
{
unsigned short Raekke_Taeller = 1, Tal_Taeller = 1, Tal;
vector<unsigned short> vec;
vector<unsigned short>::const_iterator itr;
srand(time(0));
do
{
do
{
Tal = rand()%Top +1;
itr = find(vec.begin(), vec.end(), Tal);
if(itr == vec.end())
{
vec.push_back(Tal);
cout <<setw(4) <<Tal << " ";
Tal_Taeller++;
}
}while(Tal_Taeller < Antal_Tal);
cout <<endl;
Tal_Taeller =1;
Raekke_Taeller++;
vec.clear();
}while(Raekke_Taeller <= Antal_Raekker);
}
Jeg bruger vector som har mange fordele overfor et array, vector finder man i standart template library (STL) og find funktionen finder man i algorithm headeren.
Idéen med mit forslag er at der bliver tjekket om det genererede tal allerede er i vectoren hvis ikke så bliver det skrevet ud.
Link til beskrivelse af vector:
http://www.cplusplus.com/reference/stl/vector/Link til beskrivelse af algoritm, hvor man finder f.eks find:
http://www.cplusplus.com/reference/algorithm/Håber det hjalp.
Mvh
Ossi