Here is the code I wrote this morning and asked about last night when I was a little out of it. Now did I go about this the hard way? I sometimes find myself doing that. Now please excuse it all being in main, I understand my design should have included a class or at least some functions. My while statement on line 39 is what I am really asking about.
Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <algorithm>
struct compare
{
bool operator() (int x, int y){ return (x<y);};
}MyCompare;
int main(int argc, const char * argv[])
{
std::vector<int>num;
std::vector<int>commonNum;
int lottoNum;
std::ifstream infile("textfile.txt"); //opening lotto numbers
while (infile>>lottoNum) { num.push_back(lottoNum); } //pushing numbers from file into vector
for (auto it = num.begin(); it!=num.end(); ++it)
{
for (int compare = 1; compare<=49; ++compare) {
if (compare==*it) {
commonNum.push_back(*it); //pushing common numbers to common Num vector
}
}
}
std::sort(commonNum.begin(), commonNum.end(), MyCompare);//sorting the numbers
for (int i=0; i<commonNum.size(); i++)
{
int count = 1;
size_t limit = commonNum.size();
while (i<limit && commonNum[i]==commonNum[i+1]) //comparing the current number with the next
{
count++;
i++;
}
std::cout<<commonNum[i]<<"\t"<<count<<"\n"; //current number and how many time it occurred
}
infile.close();
return 0;
}