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 106 107 108 109 110 111 112 113 114 115 116 117 118
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void mainmenu(string name[], int numberofvotes[], double percentage[], int arraysize);
int readdata(string[], int[], double[], int);
void display(string[], int[], double[], int);
int main()
{
string name[4] = { "" };
int numberofvotes[4] = { 0 };
double percentage[4] = { 0.0 };
int arraysize = 4;
mainmenu(name, numberofvotes, percentage, arraysize);
return 0;
}
void mainmenu(string name[], int numberofvotes[], double percentage[], int arraysize)
{
int choice = int();
char cont = 'y';
while (cont == 'y' || cont == 'Y')
{
system("cls");
cout << "\t\tMain Menu" << endl;
cout << "\t\t___________" << endl;
cout << endl << endl << endl;
cout << "\t\t1. Read Candidate Data" << endl;
cout << "\t\t2. Display Results" << endl;
cout << "\t\t3. Exit Program" << endl;
cout << "\n\n\n\n\n\t\tPlease choose an option: ";
cin >> choice;
if (cin.fail())
{
cout << "Invalid selection" << endl;
cin.clear();
cin.ignore(1000, '\n');
}
else
{
if (choice == 1)
{
arraysize = readdata(name, numberofvotes, percentage, arraysize);
}
else if (choice == 2)
{
display(name, numberofvotes, percentage, arraysize);
}
else if (choice == 3)
{
cout << "Goodbye" << endl;
exit(1);
}
else
{
cout << "Selection not valid, please try again." << endl;
}
}
cout << "\nWould you like to continue? Y/N";
cin >> cont;
}
}
int readdata(string name[], int numberofvotes[], double percentage[], int arraysize)
{
ifstream in;
in.open("h:\\results.rtf"); //working on a mac(tried txt on pc, same prob
char char1 = char();
int i = 0;
system("cls");
cout << endl;
cout << fixed << setprecision(2);
while (!in.eof())
{
in >> name[i] >> numberofvotes[i] >> percentage[i];
cout << setw(10) << left << name[i] << setw(15) << left << numberofvotes[i] << setw(15) << left << percentage[i];
cout << endl;
++i;
}
arraysize = 4;
return arraysize;
}
void display(string name[], int numberofvotes[], double percentage[], int arraysize)
{
system("cls");
for (int i = 0; i < arraysize; i++)
{
cout << "Name: " << name[i] << endl;
cout << "Number of Votes: " << numberofvotes[i] << endl;
cout << "Percentage of Votes: " << percentage[i] << endl;
cout << "==============================" << endl;
}
}
|