1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <iostream>
//#include <limits>//for cin.ignore() limits;
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
bool exit = false;
while (!exit)
{
cout << "Choose an Option \n1. Highest Vote \n2. Testing \n3 Exit \n";
int menu;
cin>>menu;
/* if(!(cin >> menu))//some sample code to validate user input, must use this/similar in 'real' programs
{
cout<<"Wrong entry, try again \n";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
continue;
}*/
switch (menu)
{//braces required for case 1 to scope local variables declared within this case;
case 1:
{
ifstream file1("F:\\test.txt");
bool match_name = false;
if(file1)
{
cout << "Please enter voter name: \n";
string inputName;
cin >> inputName;
string line;
while(getline(file1, line))
{
if(line == inputName)
{
match_name = true;
break;
}
}
}
else
{
cout<<"Name file cannot be opened \n";
}
if(match_name == false)
{
cout << "No matching name.\n";
}
else
{
ifstream file2("F:\\test1.txt");//declare variables when & where needed ;
cout << "\nName found, now enter voting Date (dd/mm/yyyy): ";
string inputDate;
cin>>inputDate;
vector<pair<string, double>> v;
if(file2)
{
string line;
while(getline(file2, line))
{
istringstream stream(line);
string date, time;
double price , qty;
stream >> date >> time >> price >> qty;
if(date == inputDate)
{
v.emplace_back(time, price);
}
}
}
if(v.empty())
{
cout<<"No matching date for the name \n";
}
else
{
sort(v.begin(), v.end(),[](const pair<string, double>&left, const pair<string, double>&right)
{return left.second > right.second;});
for (auto& elem : v)
{
cout<<elem.first<<": "<<elem.second<<"\n";
}
}
}
}
break;
case 2:
cout << "Test 2" << endl;
break;
case 3:
exit = true;
break;
default:
cout << "Error: Invalid input. \n";
break;
}
}
}
|