From 9227cecf4581fc06daab687b4f3fa96a4eb6e993 Mon Sep 17 00:00:00 2001 From: James Vickery Date: Sun, 12 Mar 2017 02:34:11 +0000 Subject: [PATCH] Databases and user input stuff --- src/dbstore.cpp | 30 ++++++++++-------------------- src/userin.cpp | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/dbstore.cpp b/src/dbstore.cpp index be11f48..b655963 100644 --- a/src/dbstore.cpp +++ b/src/dbstore.cpp @@ -5,24 +5,6 @@ #include using namespace std; -int database_test() -{ - sqlite3* db; - char* zErrMsg = 0; - int rc; - - rc = sqlite3_open("database/test.db", &db); - - if (rc) { - return (0); - } else { - return 1; - } - sqlite3_close(db); - - return 0; -} - static int callback(void* NotUsed, int argc, char** argv, char** azColName) { int i; @@ -58,10 +40,18 @@ int create_user_database(string player_name) // Execute SQL statement rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); + if (rc != SQLITE_OK) { - fprintf(stderr, "SQL error: %s\n", zErrMsg); - sqlite3_free(zErrMsg); + string zErrMsg_str = zErrMsg; + string tableExists = "table INVENTORY already exists"; + if (tableExists == zErrMsg_str) { + print("Loaded " + databaseName); + } else { + print(zErrMsg_str); + sqlite3_free(zErrMsg); + } } + sqlite3_close(db); return 0; } diff --git a/src/userin.cpp b/src/userin.cpp index 3ac01cc..94e3b0b 100644 --- a/src/userin.cpp +++ b/src/userin.cpp @@ -21,10 +21,26 @@ bool valid_name(string test_name) return true; } +bool valid_text(string text) +{ + if (text.length() < 1){ + return false; + } + return true; +} + string get_text() { string textIn = ""; - cin >> textIn; + bool validIn = false; + while(!validIn){ + getline(cin, textIn); + if (!valid_text(textIn)){ + print("Invalid input - please try again", "red"); + } else { + validIn = true; + } + } return textIn; }