I am trying to simulate a database. I basically need to prompt the user for a command (a letter essentially) and based on that letter go to the different cases. I cannot figure it out on how to read the file and check if the given string already exists on the file. If it does, then go ahead and add the information to the file. If not, then report problem for the user.
void add (std::string input2)
{
char letter1;
char letter2;
std::string abbreviatureBrand;
std::string abbreviatureBrand1;
std::stringstream comparison;
std::stringstream ss1(input2);
ss1 >> letter1 >> letter2;
if(letter2 == 'c')
{
std::fstream file("data1.txt",std::ios::app);
std::string VIN;
int miles;
std::string dealership;
int price;
char dataType = 'c';
char dataType1 = 'v';
int count = 0;
ss1 >> VIN >> miles >> dealership >> price;
char a = VIN.at(0); // get the character at position 0 of string VIN
char b = VIN.at(1); // get the character at position 1 of string VIN
char c = VIN.at(2); // get the character at postion 2 of string VIN
comparison << a << b << c; // converting each character into a string
comparison >> abbreviatureBrand1; // passing the characters to the abbreviature brand string
while (!file.eof() && count < 1)
{
if(abbreviatureBrand == abbreviatureBrand1)
{
file << dataType << " " << VIN << " " << miles << " " << dealership << " " << price << std::endl;
count++;
}
else
{
std::cout << "Manufacturer not found. Please add a manufacturer before trying to add a VIN" << std::endl;
count++;
}
}
file.close();
}
else if(letter2 == 'm')
{
std::fstream file("data1.txt",std::ios::app);
std::string Manufacturer;
char dataType = 'b';
ss1 >> abbreviatureBrand >> Manufacturer;
file << dataType << " " << abbreviatureBrand << " " << Manufacturer << std::endl;
file.close();
}