0

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();
}
  • If `VIN` is, for example, `"ABC..."` then `comparison << a << b << c` will create the string `"ABC"`. It would be much easier to just get those three first characters as a [sub-string](https://en.cppreference.com/w/cpp/string/basic_string/substr). – Some programmer dude Sep 10 '19 at 06:23
  • Also, `while (!file.eof())` only makes sense if you *read* from the file. And even then [it's still wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Some programmer dude Sep 10 '19 at 06:24
  • @Someprogrammerdude I already fixed it so i just get the first three characters. Thanks for the advice. Do you have any idea on how to fix the file reading part? – geekloco Sep 10 '19 at 06:34
  • For starters, I suggest you open the file in read-only mode initially, and read it line by line, putting each line into a `std::istringstream` that you can use to extract the data. Then do that in a loop, checking if the record you want to write exist or not. If it exist, report an error and return. Else you reopen the file in append mode and just write the data. I also suggest you split the code into several smaller functions, single responsibility principle and all that. – Some programmer dude Sep 10 '19 at 06:41
  • As an alternative, keep the data in memory, using e.g. structures (classes) for each record and store them in a map or a vector. Then if the entry exists in the container you report an error otherwise you open the file in append mode and append the new data. Again, multiple smaller functions would make it easier to both write the code and to debug it in case of problems. – Some programmer dude Sep 10 '19 at 06:44

0 Answers0