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
|
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <cstdlib>
void ViewDatabase();
void Addnew();
int main()
{
std::cout << "Choose" << std::endl;
std::cout << "1. See Database" << std::endl;
std::cout << "2. Add new car" << std::endl;
int choice;
std::cin >> choice;
switch (choice)
{
case 1:
ViewDatabase();
break;
case 2:
Addnew();
break;
}
}
void ViewDatabase()
{
system("cls");
std::string line, page;
std::fstream database("database.txt");
if (database.is_open()) {
for (int i = 0; i < 5; i++) {
std::getline(database, line);
std::cout << i + 1 << ". " << line << std::endl;
}
database.close();
std::cout << std::endl << "\"n\" next page | \"p\" previous page | \"e\" exit" << std::endl;
std::cin >> page;
}
system("pause");
}
void Addnew()
{
system("cls");
std::string carname, prodyear, power, fuel, engine, eng_pos;
std::cout << "Enter the cars name: " << std::endl;
std::getline(std::cin, carname); //skips this
std::cout << "Enter the production year(s): " << std::endl;
std::getline(std::cin, prodyear);
std::cout << "Enter the engine power output: " << std::endl;
std::getline(std::cin, power);
std::cout << "Enter the fuel type: " << std::endl;
std::getline(std::cin, fuel);
std::cout << "Enter the engine details: " << std::endl;
std::getline(std::cin, engine);
std::cout << "Enter the engine position: " << std::endl;
std::getline(std::cin, eng_pos);
std::cout << "Do you want to save? (y/n)" << std::endl;
std::string save;
std::cin >> save;
if (save == "y") {
std::ofstream database("database.txt", std::ios::app);
database << carname << "; " << prodyear << "; " << power << "kW; " << fuel << "; " << engine << "; " << eng_pos << ' ' << std::endl;
database.close();
std::cout << "Save successful" << std::endl;
system("pause");
}
else {
std::cout << "Save not successful" << std::endl;
system("pause");
}
}
|