Back to Code List
c++

Simple Name Database

This was one of my first c++ programs ever just trying to learn how to do some input, output, and vectors for storage. This script allows you to add a basic person with name, address, gender, and age or delete them. It also includes a couple functions to print just names, all data, or the average age.

   1.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

class Person {
private:
	int age;
	std::string firstName, lastName, address, gender;
public:
	Person(std::string a, std::string b,std::string c, std::string d, int e){
		firstName = a;
		lastName = b;
		address = c;
		gender = d;
		age = e;
	};
	std::string sprop(std::string a) {
		if (a == "firstName") return firstName;
		if (a == "lastName") return lastName;
		if (a == "address") return address;
		if (a == "gender") return gender;
		return "";
	};
	int Age() {
		return age;
	};
};

void addPerson(std::vector<Person> &);
void deletePerson(std::vector<Person> &);
void listNames(std::vector<Person> &);
void printDatabase(std::vector<Person> &);
void printAvgAge(std::vector<Person> &);
bool isBefore(Person, Person);

int main() {
	int inNumber;
	std::vector<Person> db;
	std::cout << "Welcome! ";
	do {
		std::cout << "Please pick an option below." << std::endl;
		std::cout << "  (0) Add person" << std::endl;
		std::cout << "  (1) Delete person" << std::endl;
		std::cout << "  (2) List names" << std::endl;
		std::cout << "  (3) Print Database" << std::endl;
		std::cout << "  (4) Print average age" << std::endl;
		std::cout << "  (5) Exit" << std::endl;
		std::cout << "Your input: ";
		std::cin >> inNumber;
		if (inNumber < 0 || inNumber > 5) std::cout << "Invalid input. Please try again." << std::endl << std::endl;
		else {
			switch (inNumber) {
				case 0: addPerson(db); break;
				case 1: deletePerson(db); break;
				case 2: listNames(db); break;
				case 3: printDatabase(db); break;
				case 4: printAvgAge(db); break;
			};
		}
	} while (inNumber != 5);
	
	std::cout << std::endl << "Thank you for using this program!" << std::endl;
	
	return 0;
}

void addPerson(std::vector<Person> &db) {
	std::string fName, lName, address, gender;
	int age;
	std::cin.ignore(1000,'\n');
	std::cout << std::endl << "Please input the following information:";
	std::cout << std::endl << "First Name: ";
	std::getline(std::cin,fName);
	std::cout << "Last Name: ";
	std::getline(std::cin,lName);
	std::cout << "Address: ";
	std::getline(std::cin,address);
	std::cout << "Gender: ";
	std::getline(std::cin,gender);
	std::cout << "Age: ";
	std::cin >> age;
	Person N = Person(fName, lName, address, gender, age);
	db.push_back(N);
	std::cout << std::endl << "Person added." << std::endl;
}

void deletePerson(std::vector<Person> &db) {
	int num;
	std::cout << std::endl << "Please input the corresponding number of the person you would like to delete.";
	listNames(db);
	std::cout << std::endl << "Your input: ";
	std::cin >> num;
	db.erase(db.begin() + num);
	std::cout << "Person Deleted." << std::endl;
}

void listNames(std::vector<Person> &db) {
	std::sort(db.begin(),db.end(),isBefore);
	std::cout << std::endl;
	for(std::vector<int>::size_type i=0; i<db.size(); i++) {
		Person p = db[i];
		std::cout << "("<<i<<") "<< p.sprop("firstName") << " " << p.sprop("lastName") << std::endl;
	}
	std::cout << std::endl;
}

void printDatabase(std::vector<Person> &db){
	std::cout << std::endl;
	std::sort(db.begin(),db.end(),isBefore);
	for(std::vector<int>::size_type i=0; i<db.size(); i++) {
		Person p = db[i];
		std::cout << p.sprop("firstName") << " " << p.sprop("lastName") << ", " << p.Age() << ", " << p.sprop("gender") << "\n" << p.sprop("address") << std::endl;
		std::cout << std::endl;
	}
	std::cout << std::endl;
}

void printAvgAge(std::vector<Person> &db){
	float total = 0;
	std::vector<int>::size_type i=0;
	for (; i<db.size(); i++) {
		total += db[i].Age();
	}
	std::cout << std::endl << "The average age of all names is " << total/i << "." << std::endl << std::endl;
}

bool isBefore(Person A, Person B) {
	if (A.sprop("lastName") > B.sprop("lastName")) {
		return true;
	} else if (A.sprop("lastName") == B.sprop("lastName")) {
		if ( A.sprop("firstName") > B.sprop("firstName")) {
			return true;
		} else if (A.sprop("firstName") == B.sprop("firstName")) {
			if (A.Age() > B.Age()) {
				return true;
			}
		}
	}
	return false;
}