Carol Cody
Carol Cody

Reputation: 15

finding a string in a file using fstream in c++

This is my code

/*
    Asks the user for their ID, depending on the ID depends on the results. It either goes to maintanance
    or it asks the user to return DVD's or check DVD's out and changes the stock of the DVD's.
    Cody Close
*/

#include <iostream>
#include <fstream>
#include <conio.h>
#include <sstream>
#include <string>

using namespace std;

void custID();
void sales();
void returns();
void discounts();
void maint();
void createAcc(string* filename, string* newID);
bool checkID(string* filename, string* search);

int main()
{
   //Declares all the variables for the program
   int mainID= 99959, menuChoice;
   bool close = false;
   bool done = false;
   string vidId;

   //Declares and input file and opens a file
   fstream inFile;
   inFile.open("dayin00.dat");

   do{
       do{
           cout << "accountID: " << endl;
           cin >> mainID;
           stringstream out;
           out << mainID;
           mainid = out.str();
           checkID("IDlist.txt", mainid);
       }while(mainid.length() < 5 || mainid.length() > 9);
           if(mainID!= 99959)
           {
               do
               {
                   cout << "MENU:" << endl;
                   cout << "(1)Purchase\n(2)Return\n(3)Exit" << endl;
                   cin >> menuChoice;
                   switch(menuChoice)
                   {
                   case 1:
                   case 2:
                   case 3:
                       done = true;
                   }
               }while(done == false);
           }else{
               maint();
           }

       close = true;
   }while(close == false);

   return 0;
}

void maint()
{
   int maintChoice;

   cout << "\n(1)Summary\n(2)Withdrawl\n(3)Close Down\n(4)Back to >main\n(0)Help" << endl;
   cin >> maintChoice;

   switch (maintChoice)
   {
       case 1:

       case 2:
       case 3:
       case 4:
       default:
           cout << "1 for summary, 2 for withdrawl, 3 to close down, 4 to >go back to main" << endl;
   }
}

void createAcc(string* filename, string* newID)
{
   fstream newFile;
   newFile.open(filename);
   newFile << newID;
}
void checkID(string* filename, string* ID)
{
   fstream infile;
   infile.open("IDlist.txt");
   string word;

   infile >> word;
   while (!infile.eof()){
       if(word == ID)
       {
           cout << "ID FOUND!" << endl;
       }else{
           createAcc(infile, ID);
       }
   }
}

The text file only contains the ID 99959. How do I check if the ID the user types in already exists in the text file and if it doesn't, then it goes to createAcc(),setting up a new account using the ID that the user has entered.

Upvotes: 1

Views: 2836

Answers (1)

gomons
gomons

Reputation: 1976

The code opens file with users ID in read mode, reads it line by line and tries to finde ID. If ID not found in file, it opens file in write mode and add user ID in file.

#include <iostream>
#include <fstream>
#include <stdexcept>

void createAcc(const std::string& filename, const std::string& id)
{
    std::ofstream os(filename);
    if (os)
        os << id;
    else
        throw std::runtime_error("Open file error: " + filename);
}

bool isStringContainsID(const std::string& line, const std::string& id)
{
    if (line.find(id) == std::string::npos)
        return false;
    else
        return true;
}

bool isFileContainsID(const std::string& filename, const std::string& id)
{
    std::ifstream is(filename);
    if (!is)
        throw std::runtime_error("Open file error: " + filename);
    std::string line;
    while (is) 
    {
         std::getline(is, line);
         if (isStringContainsID(line, id))
             return true;
    }
    return false;
}

int main() {

    std::string id("99959");
    std::string file_name("IDlist.txt");

    if (isFileContainsID(file_name, id))
        std::cout << "ID FOUND!" << std::endl;
    else
        createAcc(file_name, id);

    return 0;
}

Note that all users ID should have the same length in string representation, otherwise the code can find shorter ID in file that contains larger ID with shorter ID as sub-string.

Upvotes: 1

Related Questions