From 5e2b91fda659e9f2e4bbf2551ea687a9aeea7ab8 Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Mon, 8 Jul 2019 18:31:10 -0300 Subject: [PATCH] fixed buffer thing in input --- Customer.cpp | 9 +++++---- Interface.cpp | 39 ++++++++++++++++++++++++++++----------- test.db3 | Bin 16384 -> 16384 bytes 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Customer.cpp b/Customer.cpp index 7bb5e8b..1ff4fb8 100644 --- a/Customer.cpp +++ b/Customer.cpp @@ -54,13 +54,14 @@ int Customer::parked_at() { return park_instances[park_instances.size() - 1].spo // functions that interact with the database void Customer::save_db() { - string statement{"insert into Customer values (, '', '', , );"}; + string statement{"insert into Customer values (, '', '', ,'');"}; // after ( = 28) - statement.insert(40, telephone); + statement.insert(41, telephone); statement.insert(38, to_string(int(vehicle))); statement.insert(36, password); statement.insert(32, name); statement.insert(29, to_string(id)); + // cout << statement; SQLite::Transaction transaction(data::db); data::db.exec(statement); transaction.commit(); @@ -75,8 +76,8 @@ void Customer::update_db() { statement.insert(57, to_string(int(vehicle))); statement.insert(43, password); statement.insert(28, name); - cout << statement; - // data::db.exec(statement); + // cout << statement; + data::db.exec(statement); } void Customer::delete_db() { diff --git a/Interface.cpp b/Interface.cpp index 0335ee3..3aec372 100644 --- a/Interface.cpp +++ b/Interface.cpp @@ -1,8 +1,10 @@ #include "headers/Interface.h" - // I added it to pass spots, because the parking options need it to check where // is free parking_spots is declared in main, and if i declare it +// liberal use of +// cin.ignore(10000, '\n'); +// so it skips to the next newline, in essence clearing the cin buffer void interface(vector& spots) { int selector; @@ -11,6 +13,7 @@ void interface(vector& spots) { cout << "\n[1]Log in as member"; cout << "\n[2]Log in as administrator"; cin >> selector; + cin.ignore(10000, '\n'); switch (selector) { case 1: { interface_member(spots); @@ -28,19 +31,20 @@ void interface_member(vector& spots) { string password; cout << "\nPlease input id:"; cin >> id; + cin.ignore(10000, '\n'); Customer c = query_customer_with_id(id); cout << "\nPlease input password:"; - cin >> password; - + std::getline(cin, password); while (!(verify_password(c.password, password))) { - cout << "ERROR: wrong password. Please retype your password \n"; - cin >> password; + cout << "ERROR: wrong password. Please retype your password:\n"; + std::getline(cin, password); } cout << "Logged in succesfully\n"; - cout << "select an option\n [1] Parking options\n[2]monthy report"; + cout << "select an option\n [1] Parking options\n[2]monthy report\n"; int option; cin >> option; + cin.ignore(10000, '\n'); switch (option) { case 1: { park(c, spots); @@ -64,9 +68,11 @@ void interface_admin(vector& spots) { cout << "[4] See weekly report of a specific parking spot\n"; cout << "[5] See current status of parking spots\n"; cout << "[6] Make new customer\n"; - cout << "option[1-6]:"; + cout << "[7] Make new parking spot\n"; + cout << "option[1-7]:"; int option; cin >> option; + cin.ignore(10000, '\n'); switch (option) { case 1: { reports_from_allparkspots(); @@ -80,6 +86,7 @@ void interface_admin(vector& spots) { cout << "Which parking spot would you like a report on?ID:"; int spotid; cin >> spotid; + cin.ignore(10000, '\n'); reports_from_parkspot(spotid); break; } @@ -87,17 +94,21 @@ void interface_admin(vector& spots) { cout << "Which parking spot would you like a report on?ID:"; int spotid; cin >> spotid; + cin.ignore(10000, '\n'); reports_from_parkspot(spotid, true); break; } case 5: { current_status_parkspots(spots); + break; } case 6: { new_customer(); + break; } case 7: { new_parkspot(spots); + break; } default: @@ -121,6 +132,7 @@ void park(Customer& c, vector& spots) { cout << "where do you want to park?"; int parkid; cin >> parkid; + cin.ignore(10000, '\n'); for (Park_spot& i : spots) { if (i.id == parkid) { i.clock(c); @@ -133,6 +145,7 @@ void park(Customer& c, vector& spots) { << ", do you want to clock out?\n enter [1] for yes and [0] for no"; int answer = 0; cin >> answer; + cin.ignore(10000, '\n'); if (answer) { query_parkspot_with_id(c.parked_at(), spots).clock(c); cout << "You have sucessfully clocked out."; @@ -148,20 +161,24 @@ void new_customer() { string password; string telephone; cout << "What's the name of the customer? "; - cin >> name; + std::getline(cin, name); cout << "What's the vehicle type? [1]twoweeler, [2] fourweeler: "; cin >> vtype; + cin.ignore(10000, '\n'); cout << "What's the telephone number? "; - cin >> telephone; - cout << "What's the password? "; - cin >> password; + std::getline(cin, telephone); + cout << "What's the password?"; + std::getline(cin, password); Customer newcustomer{name, password, Vehicle_type(vtype), telephone}; cout << "New customer sucessfully created\n"; + newcustomer.update_db(); } void new_parkspot(vector& spots) { cout << "What type of parking spot? [1] twoweeler, [2] fourweeler: "; int vtype; + cin >> vtype; + cin.ignore(10000, '\n'); Park_spot newspot{Vehicle_type(vtype)}; spots.push_back(newspot); cout << "new parking spot sucessfully created.\n"; diff --git a/test.db3 b/test.db3 index 48e8b132829772fd943c45f7cb1feeff1dbd8156..e591386fa0f5dbcd250b2f914e0d97ad7408db94 100644 GIT binary patch delta 241 zcmZo@U~Fh$oFL7}I#I@%k#%FjGJaMD2L4+9&4L17`Gl&O*cs#-{W%qj6Vnrm6p9ja zC(FpoCz%C1n}((*=T_x;Cs%k>mE}8^rI|!kdKH_PTAEuJfI%YzBRhj?qd%jvSAKCuX`+I6Vo^yx$Qr+* z#3+5|oQ$&UvOr^>^Z?^@eN%Jabd~%}g9^iv@=SLl{cK0al!z*S_wdNF$kc3~4F5cz h5GPO1f-DQ~NcRkbawecX3KoV&#wMm_iwp#S2mr*qNFe|K delta 318 zcmZo@U~Fh$oFL7}K2gS*k$q#rGJZC82L5(__RWF{zxnFxnAjOq8~r&ILsE-NoJ)&K z@^e#*R1%BQ^Ye@{Q&h@q4J}o2ZH*0$Obv8OYz=h^Yzq04J>0`mA~TEqjkSFv+!HIz&AkJ%3?fbSGedI&@=Gi#icKxe zEzFI~jg0Co%NZdK5H0~aAel%9goZjLx@8w+l~g3=MCO{L1be0wm8)cz1v+Q@8fTOT znHL)w7&`}+hZ$QI=|}h_nrC|?2UHfCxre$NI6IYu7yuo>%fP_E#LoYTf&VA}CtzSa U