Advertisement
Xom9ik

Lab_10/8 var (IIl semester)

Dec 3rd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. //Lab_10_var8
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <windows.h>
  6. #include <fstream>
  7. #include <string>
  8. using namespace std;
  9.  
  10. struct Buyer
  11. {
  12.     string name, surname, address;
  13.     string phone, card;
  14. };
  15. Buyer x[100];
  16. int countRow = 0;
  17. void outputScreen()
  18. {
  19.     cout << " Structure Buyer" << endl;
  20.     cout << " __________________________________________________________________________" << endl;
  21.     cout << " |№ |    Name    |  Surname   |   Address  |    Phone    |      Card      | " << endl;
  22.     cout << " |--|------------|------------|------------|-------------|----------------|" << endl;
  23.     for (int i = 0; i < countRow; i++)
  24.     {
  25.         cout << left << " |" << setw(2) << i + 1 << "|" << setw(12) << x[i].name << "|" << setw(12)
  26.             << x[i].surname << "|" << setw(12) << x[i].address << "|" << setw(13) << x[i].phone
  27.             << "|" << setw(16) << x[i].card << "|" << endl;
  28.     }
  29.     cout << " __________________________________________________________________________" << endl;
  30. }
  31. void inputFile()
  32. {
  33.     ifstream input;
  34.     input.open("Input.txt", ios::in);
  35.     if (input.fail()) {
  36.         cout << "Could not open file" << endl;
  37.         system("pause");
  38.     }
  39.     do
  40.     {
  41.         input >> x[countRow].name >> x[countRow].surname >> x[countRow].address >> x[countRow].phone >> x[countRow].card;
  42.         countRow++;
  43.     } while (!input.eof());
  44.     input.close();
  45.     cout << "\nFile read out. Number of items = " << countRow << endl;
  46.     outputScreen();
  47. }
  48.  
  49. void outputFile()
  50. {
  51.     ofstream output;
  52.     output.open("Output.txt", ios::out);
  53.     if (output.fail())
  54.     {
  55.         cout << "Could not create file" << endl;
  56.         system("pause");
  57.     }
  58.     for (int i = 0; i<countRow; i++)
  59.     {
  60.         output << setw(10) << x[i].surname << " " << setw(10) << x[i].name << " " << setw(10) << x[i].address << " " << setw(11) << x[i].phone << " " << setw(11) << x[i].card;
  61.         if (i != countRow - 1)
  62.             output << endl;
  63.         if (output.fail())
  64.             break;
  65.     }
  66.     output.close();
  67.     cout << "File created" << endl;
  68. }
  69.  
  70. void addBuyer()
  71. {
  72.     int countAddRow;
  73.     string tmpNumber;
  74.     cout << "How many lines to add: ";
  75.     cin >> countAddRow;
  76.     cout << "Enter card number: ";
  77.     cin >> tmpNumber;
  78.     for (int j = 0; j<countAddRow; j++)
  79.     {
  80.         cout << "-Enter Vehicle Owner Data-" << endl;
  81.         cout << "Surname: ";
  82.         cin >> x[countRow].surname;
  83.         cout << "Name: ";
  84.         cin >> x[countRow].name;
  85.         cout << "Address: ";
  86.         cin >> x[countRow].address;
  87.         cout << "Phone: ";
  88.         cin >> x[countRow].phone;
  89.         x[countRow].card = tmpNumber;
  90.         countRow++;
  91.     }
  92.     cout << countAddRow << " Byer with number " << tmpNumber << " added" << endl;
  93.     outputScreen();
  94. }
  95.  
  96.  
  97. void deleteBuyer()
  98. {
  99.     int colvo;
  100.     cout << "Enter the number of items to remove from the beginning of the list: ";
  101.     cin >> colvo;
  102.     int ptr = 0;
  103.     for (int i = colvo; i < countRow; i++)
  104.     {
  105.         x[ptr] = x[i];
  106.         ptr++;
  107.     }
  108.     countRow -= colvo;
  109.     cout << colvo << " items deleted" << endl;
  110.     outputScreen();
  111. }
  112.  
  113. int main()
  114. {
  115.     SetConsoleCP(1251);
  116.     SetConsoleOutputCP(1251);
  117.     setlocale(LC_ALL, 0);
  118.     int option;
  119.     cout << "Structure Buyer" << endl;
  120.     while (1)
  121.     {
  122.         cout << "1-Input from a text file" << endl;
  123.         cout << "2-View data" << endl;
  124.         cout << "3-Saving to text file" << endl;
  125.         cout << "4-Adding N elements with the number K" << endl;
  126.         cout << "5-Deleting an item with a specified number" << endl;
  127.         cout << "6-Exit" << endl;
  128.         cout << "Your choice (1-6): ";
  129.         cin >> option;
  130.         switch (option)
  131.         {
  132.         case 1: inputFile(); break;
  133.         case 2: outputScreen(); break;
  134.         case 3: outputFile(); break;
  135.         case 4: addBuyer(); break;
  136.         case 5: deleteBuyer(); break;
  137.         case 6: exit(0);
  138.         default: cout << "Invalid value entered" << endl;
  139.             break;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement