Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "Hardware.h"
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int ch = 0, count = 0, rNo, qty;
  11.     string fileName, h1, h2, h3, h4, hName;
  12.     double c;
  13.    
  14.     ifstream inFile;
  15.     ofstream outFile;
  16.     HardwareData hwd[100];
  17.    
  18.     std::cout << std::endl;
  19.     std::cout << "Enter 1 for opening data file" << std::endl;
  20.     std::cout << "Enter 2 for listing all the records" << std::endl;
  21.     std::cout << "Enter 3 for entering new records" << std::endl;
  22.     std::cout << "Enter 4 to exit" << std::endl;
  23.     std::cout << "Choice: ";
  24.     std::cin >> ch;
  25.    
  26.     //loop
  27.     while(ch!=4)
  28.     {
  29.         //switch case for choice
  30.         switch(ch)
  31.         {
  32.             case 1:
  33.                 std::cout << "Enter File Name: ";
  34.                 cin >> fileName;
  35.                 inFile.open(fileName);
  36.                 if(!inFile)
  37.                 {
  38.                     //creating data file since it does not exist
  39.                     std::cout << "File does not exist, creating the data file." << std::endl;
  40.                     outFile.open(fileName);
  41.                     outFile << "Record# "<<"HardwareDateName "<<"Quantity "<<"Cost";
  42.                    
  43.                     //closing data file
  44.                     outFile.close();
  45.                         inFile.open(fileName);
  46.                 }
  47.                 break;
  48.             case 2:
  49.                 if(!inFile)
  50.                 {
  51.                     std::cout << "File not opened yet. Open the file first." << std::endl;
  52.                 }
  53.                 else
  54.                 {
  55.                     inFile>>h1>>h2>>h3>>h4;
  56.                     std::cout << h1 << "\t" << h2 << "\t" << h3 << "\t" << h4 << std::endl;
  57.                    
  58.                     while(!inFile.eof())
  59.                     {
  60.                         inFile >> rNo >> hName >> qty >> c;
  61.                         hwd[count].setRecNo(rNo);
  62.                         hwd[count].setHardwareName(hName);
  63.                         hwd[count].setQuantity(qty);
  64.                         hwd[count].setCost(c);
  65.                         count++;
  66.                     }
  67.                     for (int i = 0; i < count; i++)
  68.                     {
  69.                         std::cout << hwd[i].getRecNo() << "\t" << hwd[i].getHardwareName() << "\t\t\t" << hwd[i].getQuantity() << "\t\t" << hwd[i].getCost() << std::endl;
  70.                     }
  71.                 }
  72.                 break;
  73.             case 3:
  74.                 inFile.close();
  75.                 outFile.clear();
  76.                 outFile.open(fileName, ios::app);
  77.                 cout<<"Enter the part number (0-99, -1 to end input): ";
  78.                 cin>>rNo;
  79.  
  80.             while(rNo!=-1)
  81.               {
  82.                    std::cout<<"Enter tool name: ";
  83.                    std::cin>>hName;
  84.                    std::cout<<"Enter Quantity and Price: ";
  85.                    std::cin>>qty>>c;
  86.                    outFile<<endl<<rNo<<" "<<hName<<" "<<qty<<" "<<c;
  87.                    std::cout<<"Enter the part number (0-99, -1 to end input): ";
  88.                    std::cin>>rNo;
  89.               }
  90.               outFile.close();
  91.               inFile.open(fileName);
  92.               break;
  93.           case 4:
  94.               break;
  95.           default:
  96.               std::cout<<"Invalid choice. Enter a valid choice."<<endl;
  97.               break;
  98.           }
  99.           std::cout<<"Choice: ";
  100.           std::cin>>ch;
  101.      }
  102.         return 0;  
  103.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement