Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string> //seems like I can't get the <getline> function to work without initializing <string> header
- using namespace std;
- struct product {
- int ID;
- string name;
- float price;
- };
- void insertionSort(product array[], const int);
- void printArray(product array[], const int);
- void textfilein(product array[], const int);
- void textfileout();
- int main() {
- const int size = 5;
- product p[size] = {{105, "Once again, I made something worthless, by Goemon", 5437.85}, {193, "Time Machine (Prototype)", 2212.12}, {102, "Bamboo Helicam", 176.19}, {106, "Cyalume Saber", 0.20}, {104, "Moad Snake", 4078.39}};
- cout << "--------------------------------------------------------------------------------------------------\n";
- cout << "The products are: \n\n";
- textfilein(p, size); //text file input then output
- textfileout();
- cout << "--------------------------------------------------------------------------------------------------\n";
- cout << "The product after sort are: \n\n";
- insertionSort(p, size); //do insertion sort then text file input then output
- textfilein(p, size);
- textfileout();
- return 0;
- }
- void insertionSort(product arr[], const int n) //insertion sort, a copypasta from kan Gabumpa report
- {
- for (int i = 1; i < n; i++)
- {
- int key = arr[i].ID; //initialize <ID> since amo la an ig cocompare na value dire na an iba na aada <struct>
- product key2 = arr[i]; //initialize another key but this is only for a <key> to hold value to swap later. swapping doesn't work without this
- int j = i - 1;
- // Move elements of arr[0..i-1], //a copypasta comment, just leaves it there for quality purposes
- // that are greater than key, to one
- // position ahead of their
- // current position
- while (j >= 0 && arr[j].ID > key)
- {
- arr[j + 1] = arr[j];
- j = j - 1;
- }
- arr[j + 1] = key2;
- }
- }
- void printArray(product arr[], const int n) //did use this to try if the insertion sort works but ignores after implementing text files databases
- {
- for (int i = 0; i < n; i++) {
- cout << "ID: " << arr[i].ID << "\n";
- cout << "Name: " << arr[i].name << "\n";
- cout << "Price: " << arr[i].price << " PHP \n\n";
- }
- }
- void textfilein(product arr[], const int n) { //for text file to record the values
- fstream tf;
- tf.open("LAF2textfile.txt");
- for (int i = 0; i < n; i++) {
- tf << "ID: " << arr[i].ID << "\n";
- tf << "Name: " << arr[i].name << "\n";
- tf << "Price: " << arr[i].price << " PHP \n\n";
- }
- tf.close();
- }
- void textfileout() { //outputs the values/lines in the text file
- fstream tf;
- string line;
- tf.open("LAF2textfile.txt");
- while (getline(tf, line)) {
- cout << line << "\n";
- }
- tf.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement