Twigz's Profile User Rating: -----

Reputation: 2 Apprentice
Group:
Active Members
Active Posts:
99 (0.07 per day)
Joined:
11-July 11
Profile Views:
1,417
Last Active:
User is offline Jan 16 2015 02:36 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   Twigz has not set their status

Posts I've Made

  1. In Topic: file input output problems

    Posted 13 Jan 2015

    I took a good look at it. It helped. I'm slowly starting to grasp all of this.

    However by doing this:
    void find_rec(std::istream &infile)
    
    

    both my edit_rec() and del_rec() wouldn't save the info to the file, but the procedure would work. When i looked at it, it was because the "temp.txt"(with all the correct info) wasn't able to be renamed to "record.bank", because "record.bank" was still open. I tried closing it, and it still didn't work. So i went back to the way i was doing it.

    Quote

    Now remember trying to modify a file anywhere but at the end is not practical and it is also very error prone. So your "deletion" and "modification" functions will need to read all the prior records writing them to a new file, then modify or skip the record in question, then write the modifications and continue with the rest of the file. Then you'll need to delete the old file and rename the new file to the original file name.


    When you said this, i'm guessing you meant i should do this right? Or, you meant something else.

    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <iomanip>
    #include <math.h>
    #include "Account_Querry.h"
    
    using namespace std;
    
    void Account_Querry::setAccountNo(int accountNo)
    {
        this->accountNo = accountNo;
    }
    
    int Account_Querry::getAccountNo()
    {
        return accountNo;
    }
    
    void Account_Querry::setFirstName(string firstName)
    {
        this->firstName = firstName;
    }
    
    string Account_Querry::getFirstName()
    {
        return firstName;
    }
    
    void Account_Querry::setLastName(string lastName)
    {
        this->lastName = lastName;
    }
    
    string Account_Querry::getLastName()
    {
        return lastName;
    }
    
    void Account_Querry::setBalance(double balance)
    {
        this->balance = balance;
    }
    
    double Account_Querry::getBalance()
    {
        return balance;
    }
    
    void Account_Querry::read_data()
    {
        int account_number = 0;
        cout<<"\nEnter Account Number: ";
        cin>>account_number;
        cin.ignore();
        setAccountNo(account_number);
    
        string firstName, lastName;
        cout<<"Enter First Name: ";
        getline(cin,firstName,'\n');
        setFirstName(firstName);
    
        cout<<"Enter Last Name: ";
        getline(cin,lastName,'\n');
        setLastName(lastName);
    
        double total_balance = 0;
        cout<<"Enter Balance: ";
        cin>>total_balance;
        setBalance(total_balance);
    }
    
    void Account_Querry::write_rec()
    {
        ofstream outfile("record.bank", ofstream::app);
        read_data();
        outfile << getAccountNo() << ':' << getLastName() << ':' << getFirstName() << ':' << getBalance() << endl;
        outfile.close();
    }
    
    void Account_Querry::search_rec()
    {
        ifstream infile("record.bank");
    
        int nRec = 0;
        string check;
        while(getline(infile,check))
        {
            nRec++;
        }
    
        infile.clear(); // clear eof and fail bits
        infile.seekg(0, ios::beg);
    
        if(nRec == 0)
        {
            system("cls");
            cout << "There are " << nRec << " records in the file." << endl;
        }
        else
        {
            cout << "There are " << nRec << " records in the file." << endl;
    
            system("cls");
            Account_Querry tmp;
            char colon;
    
            while(infile >> tmp.accountNo >> colon)
            {
                // The names can have spaces embedded within them so you need to use getline. Use the optional third parameter to stop at the semicolon.
                // And remember the semicolon is retrieved and discarded.
                getline(infile, tmp.firstName, ':');
                getline(infile, tmp.lastName, ':');
                infile >> tmp.balance;
    
                cout << "Current Information:" << endl;
    
                int lNameLength = tmp.lastName.size();
                int fNameLength = tmp.firstName.size();
                int lengthTotal = fNameLength + lNameLength;
                int balLength = log10(tmp.balance);
                int spaces_needed = 7 - balLength;
    
                cout << "Name";
                for(int y = 1; y < lengthTotal-1; y++)
                {
                    cout << " ";
                }
                cout << "Balance";
                cout << " ";
                cout << "ID#" << endl;
                for(int x = 2; x < lengthTotal + 20; x++)
                {
                    cout << "-";
                }
                cout << ' ' << endl;
    
                cout << tmp.firstName << ' ' << tmp.lastName << ' '  <<  tmp.balance << string(spaces_needed, ' ') << tmp.accountNo << endl;
                cout << endl;
                for(int x = 2; x < lengthTotal + 20; x++)
                {
                    cout << "-";
                }
                cout << endl;
            }
        }
        infile.clear(); // clear eof and fail bits
        infile.seekg(0, ios::beg);
        infile.close();
    }
    
    void Account_Querry::read_rec()
    {
        ifstream infile("record.bank");
    
        int nRec = 0;
        string check;
        while(getline(infile,check))
        {
            nRec++;
        }
    
        infile.clear(); // clear eof and fail bits
        infile.seekg(0, ios::beg);
    
        if(nRec == 0)
        {
            system("cls");
            cout << "There are " << nRec << " records in the file." << endl;
        }
        else
        {
            cout << "There are " << nRec << " records in the file." << endl;
            int empId;
            cout << "Enter ID of the employee whose data you wish to find: ";
            cin >> empId;
    
            system("cls");
            Account_Querry tmp;
            char colon;
    
            while(infile >> tmp.accountNo >> colon)
            {
                // The names can have spaces embedded within them so you need to use getline. Use the optional third parameter to stop at the semicolon.
                // And remember the semicolon is retrieved and discarded.
                getline(infile, tmp.firstName, ':');
                getline(infile, tmp.lastName, ':');
                infile >> tmp.balance;
                if(tmp.accountNo == empId)
                {
                    cout << "Current Information:" << endl;
    
                    int lNameLength = tmp.lastName.size();
                    int fNameLength = tmp.firstName.size();
                    int lengthTotal = fNameLength + lNameLength;
                    int balLength = log10(tmp.balance);
                    int spaces_needed = 7 - balLength;
    
                    cout << "Name";
                    for(int y = 1; y < lengthTotal-1; y++)
                    {
                        cout << " ";
                    }
                    cout << "Balance";
                    cout << " ";
                    cout << "ID#" << endl;
                    for(int x = 2; x < lengthTotal + 20; x++)
                    {
                        cout << "-";
                    }
                    cout << ' ' << endl;
    
                    cout << tmp.firstName << ' ' << tmp.lastName << ' '  <<  tmp.balance << string(spaces_needed, ' ') << tmp.accountNo << endl;
                    cout << endl;
                }
            }
        }
        infile.clear(); // clear eof and fail bits
        infile.seekg(0, ios::beg);
        infile.close();
    }
    
    void Account_Querry::edit_rec()
    {
        ifstream infile("record.bank");
        ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete
    
        int nRec = 0,x=0;
        string check;
        while(getline(infile,check))
        {
            nRec++;
        }
    
        infile.clear(); // clear eof and fail bits
        infile.seekg(0, ios::beg);
    
        if(nRec == 0)
        {
            system("cls");
            cout << "There are " << nRec << " records in the file." << endl;
        }
        else
        {
            cout << "There are " << nRec << " records in the file." << endl;
            int empId;
            cout << "Enter ID of the employee whose data you wish to edit: ";
            cin >> empId;
            cin.ignore();
    
            system("cls");
            Account_Querry tmp;
            char colon;
    
            while(infile >> tmp.accountNo >> colon)
            {
                // The names can have spaces embedded within them so you need to use getline. Use the optional third parameter to stop at the semicolon.
                // And remember the semicolon is retrieved and discarded.
                getline(infile, tmp.lastName, ':');
                getline(infile, tmp.firstName, ':');
                infile >> tmp.balance;
    
    
                if(tmp.accountNo != empId) // if there are students with different name, input their data into temp file
                {
                    temp << tmp.accountNo << ':' << tmp.lastName << ':' << tmp.firstName << ':' << tmp.balance << endl;
                }
                if(tmp.accountNo == empId) // if user entered correct name, x=1 for later output message that the user data has been deleted
                {
                    string firstName, lastName;
                    cout<<"Enter First Name: ";
                    getline(cin,firstName,'\n');
                    setFirstName(firstName);
    
                    cout<<"Enter Last Name: ";
                    getline(cin,lastName,'\n');
                    setLastName(lastName);
    
                    double total_balance = 0;
                    cout<<"Enter Balance: ";
                    cin>>total_balance;
                    setBalance(total_balance);
                    temp << tmp.accountNo << ':' << getLastName() << ':' << getFirstName() << ':' << getBalance() << endl;
                    x=1;
                }
            }
            infile.clear(); // clear eof and fail bits
            infile.seekg(0, ios::beg);
            infile.close();
            temp.close();
            remove("record.bank");
            rename("temp.txt","record.bank");
        }
    
        if(x==0)  // x was set to 0 at start, so if it didn't change, it means user entered the wrong name
        {
            cout << "There is no Account with that ID." << endl;
        }
        else  // x is not 0, it means user entered the correct name, print message that students data has been deleted
        {
            cout << "Account data has been successfully updated." << endl;
        }
    }
    
    void Account_Querry::delete_rec()
    {
        ifstream infile("record.bank");
        ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete
    
        int nRec = 0,x=0;
        string check;
        while(getline(infile,check))
        {
            nRec++;
        }
    
        infile.clear(); // clear eof and fail bits
        infile.seekg(0, ios::beg);
    
        if(nRec == 0)
        {
            system("cls");
            cout << "There are " << nRec << " records in the file." << endl;
        }
        else
        {
            cout << "There are " << nRec << " records in the file." << endl;
            int empId;
            cout << "Enter ID of the employee whose data you wish to find: ";
            cin >> empId;
    
            system("cls");
            Account_Querry tmp;
            char colon;
    
            while(infile >> tmp.accountNo >> colon)
            {
                // The names can have spaces embedded within them so you need to use getline. Use the optional third parameter to stop at the semicolon.
                // And remember the semicolon is retrieved and discarded.
                getline(infile, tmp.lastName, ':');
                getline(infile, tmp.firstName, ':');
                infile >> tmp.balance;
    
    
                if(tmp.accountNo != empId) // if there are students with different name, input their data into temp file
                {
                    temp << tmp.accountNo << ':' << tmp.lastName << ':' << tmp.firstName << ':' << tmp.balance << endl;
                }
                if(tmp.accountNo == empId) // if user entered correct name, x=1 for later output message that the user data has been deleted
                {
                    x=1;
                }
            }
            infile.clear(); // clear eof and fail bits
            infile.seekg(0, ios::beg);
            infile.close();
            temp.close();
            remove("record.bank");
            rename("temp.txt","record.bank");
        }
    
        if(x==0)  // x was set to 0 at start, so if it didn't change, it means user entered the wrong name
        {
            cout << "There is no Account with that ID." << endl;
        }
        else  // x is not 0, it means user entered the correct name, print message that students data has been deleted
        {
            cout << "Account data has been deleted." << endl;
        }
    }
    
    


    Although, this works i doubt it's completely what you meant.
  2. In Topic: file input output problems

    Posted 12 Jan 2015

    Well, i changed the delimiter to a colon... hopefully thats alright.

    My compiler didn't give me those errors....
    Is there a way to make it show me all those kinds of errors. I'm using code blocks.

    also i mad e a few changes here:
    void Account_Querry::read_rec()
    {
        ifstream infile("record.bank");
    
        int nRec = 0;
        string check;
        while(!infile.eof())
        {
            nRec++;
        }
        cout << "There are " << nRec << " records in the file." << endl;
    
        infile.seekg(ios::beg);
    
        int empId;
        cout << "Enter ID of the employee whose data you wish to find: ";
        cin >> empId;
    
        Account_Querry tmp;
    
        while(infile >> tmp.accountNo >> tmp.firstName >> tmp.lastName >> tmp.balance)
        {
            system("cls");
            if(tmp.accountNo == empId)
            {
                cout << "Current Information:" << endl;
    
                int lNameLength = tmp.lastName.size();
                int fNameLength = tmp.firstName.size();
                int lengthTotal = fNameLength + lNameLength;
                int balLength = log10(tmp.balance);
                int spaces_needed = 7 - balLength;
    
                cout << "Name";
                for(int y = 1; y < lengthTotal-1; y++)
                {
                    cout << " ";
                }
                cout << "Balance";
                cout << " ";
                cout << "ID#" << endl;
                for(int x = 2; x < lengthTotal + 20; x++)
                {
                    cout << "-";
                }
                cout << ' ' << endl;
    
                cout << tmp.firstName << ' ' << tmp.lastName << ' '  <<  tmp.balance << string(spaces_needed, ' ') << tmp.accountNo << endl;
                cout << endl;
                //flag = false;
            }
            else
            {
                cout << "Record Not Found! Try Again...." << endl;
            }
        }
        infile.close();
    }
    


    I got rid of the parallel arrays and did this instead..
    Account_Querry tmp;
    
        while(infile >> tmp.accountNo >> tmp.firstName >> tmp.lastName >> tmp.balance)
        {
    


    Sadly, this reads in only the first line of the file...., i'm not sure if i'm doing it wrong...

    Also i tried not to close the file here:
    while(!infile.eof())
        {
            nRec++;
        }
        cout << "There are " << nRec << " records in the file." << endl;
    
        infile.seekg(ios::beg);
    


    using seekg to get back to the beginning of the file... but that didn't work out...

    Quote

    Lastly you may want to consider trying to use a vector to keep all the data in memory instead of continually reading and writing the information to and from your files.


    i have no knowledge of vectors as yet.

    Trying to wrap my head around this, but it's complicated....
    Thank you for your assistance.
  3. In Topic: file input output problems

    Posted 12 Jan 2015

    Ahh i see, well i removed the global variables and seperated the files with a space as the delimiter.

    now for this part:

    Quote

    Now remember trying to modify a file anywhere but at the end is not practical and it is also very error prone. So your "deletion" and "modification" functions will need to read all the prior records writing them to a new file, then modify or skip the record in question, then write the modifications and continue with the rest of the file. Then you'll need to delete the old file and rename the new file to the original file name.


    I'm still not sure how i'll go about doing that.... But i did try out something... I doubt it's efficient....
    can you clear that out for me a bit. Thank you.

    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <iomanip>
    #include <math.h>
    #include "Account_Querry.h"
    
    using namespace std;
    
    void Account_Querry::setAccountNo(int accountNo) {
        this->accountNo = accountNo;
    }
    
    int Account_Querry::getAccountNo() {
        return accountNo;
    }
    
    void Account_Querry::setFirstName(string firstName) {
        this->firstName = firstName;
    }
    
    string Account_Querry::getFirstName() {
        return firstName;
    }
    
    void Account_Querry::setLastName(string lastName) {
        this->lastName = lastName;
    }
    
    string Account_Querry::getLastName() {
        return lastName;
    }
    
    void Account_Querry::setBalance(double balance) {
        this->balance = balance;
    }
    
    double Account_Querry::getBalance() {
        return balance;
    }
    
    void Account_Querry::read_data()
    {
        int account_number = 0;
        cout<<"\nEnter Account Number: ";
        cin>>account_number;
        cin.ignore();
        setAccountNo(account_number);
    
        string firstName, lastName;
        cout<<"Enter First Name: ";
        getline(cin,firstName,'\n');
        setFirstName(firstName);
    
        cout<<"Enter Last Name: ";
        getline(cin,lastName,'\n');
        setLastName(lastName);
    
        double total_balance = 0;
        cout<<"Enter Balance: ";
        cin>>total_balance;
        setBalance(total_balance);
    }
    
    void Account_Querry::write_rec()
    {
        ofstream outfile;
        outfile.open("record.bank", ofstream::app);
        read_data();
        outfile << getAccountNo() << ' ' << getLastName() << ' ' << getFirstName() << ' ' << getBalance() << endl;
        outfile.close();
    }
    
    void Account_Querry::read_rec()
    {
        ifstream infile;
        infile.open("record.bank");
    
        int nRec = 0;
        string check;
        while(getline(infile,check))
        {
            nRec++;
        }
        cout << "There are " << nRec << " records in the file." << endl;
    
        infile.close();
        infile.open("record.bank");
    
        int empId;
        cout << "Enter ID of the employee whose data you wish to find: ";
        cin >> empId;
    
        int acctNo[nRec], i = 0;
        string fName[nRec];
        string lName[nRec];
        double bal[nRec];
        bool flag = true;
    
        while(infile >> acctNo[i] >> lName[i] >> fName[i] >> bal[i] && flag == true)
        {
            system("cls");
            if(acctNo[i] == empId)
            {
                cout << "Current Information:" << endl;
    
                int lNameLength = lName[i].size();
                int fNameLength = fName[i].size();
                int lengthTotal = fNameLength + lNameLength;
                int balLength = log10(bal[i]);
                int spaces_needed = 7 - balLength;
                cout << "Name";
                for(int y = 1; y < lengthTotal-1; y++)
                {
                    cout << " ";
                }
                cout << "Balance";
                cout << " ";
                cout << "ID#" << endl;
                for(int x = 2; x < lengthTotal + 20; x++)
                {
                    cout << "-";
                }
                cout << ' ' << endl;
    
                cout << fName[i] << ' ' << lName[i] << ' '  <<  bal[i] << string(spaces_needed, ' ') << acctNo[i] << endl;
                cout << endl;
                flag = false;
            }
            else
            {
                cout << "Record Not Found! Try Again...." << endl;
            }
            i++;
        }
        cout << empId << endl;
        cout << acctNo[2] << endl;
        infile.close();
    }
    
    void Account_Querry::edit_rec()
    {
        ifstream infile;
        infile.open("record.bank");
    
        int nRec = 0;
        string check;
        while(getline(infile,check))
        {
            nRec++;
        }
    
        infile.close();
        infile.open("record.bank");
    
        int empId;
        cout << "Enter ID of the employee whose data you wish to edit: ";
        cin >> empId;
    
        int acctNo[nRec], i = 0;
        string fName[nRec];
        string lName[nRec];
        double bal[nRec];
    
        while(infile >> acctNo[i] >> lName[i] >> fName[i] >> bal[i])
        {
            system("cls");
            if(empId == acctNo[i])
            {
                read_data();
                lName[i] = getLastName();
                fName[i] = getFirstName();
                bal[i] = getBalance();
            }
        }
        infile.close();
    
        ofstream outfile;
        outfile.open("record.bank");
        for(int i = 0; i < nRec; i++)
        {
           outfile << acctNo[i] << ' ' << lName[i] << ' ' << fName[i] << ' ' << bal[i] << endl;
        }
        outfile.close();
    }
    
    void Account_Querry::delete_rec()
    {
        
    }
    
    
  4. In Topic: file input output problems

    Posted 8 Jan 2015

    It could be text... I made a few adjustments but i doubt it's efficient.

    I'm still trying to implement the options to:
    1) search for a record
    2) modify a record
    3) delete a record

    Any assistance would be greatly appreciated....



    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <iomanip>
    #include "Account_Querry.h"
    
    using namespace std;
    
    void Account_Querry::setAccountNo(int accountNo) {
        this->accountNo = accountNo;
    }
    
    int Account_Querry::getAccountNo() {
        return accountNo;
    }
    
    void Account_Querry::setFirstName(string firstName) {
        this->firstName = firstName;
    }
    
    string Account_Querry::getFirstName() {
        return firstName;
    }
    
    void Account_Querry::setLastName(string lastName) {
        this->lastName = lastName;
    }
    
    string Account_Querry::getLastName() {
        return lastName;
    }
    
    void Account_Querry::setBalance(double balance) {
        this->balance = balance;
    }
    
    double Account_Querry::getBalance() {
        return balance;
    }
    
    ofstream outfile;
    void Account_Querry::read_data()
    {
        int account_number = 0;
        cout<<"\nEnter Account Number: ";
        cin>>account_number;
        cin.ignore();
        setAccountNo(account_number);
    
        string firstName, lastName;
        cout<<"Enter First Name: ";
        getline(cin,firstName,'\n');
        setFirstName(firstName);
    
        cout<<"Enter Last Name: ";
        getline(cin,lastName,'\n');
        setLastName(lastName);
    
        double total_balance = 0;
        cout<<"Enter Balance: ";
        cin>>total_balance;
        setBalance(total_balance);
        outfile << fixed << showpoint;
        outfile << setprecision(2);
        outfile << setw(10) << getAccountNo() << "       " << setw(14) << getLastName() << "   " << setw(14) << getFirstName() << "    " << setw(12) << getBalance() << '\n';
        outfile.close();
    }
    
    void Account_Querry::write_rec()
    {
        outfile.open("record.bank", ofstream::app);
        read_data();
        outfile.close();
    }
    
    ifstream infile;
    void Account_Querry::show_data()
    {
        string STRING;
        while(!infile.eof()) // To get you all the lines.
        {
            getline(infile,STRING); // Saves the line in STRING.
            cout << STRING << '\n'; // Prints our STRING.
        }
    }
    
    void Account_Querry::read_rec()
    {
        infile.open("record.bank");
        if(!infile)
        {
            cout<<"Error in Opening! File Not Found!!"<<endl;
            return;
        }
    
        cout<<"\n****Data from file****"<<endl;
        show_data();
        infile.close();cout << endl;
    }
    
    void Account_Querry::search_rec()
    {
        infile.open("record.bank");
        string line, search;
        cout << "Please enter the Account Number: ";
        cin >> search;
        for (int curLine = 0; getline(infile, line); curLine++)
        {
            if (line.find(search) != string::npos)
            {
                cout << endl;
                cout << "****************************************************************" << endl;
                cout << "Account Number        Last Name        First Name        Balance" << endl;
                cout << "****************************************************************" << endl;
                cout << '\n' << line << endl;
                cout << endl;
            }
        }
        infile.close();
    }
    
    
    
    
    
  5. In Topic: file input output problems

    Posted 7 Jan 2015

    Ahh i see no wonder Thank You.... Still getting used to this....

    View Postjimblumberg, on 07 January 2015 - 09:38 PM, said:

    Oh, and by the way you can't write() a std::string, and because your class has std::string members you'll need to write() each element separately, you'll need to take extra steps with the std::strings, you'll need to preserve the length information and print the actual data instead.

    Jim


    But, i'm a bit lost right there..... I'm not sure how i'll begin tp take those steps...
    Sorry...

My Information

Member Title:
D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

Comments

Twigz has no profile comments yet. Why not say hello?