#include #include #include using
convert it into file handling.
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
class votes
{
private:
int no_of_people = 0;
string arr[15] = { "Arif ur Rehman", "Waqar Chohan", "Sumaira Kausar", "Momina Moetesum","Muhammad Muzammal" };
int no[15] = { 77,56,55,44,36 };
public:
void work()
{
string name;
char selection;
do
{
cout << "\n Enter The name of Candidate you Like: ";
getline(cin, name);
char press;
for (int i = 0; i < 10; i++)
{
if (name == arr[0])
{
no_of_people = no[0];
}
else if (name == arr[1])
{
no_of_people = no[1];
}
else if (name == arr[2])
{
no_of_people = no[2];
}
else if (name == arr[3])
{
no_of_people = no[3];
}
else if (name == arr[4])
{
no_of_people = no[4];
}
else if (name == arr[5])
{
no_of_people = no[5];
}
if (name == arr[i])
{
cout << "\n The Candidate is in
cout << " Do you want to vote for him as well(y/n): ";
cin >> press;
if (press == 'y')
{
cout << endl;
no_of_people++;
}
else
{
goto jump;
}
cout << "\n\n Now Candidate is voted by " << no_of_people << " people.";
goto jump;
}
}
cout << "\n\n Candidate is not found in database Do you want to add it in database(y/n).";
char press1;
cin >> press1;
if (press1 == 'y')
{
arr[5].append(name);
cout << "\n\n Candidate added in database";
}
else
{
goto jump;
}
jump:
cout << "\n\n Do you want to look for another Candidate(y/n): ";
cin >> selection;
cin.ignore();
} while (selection != 'n');
}
void Total()
{
int total = 0;
for (int i = 0; i < 10; i = i + 1)
{
total = total + no[i];
}
cout << "\n\n The Total Number of People Who Have Alredy Voted : " << total;
}
void file()
{
ofstream file;
file.open("Data.txt");
if (!file)
{
cout << "\n File Not Open...\n\n";
exit(0);
}
else
{
for (int i = 0; i < 6; i = i + 1)
{
file << arr[i] << "," << no[i] << endl;
}
cout << "\n The file has written..";
}
file.close();
}
void display()
{
cout << "\n **";
cout << "\n The Top five Candidates are: " << endl;
for (int i = 0; i < 6; i++)
{
cout << "\n " << arr[i] << "," << no[i] << endl;
}
cout << "\n **";
}
};
int main()
{
votes v1;
v1.work();
v1.display();
v1.Total();
v1.file();
_getch();
system("pause");
}
Step by step
Solved in 3 steps