Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

To create a Voter ID application with CRUD (Create, Read, Update, Delete)

operations using C++, you can follow the steps below:

Step 1: Define a struct to store the voter details like name, age, address, etc.

c
Copy code
struct Voter {
string name;
int age;
string address;
string voter_id;
};
Step 2: Create a class named VoterDatabase to perform CRUD operations on the voter
details.

csharp
Copy code
class VoterDatabase {
public:
void createVoter();
void readVoter();
void updateVoter();
void deleteVoter();
private:
vector<Voter> voters;
};
Step 3: Implement the createVoter() function to add a new voter to the database.

c
Copy code
void VoterDatabase::createVoter() {
Voter voter;
cout << "Enter name: ";
getline(cin, voter.name);
cout << "Enter age: ";
cin >> voter.age;
cin.ignore();
cout << "Enter address: ";
getline(cin, voter.address);
voter.voter_id = generateVoterId();
voters.push_back(voter);
cout << "Voter added successfully.\n";
}
Step 4: Implement the readVoter() function to display the details of a specific
voter based on their voter ID.

c
Copy code
void VoterDatabase::readVoter() {
string voter_id;
cout << "Enter voter ID: ";
cin >> voter_id;
for (Voter voter : voters) {
if (voter.voter_id == voter_id) {
cout << "Name: " << voter.name << endl;
cout << "Age: " << voter.age << endl;
cout << "Address: " << voter.address << endl;
cout << "Voter ID: " << voter.voter_id << endl;
return;
}
}
cout << "Voter not found.\n";
}
Step 5: Implement the updateVoter() function to update the details of a specific
voter based on their voter ID.

c
Copy code
void VoterDatabase::updateVoter() {
string voter_id;
cout << "Enter voter ID: ";
cin >> voter_id;
for (int i = 0; i < voters.size(); i++) {
if (voters[i].voter_id == voter_id) {
Voter voter;
cout << "Enter new name: ";
getline(cin, voter.name);
cout << "Enter new age: ";
cin >> voter.age;
cin.ignore();
cout << "Enter new address: ";
getline(cin, voter.address);
voter.voter_id = voter_id;
voters[i] = voter;
cout << "Voter updated successfully.\n";
return;
}
}
cout << "Voter not found.\n";
}
Step 6: Implement the deleteVoter() function to delete a specific voter based on
their voter ID.

c
Copy code
void VoterDatabase::deleteVoter() {
string voter_id;
cout << "Enter voter ID: ";
cin >> voter_id;
for (int i = 0; i < voters.size(); i++) {
if (voters[i].voter_id == voter_id) {
voters.erase(voters.begin() + i);
cout << "Voter deleted successfully.\n";
return;
}
}
cout << "Voter not found.\n";
}
Step 7: Implement a function to generate a unique voter ID for each new voter.

csharp
Copy code
string generateVoterId() {
static int id = 0;
id++;

You might also like