title: Why does adding code break what was working even if it isn't executed?
body: <p>Function 5:<br>
  The Display Sales History Function.. It is just displaying the last entry twice...</p>
  
  <p>Function 1:<br>
  The 'Add Book' Feature.. It was actually working alright until I added the 'Edit Book' and 'Add Sales' Features. Now it writes something to the file("Books.txt"); that none of the other functions identify. Earlier,i.e. 
  when 'Edit Book' and 'Add Sales' weren't coded and structure Sale not declared,
  'Search Book' could work properly with the entry done by 'Add Book'.</p>
  
  <p>((This is what I found in BOOKS.txt when I only coded functions 1 and 2 and did a test run:
  {{Mein Kampf ÿu)&lt;èÿX  0@ Ÿ0  :òÿ2  XThe Accidental Prime Minister Ÿ0  :òÿ&lt;  ^Quantico ental Prime Minister Ÿ0  :òÿ  ÂSurely You Are Joking, Mr. Feynman  X0-  ô}}</p>
  
  <p>If you paste this in Books.txt, all features except 'Add' work allright))</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;fstream.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;dos.h&gt;
  struct Sale {
      char costumer[40], name[40];
      int quant, pric, netprice;
  };
  class Book {
  public:
      char name[40];
      int qty, code, price;
      void Add()
      {
          cout &lt;&lt; "\n Enter Name Of New Book\n";
          gets(name);
          cout &lt;&lt; "\n Enter book code:";
          cin &gt;&gt; code;
          cout &lt;&lt; "\n Enter book price:";
          cin &gt;&gt; price;
          cout &lt;&lt; "\n Enter book quantity:";
          cin &gt;&gt; qty;
      }
      void Display()
      {
          cout &lt;&lt; "\n Name:";
          puts(name);
          cout &lt;&lt; "Code:" &lt;&lt; code;
          cout &lt;&lt; "\nPrice:" &lt;&lt; price;
          cout &lt;&lt; "\nQuantity Available:" &lt;&lt; qty;
      }
      void Modify();
      Sale Sell(int q)
      {
          Sale sale;
          sale.pric = price;
          qty -= q;
          sale.quant = q;
          sale.netprice = q * price;
          cout &lt;&lt; "\n Enter costumer name\n";
          gets(sale.costumer);
          strcpy(sale.name, name);
          return sale;
      }
  };
  void Book::Modify()
  {
      Display();
      char ch = 'n';
      do {
          cout &lt;&lt; "\n Enter detail to modify \n";
          cout &lt;&lt; "1.NAME\t2.QUANTITY\t3.CODE\t4.PRICE\n";
          int mod;
          cin &gt;&gt; mod;
          switch (mod) {
          case 1:
              cout &lt;&lt; "\nEnter new Name\n";
              gets(name);
              break;
          case 2:
              cout &lt;&lt; "\nEnter new available quantity\n";
              cin &gt;&gt; qty;
              break;
          case 3:
              cout &lt;&lt; "\nEnter changed CODE\n";
              cin &gt;&gt; code;
              break;
          case 4:
              cout &lt;&lt; "\nEnter updated PRICE\n";
              cin &gt;&gt; price;
              break;
          }
          Display();
          cout &lt;&lt; "\nWant to edit more? (y/n)\n";
          ch = getch();
      } while (ch == 'y' || ch == 'Y');
  }
  void Print(Sale s)
  {
      cout &lt;&lt; "\nCostumer Name:\t";
      puts(s.costumer);
      cout &lt;&lt; "Book Name:\t";
      puts(s.name);
      cout &lt;&lt; "\nQuantity(units):" &lt;&lt; s.quant &lt;&lt; "\tPrice per pice:" &lt;&lt; s.pric &lt;&lt; "\n\t Net Sale:" &lt;&lt; s.netprice;
  }
  void MScreen()
  {
      clrscr();
      cout &lt;&lt; "\tTHE BOOK STORE MANAGEMENT SOFTWARE\n";
      cout &lt;&lt; "\tPLEASE CHOOSE AN OPTION";
      cout &lt;&lt; "\n1.ADD NEW BOOK\n2. SEARCH FOR A BOOK\n3. EDIT BOOK DETAILS\n 4.ADD NEW SALES\n5. DISPLAY SALES HISTORY\n";
  }
  void main()
  {
      char ch = 'n';
      while (ch == 'n' || ch == 'N') {
          MScreen();
          int i;
          cin &gt;&gt; i;
          clrscr();
          switch (i) {
          case 1:
              Book a;
              a.Add();
              ofstream obj1("BOOKS.txt", ios::app);
              obj1.write((char*)&amp;a, sizeof(Book));
              obj1.close();
              break;
          case 2:
              Book b;
              ifstream obj2("BOOKS.txt");
              obj2.seekg(0);
              cout &lt;&lt; "Enter Book Code:";
              long int x;
              cin &gt;&gt; x;
              while (!obj2.eof()) {
                  obj2.read((char*)&amp;b, sizeof(Book));
                  if (b.code == x) {
                      b.Display();
                      obj2.close();
                      break;
                  }
              }
              break;
          case 3:
              Book c;
              cout &lt;&lt; "\n Enter Code of book to be modified\t";
              int y;
              cin &gt;&gt; y;
              fstream obj3("BOOKS.txt", ios::in | ios::out | ios::ate);
              long int pos;
              obj3.seekg(0);
              while (!obj3.eof()) {
                  pos = obj3.tellg();
                  obj3.read((char*)&amp;c, sizeof(Book));
                  if (c.code == y) {
                      c.Modify();
                      obj3.seekp(pos);
                      obj3.write((char*)&amp;c, sizeof(Book));
                      break;
                  }
              }
              cout &lt;&lt; "\nDATA MODIFIED\n";
              break;
          case 4:
              char ch1 = 'n';
              do {
                  Book d;
                  cout &lt;&lt; "\n Enter the book code \t";
                  int z;
                  cin &gt;&gt; z;
                  fstream obj4("BOOKS.txt", ios::in | ios::out | ios::ate);
                  long int pos;
                  obj4.seekg(0);
                  int found = 0;
                  while (!obj4.eof()) {
                      pos = obj4.tellg();
                      obj4.read((char*)&amp;d, sizeof(Book));
                      if (d.code == z) {
                          found = 1;
                          break;
                      }
                  }
                  if (found == 0) {
                      cout &lt;&lt; "\nIncorrect Code\t Aborting";
                      break;
                  }
                  else {
                      d.Display();
                      cout &lt;&lt; "\nEnter Sales Quantity:\t";
                      int q;
                      cin &gt;&gt; q;
                      if (q &gt; d.qty) {
                          cout &lt;&lt; "\n Can't Sell. Aborting ";
                          break;
                      }
                      else {
                          Sale newsale;
                          newsale = d.Sell(q);
                          obj4.seekp(pos);
                          obj4.write((char*)&amp;d, sizeof(Book));
                          obj4.close();
                          ofstream obj5("Sales.txt", ios::app);
                          obj5.write((char*)&amp;newsale, sizeof(newsale));
                          obj5.close();
                          cout &lt;&lt; "\nSALE SUCCESSFUL\n";
                          sleep(2);
                      }
                  }
                  cout &lt;&lt; "\n Add more sale? (y/n)";
                  cin &gt;&gt; ch1;
              } while (ch1 == 'y' || ch1 == 'Y');
              break;
          case 5:
              cout &lt;&lt; "\nPlease enter SALE password:";
              char pass[40];
              gets(pass);
              if (strcmp(pass, "creationbydhruvarora\n")) {
                  clrscr();
                  ifstream obj6("Sales.txt");
                  obj6.seekg(0);
                  Sale readsale;
                  int net = 0;
                  while (!obj6.eof()) {
                      obj6.read((char*)&amp;readsale, sizeof(Sale));
                      net += readsale.netprice;
                      Print(readsale);
                      sleep(1);
                  }
                  cout &lt;&lt; "\n END OF SALES\n TODAY NET SALE:" &lt;&lt; net;
                  break;
              }
              else {
                  cout &lt;&lt; "\nIncorrect Password\n ABORTING";
                  break;
              }
          }
          getch();
          clrscr();
          cout &lt;&lt; "Press Any Key To QUIT. To Go To Main Menu Press 'n'\n";
          ch = getch();
      }
  }
  </code></pre>
  
  <p>I do not understand what this is. A piece of code added to some code is disrupting working w/o being executed.</p>