-3

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));
}

}
jorge
  • 1
  • 2
  • 1
    Describe what the problem is and please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). "I don't know where to start" is too broad. – Ted Lyngmo Dec 14 '18 at 16:58
  • 1
    Also, please ask a question. [Stack Overfow is a question and answer site](https://stackoverflow.com/tour). – Drew Dormann Dec 14 '18 at 16:58
  • Your code is mixed up. Do things in the following order, 1 open the file, 2, read the file contents, 3 close the file, 4 pick the random number. In particular the code above tries to pick the random number *inside the reading while loop*, that makes no sense at all. Read the numbers first, then pick one randomly, don't try to do both at once. There are various other mistakes in the code but at least by spltting things into separate pieces you can test each piece in turn. – john Dec 14 '18 at 17:11
  • See also: [Why is eof considered wrong in a `while` statement](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Thomas Matthews Dec 14 '18 at 17:20
  • The [std::getline](https://en.cppreference.com/w/cpp/string/basic_string/getline) does not read integers, you'll need a `std::string` for that. – Thomas Matthews Dec 14 '18 at 17:21

1 Answers1

3

Preferred Method for Reading Many Integers

Here is a code fragment for reading in multiple integers, space or newline separated, and placing into a database:

int value;
std::vector<int> database;
while (inFile >> value)
{
  database.push_back(value);
}

You can treat std::vector as an array, such as value = database[3];. The std::vector works well with std::sort also.

Limiting Input Quantity

You can add some extra code to limit the quantity of numbers read:

const size_t MAXIMUM_INTEGERS = 10;
size_t quantity = 0;
int value;
std::vector<int> database;
while ((quantity < MAXIMUM_INTEGERS) && (inFile >> value))
{
  database.push_back(value);
  ++quantity;
}

Reading Into an Array

If you must use an array (because of assignment restrictions), the code is similar to the above limiting code:

const size_t MAXIMUM_INTEGERS = 10;
size_t quantity = 0;
int value;
int array[MAXIMUM_INTEGERS];;
while ((quantity < MAXIMUM_INTEGERS) && (inFile >> value))
{
  array[quantity] = value;
  ++quantity;
}

Reading Strings

Reading strings instead of integers uses the same method / pattern:

std::string number_as_text;
size_t quantity_read = 0;
std::vector<std::string> database;
while (std::getline(inFile, number_as_text))
{
  database.push_back(number_as_text);
  ++quantity_read;
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154