1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <chrono>
#include <thread>
void update()
{
std::fstream f; // <--- Better names for these variables would be helpful.
std::fstream t;
int itemnumber{}, availability{}, choose{};
std::string author, title;
// Switch comments for testing. Remove quoted text for run.
std::string searching{ "Harry Potter" };
//std::string searching{ "Hobbit" };
f.open("file.txt", std::ios::in);
if (!f.is_open())
{
std::cout << "\n File did not open.";
std::this_thread::sleep_for(std::chrono::seconds(2)); // Requires header files "chrono" and "thread"
exit(1);
}
t.open("temp.txt", std::ios::app);
if (!t.is_open())
{
std::cout << "\n File did not open.";
std::this_thread::sleep_for(std::chrono::seconds(2)); // Requires header files "chrono" and "thread"
exit(1);
}
// Uncomment next three lines for normal use.
//std::cout << "Enter title for search: ";
////std::cin.get(); // <--- Not needed here. Will cause someone to enter a title twice.
//std::getline(std::cin, searching);
while (f >> itemnumber)
{
f.get(); // <--- Used here to get the space after the number.
std::getline(f, title, ',');
f.get(); // <--- Used here to get the space after the title.
std::getline(f, author, ',');
f >> availability;
// Used for testing. Comment out when not needed.
//std::cout << itemnumber << " " << title << ", " << author << ", " << availability << std::endl;
// This section not needed.
//f >> itemnumber;
////f.get();
//std::getline(f, title, ',');
////f.get();
//std::getline(f, author, ',');
//f >> availability;
if (searching == title)
{
std::cout << "Enter 0 for renting and 1 for returning ";
std::cin >> choose;
if (choose == 0)
availability--;
else if (choose == 1)
availability++;
else
{
std::cout << "Invalid choice ";
//break; // <-- Should not leave the while loop. Leaves the while loop to early. Skips writing to the file.
}
}
else
{
std::cout << "There isnt a book with this name ";
//break; // <--- Leaves the while loop to early. Skips the next statement.
}
// This line writes every input to the "temp.txt" . Is that what you want?
t << itemnumber << " " << title << ", " << author << ", " << availability << std::endl;
// Used for testing. Comment out when not needed.
std::cout << itemnumber << " " << title << ", " << author << ", " << availability << std::endl;
}
t.close();
f.close();
return; // <--- Leaves the function while working on the first half.
// I find it hard to understand the purpose of this section.
t.open("temp.txt", std::ios::in);
f.open("file2.txt", std::ios::out || std::ios::app);
while (!t.eof()); // <---Incorrect see above.
{
// This should look more like what is at the beginning of the while loop.
t >> itemnumber;
getline(t, title, ',');
getline(t, author, ',');
t >> availability;
f << itemnumber << " " << title << ", " << author << ", " << availability << std::endl;
}
f.close();
t.close();
}
|