Can anyone help me with my code, as described below, i need to make a function called getRandomCode to take 10 codes from a text file, read them and then randomly select one as the secret code. I started but im stuck a need some help moving forward
A function (getRandomCode) that, takes as an input a four-element array of integers (code). The function will then proceed to open the file codes.txt and try to read in at most 10 four digit codes. If the file could not be opened, the function should return false. Otherwise, the function proceeds as follows: – Read each line of the file and place it in an array (I recommend an array of 10 strings). – Once the codes have been read, you should seed the random number generator (srand(time(0))) and use rand() to select a random number between 0 and the number of codes you read. Hint: think about how your file processing loop will keep track of the number of codes read.
– Once you have selected a random code, you must copy the digits in to the cor- responding elements of the four-element array code. To do this you will need to
convert a character to a number. For example, since every character is one digit we just subtract ’0’ from the character in position zero of the selected codes string, and assign the result to code[0]. – Once the code array has been populated, we return true indicating success. You may safely assume that the file is formatted in such a way that there is exactly one code per line. You may not, however, assume there are exactly 10 codes in the file.
This is what i have so far :
int getRandomCode(int code[4])
{
ifstream inFile;
inFile.open("codes.txt");
int getCode[10];
int a = 0;
if(!inFile){
cout<<"Error opening output file"<< endl;
return -1;
}
while(!inFile.eof())
{
getline(inFile, getCode[a], '');
srand(time(0));
int randomInt = (rand(getCode));
}
}