Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help fixing my code in c++. The program still fails to create a table because no database is being used. Here is the

I need help fixing my code in c++. The program still fails to create a table because no database is being used.

Here is the terminal output below. As you can see, despite using database db_1, it fails to create a table due to no database being use.image text in transcribed

Here is the sql file test case with the expected output on the bottom. The expected output is that table tb_1 along with the data is created.

image text in transcribed

Here is my main.cpp file and table.h file that needs fixing

==== main.cpp ====

#include #include #include #include #include #include #include #include

#include "table.h"

using namespace std;

void parseInput(const string& input, vector& commands); //parse commands with delimiter void parseCommands(vector& commands, vector

& tables); //execute commands and modify tables string getName(string line); //get name string getTblName(string line); //get table name string getData(string line); //get data

int main(int argc, char* argv[]){

string input; vector commands; vector

tables;

while (input != ".EXIT"){ //main loop cout "; getline(cin, input);

parseInput(input, commands); parseCommands(commands, tables); } return 0; }

//getData function, grabs data return line string getData(string line){ auto it = find(line.begin(), line.end(), '('); line = string(it, line.end()); line = line.substr(1, line.size() - 2); replace(line.begin(), line.end(), ',', '|');

return line; }

//getName function, gets name string getName(string line){ stringstream ss(line); string token; while (getline(ss, token, ' ')){ } return token; }

//getTblName, gets table name string getTblName(string line){ stringstream ss(line); string token; int i = 0; while (getline(ss, token, ' ') && i & commands){ stringstream ss(input); string token; while (getline(ss, token, ';')){ commands.push_back(token); } }

//parseCommands function, handles the database systems void parseCommands(vector& commands, vector

& tables){ string currDB;

for (auto i : commands){ if (i.find("CREATE DATABASE") != -1){ string dbName = getName(i); const int direrr = system(("mkdir " + dbName).c_str()); if (direrr == 0) cout

else if (i.find("USE") != -1) {

string dbName = getName(i);

if (chdir(dbName.c_str()) == 0) {

currDB = dbName;

cout

} else {

cout

}

}

else if (i.find("CREATE TABLE") != -1){ if (currDB.empty()){ cout

size_t index = it - tables.begin(); //finds table index if it exists if (it != tables.end()){ cout

commands.clear(); }

==== table.h ====

#pragma once

#include #include

using namespace std;

class table{ public: string name; string db; string data;

table(string tbName, string currDB, string currData){ name = tbName; db = currDB; data = currData; }

table(string tbName, string currDB){ name = tbName; db = currDB; } string getName(){ return name; } string getData(){ return data; } void add(string line){ //puts add in substr line = line.substr(line.find("ADD") + 3); data.append(" | " + line); }

~table(){}

//operator overload to check for equality to see if table exists bool operator==(table const& rhs){ if (name == rhs.name && db == rhs.db) return true; else false; } };

PS C: VUsers\17029\Desktop\PA1_Database> ./main > CREATE DATABASE db 1; Database db_ 1 created. > USE db1; Using database db_1. > CREATE TABLE tbl_1 (a1 int, a2 varchar(20)); IFailed to create table because no database is being used

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

b. Will new members be welcomed?

Answered: 1 week ago

Question

c. Will leaders rotate periodically?

Answered: 1 week ago

Question

b. Will there be one assigned leader?

Answered: 1 week ago