-2

I need to write a program in C++ that reads numbers from a file and calculates

  1. The Ten most common Regular Number
  2. The Ten least common Regular Number
  3. Frequency of the regular lottery numbers
  4. Frequency of the powerball lottery numbers

I managed to find a way to open the file and display it in my program. I stored all the numbers inside the char c but I have to find a way to store the first 5 numbers in a vector or array to look for the least most common and least most regular numbers.

I don't know how to store the first five numbers from the whole list in one array or vector and how to store the fifth column of numbers to calculate them.

This is what I have so far.

using namespace std;

int main() {

ifstream MyFile("LotteryNumbers.txt");
char ch;

while(!MyFile.eof())
{
    MyFile.get(ch);
    cout << ch;
}
MyFile.close();

return 0;
}
user4581301
  • 29,019
  • 5
  • 26
  • 45
  • That's not a [mcve] - you are, at a minimum, lacking some includes. Just saying. – Jesper Juhl Apr 17 '20 at 18:32
  • 1
    Helpful reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Apr 17 '20 at 18:34
  • When `MyFile.get(ch);` hits the EOF, the `cout << ch;` will be the previously read character a second time. – Eljay Apr 17 '20 at 18:34
  • Regarding *in a vector or array to look for the least most common and least most regular numbers.* If you can use it for this assignment, a `std::map` makes frequency counting very, very easy. – user4581301 Apr 17 '20 at 18:36
  • 3
    Recommendation: Start small. Pick one of those tasks and attempt to solve it. If it's still too big, break it down into smaller sub-tasks and solve the sub-tasks and assemble the results to solve the task. – user4581301 Apr 17 '20 at 18:39
  • 4
    Why are you reading numbers as single characters? – BessieTheCookie Apr 17 '20 at 18:46
  • std::map could be used to help count the frequency of a number in the file. I assume the file has more the 5 total numbers. In powerball all numbers appear at most 1 time in a single draw. Isn't there 6 numbers? – drescherjm Apr 17 '20 at 18:48
  • How are the numbers formatted, is it a comma separated file? If so, there are loads of off the shelf examples for this! – Owl Apr 17 '20 at 18:49

1 Answers1

1

Since you need to perform more actions with the numbers, I recommend you read them into a vector:

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

You can treat the vector with array notation, e.g. [], or use the at() member function. The at() member function is recommend when developing because it will return an error if the index is out of bounds.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144