Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include "Hardware.h"
- using namespace std;
- int main()
- {
- int ch = 0, count = 0, rNo, qty;
- string fileName, h1, h2, h3, h4, hName;
- double c;
- ifstream inFile;
- ofstream outFile;
- HardwareData hwd[100];
- std::cout << std::endl;
- std::cout << "Enter 1 for opening data file" << std::endl;
- std::cout << "Enter 2 for listing all the records" << std::endl;
- std::cout << "Enter 3 for entering new records" << std::endl;
- std::cout << "Enter 4 to exit" << std::endl;
- std::cout << "Choice: ";
- std::cin >> ch;
- //loop
- while(ch!=4)
- {
- //switch case for choice
- switch(ch)
- {
- case 1:
- std::cout << "Enter File Name: ";
- cin >> fileName;
- inFile.open(fileName);
- if(!inFile)
- {
- //creating data file since it does not exist
- std::cout << "File does not exist, creating the data file." << std::endl;
- outFile.open(fileName);
- outFile << "Record# "<<"HardwareDateName "<<"Quantity "<<"Cost";
- //closing data file
- outFile.close();
- inFile.open(fileName);
- }
- break;
- case 2:
- if(!inFile)
- {
- std::cout << "File not opened yet. Open the file first." << std::endl;
- }
- else
- {
- inFile>>h1>>h2>>h3>>h4;
- std::cout << h1 << "\t" << h2 << "\t" << h3 << "\t" << h4 << std::endl;
- while(!inFile.eof())
- {
- inFile >> rNo >> hName >> qty >> c;
- hwd[count].setRecNo(rNo);
- hwd[count].setHardwareName(hName);
- hwd[count].setQuantity(qty);
- hwd[count].setCost(c);
- count++;
- }
- for (int i = 0; i < count; i++)
- {
- std::cout << hwd[i].getRecNo() << "\t" << hwd[i].getHardwareName() << "\t\t\t" << hwd[i].getQuantity() << "\t\t" << hwd[i].getCost() << std::endl;
- }
- }
- break;
- case 3:
- inFile.close();
- outFile.clear();
- outFile.open(fileName, ios::app);
- cout<<"Enter the part number (0-99, -1 to end input): ";
- cin>>rNo;
- while(rNo!=-1)
- {
- std::cout<<"Enter tool name: ";
- std::cin>>hName;
- std::cout<<"Enter Quantity and Price: ";
- std::cin>>qty>>c;
- outFile<<endl<<rNo<<" "<<hName<<" "<<qty<<" "<<c;
- std::cout<<"Enter the part number (0-99, -1 to end input): ";
- std::cin>>rNo;
- }
- outFile.close();
- inFile.open(fileName);
- break;
- case 4:
- break;
- default:
- std::cout<<"Invalid choice. Enter a valid choice."<<endl;
- break;
- }
- std::cout<<"Choice: ";
- std::cin>>ch;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement