Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6.  
  7. struct purchase
  8. {
  9.     int accountNumber;
  10.     string firstName;
  11.     string lastName;
  12.     string itemName;
  13.     double cost;
  14.     string itemDescription;
  15.     int Quantity;
  16.     purchase* next; // link
  17. };
  18.  
  19. purchase *start_ptr = NULL; //permanently points to the start of the list.
  20.  
  21. int main()
  22.  
  23. {
  24.     ifstream fin;
  25.     fin.open("CustomerPurchases.txt");
  26.     if(!fin.good()) throw "I/O error";
  27.  
  28.     purchase* head = 0;
  29.     purchase *Customers[6]; //Array of pointers
  30.     purchase *aPurchase, *temp2; //temporary pointers
  31.  
  32.     while (!fin.good())
  33.     {
  34.  
  35.         aPurchase = new purchase;
  36.          //declaring a new pointer and setting it to point at a new purchase object that you just made
  37.         getline(fin, aPurchase -> accountNumber);
  38.  
  39.         fin >> aPurchase->firstName;
  40.         fin.ignore(1000, 10);
  41.         fin >> aPurchase->lastName;
  42.         fin.ignore(1000, 10);
  43.         fin >> aPurchase->itemName;
  44.         fin.ignore(1000, 10);
  45.         fin >> aPurchase->cost;
  46.         fin.ignore(1000, 10);
  47.         fin >> aPurchase->itemDescription;
  48.         fin.ignore(1000, 10);
  49.         fin >> aPurchase->Quantity;
  50.         fin.ignore(1000, 10);
  51.  
  52.         aPurchase->next = NULL; /*sets the pointer from this node to the next to NULL indicating that it will be
  53.                                   the LAST NODE when inserted into the list*/
  54.  
  55.  
  56.      if (start_ptr == NULL)
  57.          start_ptr = aPurchase; //set up a link to this node.
  58.      else
  59.        { temp2 = start_ptr;
  60.          // We know this is not NULL - list not empty!
  61.          while (temp2->next != NULL)                          //This loop terminates when pointing to null
  62.            {  temp2 = temp2->next;
  63.               // Move to next link in chain
  64.            }
  65.          temp2->next = aPurchase;
  66.        }
  67.   } //while
  68.  
  69.     //-------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement