so i've tried and tried and tried and changed my code about ten times in the last hour, its getting really frustrating, so i decided i'll sign up and ask for help =( (feel like i failed myself) help please, here's the code:
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <iostream>
int main()
{
char name[15];
char position[10];
char password[7];
char password1[]="z3xn4ct";
char lot;
int x;
int y;
int n;
int counter;
int aspace[51];
usingnamespace std;
bool check;
time_t seconds;
time(&seconds);
srand((unsignedint) seconds);
cout << rand()% 50 + 1 << endl;
printf("\tWelcome TO P-SAS!\n NixGen's Parking Space Allotment Software\n");
printf("Please Enter Name and Work Position\n");
scanf("%s%s",name ,position);
for (counter=0; counter <3; counter++) {
printf("Please Enter Password\n");
scanf("%s",password);
if (strcmp(password,password1)==0) //verify password
{
printf("Password Accepted!\n");
break;
}
else {
printf("Incorrect Pass!\n");
counter = 0;
}
}
// for ( x = 0; x < 10; x++ ) {
//aspace[x] = 0; /* Set each element to a value */
// }
printf( "Please Press the letter corresponding with the lot in which you wish to dock a vehicle:\n""Lot A - Press A Lot B - Press B Lot C - Press C\n" );
for (y = 0; y < 50; y++) {
do
{
n=rand() % 50 + 1;
check=true;
for ( x = 0; x < y; x++ )
if (n == aspace[y]) {
check=false; //set check to false
break; //no need to check the other elements of value[]
}
} while (!check); //loop until new, unique number is found
aspace[y]= n;
printf("%d",aspace[y]);
printf( "\n" );
}
if (x==50) {
printf("Lot %s has reached its maximum capacity",lot);
}
getch();
}
[code]
Hi. If your goal is to get random numbers without repeating numbers then you should consider too things: 1) how big is the space between minimum and maximum number the generator can give you; 2) how many of the numbers in the range are you going to use.
In case you are going to use, I would say, less than half of the numbers then you could store all generated numbers in some STL container/array and check for the presence of a new generated number, if a new generated number exists then you will generate a new one, otherwise you will store and use it.
In case the space between minimum and maximum number you can get isn't very big then you could store all possibilities in a STL container/array and get numbers from it and remove the number after you have got it.
First approach uses less memory, the second one uses less time.