Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <iomanip>
- #include <windows.h>
- #include <fstream>
- #include <string>
- using namespace std;
- struct theState
- {
- string name, capital;
- int area, population;
- };
- theState state[100];
- int countRow = 0;
- void outputScreen()
- {
- cout << " Structure The State" << endl;
- cout << " _________________________________________________________" << endl;
- cout << " |№ | Name | Capital | Population | Area | " << endl;
- cout << " |--|------------|------------|------------|-------------|" << endl;
- for (int i = 0; i < countRow; i++)
- cout << left << " |" << setw(2) << i + 1 << "|" << setw(12) << state[i].name << "|" << setw(12)
- << state[i].capital << "|" << setw(12) << state[i].population << "|" << setw(13) << state[i].area << "|" << endl;
- cout << " _________________________________________________________" << endl;
- }
- void inputFile()
- {
- ifstream input;
- input.open("Input.txt", ios::in);
- if (input.fail()) {
- cout << "Could not open file" << endl;
- system("pause");
- }
- do
- {
- input >> state[countRow].name >> state[countRow].capital >> state[countRow].population >> state[countRow].area;
- countRow++;
- } while (!input.eof());
- input.close();
- cout << "\nFile read out. Number of items = " << countRow << endl;
- outputScreen();
- }
- void outputFile()
- {
- ofstream output;
- output.open("Output.txt", ios::out);
- if (output.fail())
- {
- cout << "Could not create file" << endl;
- system("pause");
- }
- for (int i = 0; i<countRow; i++)
- {
- output << setw(15) << state[i].name << " " << setw(15) << state[i].capital << " " << setw(15) << state[i].population << " " << setw(15) << state[i].area;
- if (i != countRow - 1)
- output << endl;
- if (output.fail())
- break;
- }
- output.close();
- cout << "File created" << endl;
- }
- void addState()
- {
- int countAddRow;
- int tmpArea;
- cout << "How many lines to add: ";
- cin >> countAddRow;
- cout << "Enter area: ";
- cin >> tmpArea;
- for (int j = 0; j<countAddRow; j++)
- {
- cout << "-Enter State Data-" << endl;
- cout << "Name: ";
- cin >> state[countRow].name;
- cout << "Capital: ";
- cin >> state[countRow].capital;
- cout << "Population: ";
- cin >> state[countRow].population;
- state[countRow].area = tmpArea;
- countRow++;
- }
- cout << countAddRow << " state with area " << tmpArea << " added" << endl;
- outputScreen();
- cout << "Saving file" << endl;
- outputFile();
- }
- void deleteState()
- {
- int findPopulation;
- theState tmp;
- cout << "Enter the population: ";
- cin >> findPopulation;
- for (int i = 0; i < countRow; i++)
- {
- if (findPopulation > state[i].population)
- {
- cout << findPopulation << ">" << state[i].population << " - delete" << endl;
- for (int ii = i; ii <= countRow; ii++)
- state[ii] = state[ii + 1];
- i--;
- countRow--;
- }
- }
- outputScreen();
- cout << "Saving file" << endl;
- outputFile();
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- int option;
- cout << "Structure The State" << endl;
- while (1)
- {
- cout << "1-Input from a txt file" << endl;
- cout << "2-View data" << endl;
- cout << "3-Saving to txt file" << endl;
- cout << "4-Adding N elements with the number K" << endl;
- cout << "5-Delete all items that have fewer than the specified number" << endl;
- cout << "6-Exit" << endl;
- cout << "Your choice (1-6): ";
- cin >> option;
- switch (option)
- {
- case 1: inputFile(); break;
- case 2: outputScreen(); break;
- case 3: outputFile(); break;
- case 4: addState(); break;
- case 5: deleteState(); break;
- case 6: exit(0);
- default: cout << "Invalid value entered" << endl;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment