Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

In the below code there is an error at the getline function..I am not able to resolve it kindly help

0 votes
asked Aug 5, 2018 by anonymous
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class prod
{
    public:
    string prodname;
    int prodid;
    double amount;
    int seqno;
    prod* next;
    prod(string, int, double, int, prod*);
};

prod::prod(string tempname,int tempid, double tempamount, int seq_no,  prod* tempNext)
{
    prodname=tempname;
    prodid=tempid;
    amount=tempamount;
    seqno=seq_no;
    next=tempNext;
}

typedef prod* prodPtr;

void getline(istream &stream, string &str, char delimiter)
{    char temp[500];

    stream.get(temp, 500, delimiter);
    stream.ignore(500, delimiter);
    str = temp;
}

void getline(istream &stream, int &num, char delimiter)
{    int temp;

    stream >> temp;
    stream.ignore(500, delimiter);
    num= temp;
}

void readFile(prodPtr &root);
void insert (prodPtr &root);
void delprod(prodPtr &root);
prodPtr locateNode(prodPtr temp, string titl);
void searchname(prodPtr temp);
void printList(prodPtr temp);
void saveFile(prodPtr temp);
int countNodes(prodPtr temp);

void readFile(prodPtr &root)
{
    int numprod,prodi,sqn;
    string prodn;
    double prodamt;
    
    ifstream infile ("PRODUCTS.txt", ios::in);
    infile >> numprod;
    infile.ignore(500,'\n');
    for (int count = 0; count < numprod; count++)
    {
        getline(infile,prodn, '\n');
        getline(infile, prodi, '\n');
        getline(infile, prodamt, '\n');
        getline(infile,sqn, '\n');

        root = new prod (prodn,prodi, prodamt, sqn, root);
    }
}

void insert (prodPtr &root)
{
    int numprod,prodi,sqn;
    string prodn;
    double prodamt;

    cout << "PRODUCT NAME:\t\t\t";
    cin.ignore(500,'\n');
    getline(cin, prodn, '\n');
    cout << "PRODUCT ID:\t\t\t";
    getline(cin, prodi, '\n');
    cout << "PRODUCT AMOUNT:\t\t";
    getline(cin,prodamt, '\n');
    cout << "PRODUCT SEQ NO:\t\t\t";
    getline(cin,sqn, '\n');

    root = new prod (prodn,prodi, prodamt, sqn, root);
}

void searchname(prodPtr temp)
{
    string prodn;

    cout << "PRODUCT NAME PLEASE:\t\t\t";
    cin.ignore(500,'\n');
    getline(cin, prodn, '\n');

    while (temp != NULL)
    {
        if (prodn == temp->prodname)
        {
            cout << temp->prodname << "\n";
            cout << temp->prodid << "\n";
            cout << temp->amount<< "\n";
            cout << temp->seqno << "\n";
           
        }
        temp = temp->next;
    }
    cout << "\n";
}

void printList(prodPtr temp)
{
    while (temp != NULL)
    {
        cout << temp->prodname << "\n";
            cout << temp->prodid << "\n";
            cout << temp->amount << "\n";
            cout << temp->seqno << "\n";
        temp = temp->next;
    }
    cout << "\n";
}

void saveFile(prodPtr temp)
{
    int count = countNodes(temp);
    ofstream outFile("Input_records.txt",ios::out);

    outFile << count << "\n";
    while (temp != NULL)
    {
        outFile << temp->prodname<< "\n";
        outFile << temp->prodid << "\n";
        outFile << temp->amount << "\n";
        outFile << temp->seqno << "\n";
     
        temp = temp->next;
    }
    cout << "\n";
}

int countNodes(prodPtr temp)
{
    int countB = 0;
    while (temp != NULL)
    {
        countB++;
        temp = temp->next;
    }
    return countB;
}
int main()
{
    int choice;
    prodPtr root = NULL;
    readFile(root);

    do
    {
        cout << "\t\t<<=========================>>";
        cout << "\n\t\t>>LIBRARY MANAGEMENT SYSTEM<<";
        cout << "\n\t\t<<=========================>>";
        cout << "\n\nMenu: Select your option\n\n";
        cout << "(1) Add a product to the list\n";
        cout << "(2) Delete by produvt name\n";
        cout << "(3) Search a product by name.\n";
        cout << "(4) List all products\n";
        cout << "(5) Quit.\n\n";
        cout << "Enter your choice ---> ";

        cin >> choice;

        if (1 <= choice && choice <= 4)
        {
            switch (choice)
            {
            case 1:
                insert(root);
                break;
            case 2:
                delprod(root);
                break;
            
            case 3:
                searchname(root);
                break;
            case 4:
                printList(root);
                break;
            default:
                cout << "Invalid choice.  Enter again.\n\n";
                break;
            }
        }
    }
    while (choice != 5);
    saveFile(root);
    return 0;
}

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...