Jun 28, 2015 at 6:52am UTC
If after first check broj will be rerolled and happens to be the same number as first array element, it will not be cchecked, as your loop skips first element when you restart it.
Jun 28, 2015 at 8:56am UTC
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
int broj;
int polje[]={0,0,0,0,0,0,0,0,0,0};
char answer;
do{
for(int a=0;a<=9;a++)
{
cout<<"Unesite broj "<<endl;
cin>>broj;//broj=rand()%15;
for(int i=0;i<=9;i++)
{
if(polje[i]==broj)
{
cout<<"Unesite broj drugi "<<i<<endl;
cin>>broj;//broj=rand()%15;
if(i==0)
i-=1;
else i=0;
continue;
}
}
polje[a]=broj;
}
cout<<"Here comes numbers "<<endl;
for(int i=0;i<10;i++)
cout<<polje[i]<<endl;
cout<<endl<<"Play again? "<<endl;
cin>>answer;
}while(answer=='y'||answer=='Y');
system("PAUSE");
return 0;
}
Jun 28, 2015 at 9:18am UTC
thanks, Here is the same, little different code I do not know where is a bug, can you explain me please? I tried to use debugger but I cant find where is error?
Jun 28, 2015 at 2:21pm UTC
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
int number;
int polje[]={0,0,0,0,0,0,0,0,0,0};
int same_number=0;
int written_to_array=10;
int a=0;
while(written_to_array>0)
{
same_number=0;
number=(rand()%15)+1;
for(int i=0;i<=9;i++)
if(polje[i]==number)
same_number=1;
if(same_number!=1){
polje[a]=number;
a++;
written_to_array-=1;
}
}
cout<<"Numbers"<<endl;
for(int i=0;i<10;i++)
cout<<polje[i]<<endl;
system("PAUSE");
return 0;
}
Jun 28, 2015 at 3:03pm UTC
Yes. That is better. And it does not seems to have errors.