find errors of my program and fix it please need to read fil

find errors of my program and fix it please. need to read file from pc

create a inventory.txt and put those in it:

My First Book
Mark Lusk
Pearson Publishing
40
45.34
9780316
Brown Family
Mason Victor
Little Brown
36
105.99
1349877
Story of My Life
Norah M Jones
CreateSpace Independent Publishing Platform
20
18
20451

thanks you will thumb up

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

struct book

{

int ISBN;

string Author;

string Title;

string publisher;

int Quantity;

double price;

};

void choice1(book books[], int& size, int MAX_SIZE)

{

ifstream inFile;

inFile.open(\"inventory.txt\");

if (inFile.fail())

cout <<\"file could not open\"<<endl;

string str;

  

while(inFile && size < MAX_SIZE)

{

getline(inFile, str);

books[size].ISBN = atoi(str.c_str());

getline(inFile, books[size].Title);

  

getline(inFile, books[size].Author);

getline(inFile, books[size].publisher);

  

  

  

getline(inFile, str);

books[size].Quantity = atoi(str.c_str());

  

getline(inFile, str);

books[size].price = atoi(str.c_str());

  

getline(inFile, str);

size++;

}

  

cout << \"You have successfully read the file.\" << endl;

inFile.close();

}

void choice2(book books[], int& size, int MAX_SIZE)

{

if(size < MAX_SIZE)

{

string str;

  

cout << \"\ Enter the book ISBN: \";

cin >> books[size].ISBN;

  

cout << \"Enter the author name: \";

cin >> books[size].Author;

  

cout << \"Enter the book tile: \";

cin >> books[size].Title;

cin.get();

  

cout << \"Enter the books quantity: \";

cin >> books[size].Quantity;

  

cout << \"Enter the book price: $\";

cin >> books[size].price;

  

size++;

cout << \"You have successfully inserted an entry.\" << endl;

}

}

void choice3(book books[], int& size)

{

if(size == 0)

cout << \"Array is empty. Read the data first.\" << endl;

else

{

int isbn;

  

cout << \"\ Enter the ISBN of the book: \";

cin >> isbn;

  

for(int i = 0; i < size; i++)

{

if(books[i].ISBN == isbn)

{

int j = i;

while(j < size - 1)

{

books[j] = books[j + 1];

j++;

}

  

size--;

break;

}

}

  

cout << \"You have successfully deleted an entry.\" << endl;

}

}

void choice4(book books[], int size)

{

if(size == 0)

cout << \"Array is empty. Read the data first.\" << endl;

else

{

int isbn;

int option;

int qty;

  

cout << \"\ Enter the ISBN of the book: \";

cin >> isbn;

  

cout << \"1. Increment\" << endl;

cout << \"2. Decrement\" << endl;

cout << \"3. Add New\" << endl;

cout << \"Enter your option: \";

cin >> option;

  

cout << \"Enter the quantity: \";

cin >> qty;

  

for(int i = 0; i < size; i++)

{

if(books[i].ISBN == isbn)

{

if(option == 1)

books[i].Quantity += qty;

else if(option == 2)

{

books[i].Quantity -= qty;

  

if(books[i].Quantity)

books[i].Quantity = 0;

}

else if(option == 3)

books[i].Quantity = qty;

  

break;

}

}

  

cout << \"You have successfully updated the array.\" << endl;

}

}

void choice5(book books[], int size)

{

for(int i = 1; i < size; i++)

{

book current = books[i];

int j = i;

while(j > 0 && (books[j - 1].Title).compare(current.Title) > 0)

{

books[j] = books[j - 1];

j--;

}

books[j] = current;

}

  

if(size != 0)

cout << \"You have successfully sorted the array.\" << endl;

else

cout << \"Array is empty. Read the data first.\" << endl;

}

void choice6(book books[], int size)

{

for(int i = 0; i < size; i++)

{

cout << endl;

cout << \"Book Number: \" << (i + 1) << endl;

cout << \"ISBN: \" << books[i].ISBN << endl;

cout << \"Author: \" << books[i].Author << endl;

cout << \"Title: \" << books[i].Title << endl;

cout << \"Quantity: \" << books[i].Quantity << endl;

cout << \"Price: $\" << books[i].price << endl;

}

  

if(size != 0)

cout << \"You have successfully printed the array.\" << endl;

else

cout << \"Array is empty. Read the file first.\" << endl;

}

void choice7(book books[], int size)

{

ofstream outFile;

outFile.open(\"finalData.dat\");

  

for(int i = 0; i < size; i++)

{

outFile << \"Book Number: \" << (i + 1) << endl;

outFile << \"ISBN: \" << books[i].ISBN << endl;

outFile << \"Author: \" << books[i].Author << endl;

outFile << \"Title: \" << books[i].Title << endl;

outFile << \"Quantity: \" << books[i].Quantity << endl;

outFile << \"Price: $\" << books[i].price << endl << endl;

}

  

if(size != 0)

cout << \"You have successfully printed the array.\" << endl;

else

cout << \"Array is empty. Read the file first.\" << endl;

  

outFile.close();

}

// File: Boookstore.cpp

#include<iostream>

#include\"yuan.h\"

using namespace std;

int main()

{

const int MAX_SIZE=100;

int size = 0;

int choice;

book books[MAX_SIZE];

  

do

{

cout << \"1: Read inventory forn file\" << endl;

cout << \"2: Add an entry\" << endl;

cout << \"3: Delete an entry\" << endl;

cout << \"4: Update an entry\" << endl;

cout << \"5: Sort inventory\" << endl;

cout << \"6: Display Inventory\" << endl;

cout << \"7: Write inventory to file and exit\" << endl;

cout << \"Enter your choice: \";

cin >> choice;

  

switch(choice)

{

case 1:

choice1(books, size, MAX_SIZE);

break;

case 2:

choice2(books, size, MAX_SIZE);

break;

case 3:

choice3(books, size);

break;

case 4:

choice4(books, size);

break;

case 5:

choice5(books, size);

break;

case 6:

choice6(books, size);

break;

  

case 7:

choice7(books, size);

cout << \"Thank you.\" << endl;

break;

default:

cout << \"Invalid choice!\" << endl;

}

cout << endl;

}while(choice != 7);

  

return 0;

}

Solution

Please let me know if you still facing issue/ needed more information:-

=======================================================

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct book
{
   int ISBN;
   string Author;
   string Title;
   string publisher;
   int Quantity;
   double price;

};

void choice1(book books[], int& size, int MAX_SIZE)
{
   ifstream inFile;
   inFile.open(\"inventory.txt\");
   if (inFile.fail())
       cout << \"file could not open\" << endl;
   string str;
   while (inFile && size < MAX_SIZE)
   {
       getline(inFile, books[size].Title);
       getline(inFile, books[size].Author);
       getline(inFile, books[size].publisher);
       getline(inFile, str);
       books[size].Quantity = atoi(str.c_str());
       getline(inFile, str);
       //books[size].price = atoi(str.c_str());
       books[size].price = atol(str.c_str());
       getline(inFile, str);
       books[size].ISBN = atoi(str.c_str());
       size++;

   }

   cout << \"You have successfully read the file.\" << endl;

   inFile.close();

}

void choice2(book books[], int& size, int MAX_SIZE)

{

   if (size < MAX_SIZE)

   {
       string str;
       cout << \"Enter the author name: \";
       cin >> books[size].Author;
       cout << \"Enter the book tile: \";
       cin >> books[size].Title;
       cin.get();
       cout << \"Enter the books quantity: \";
       cin >> books[size].Quantity;
       cout << \"Enter the book price: $\";
       cin >> books[size].price;
       cout << \"\ Enter the book ISBN: \";
       cin >> books[size].ISBN;
       size++;
       cout << \"You have successfully inserted an entry.\" << endl;
}

}

void choice3(book books[], int& size)

{
   if (size == 0)
       cout << \"Array is empty. Read the data first.\" << endl;
   else
   {
       int isbn;
       cout << \"\ Enter the ISBN of the book: \";
       cin >> isbn;
       for (int i = 0; i < size; i++)
       {
           if (books[i].ISBN == isbn)
           {
               int j = i;
               while (j < size - 1)
               {
                   books[j] = books[j + 1];
                   j++;
               }
               size--;
               break;
           }
       }
       cout << \"You have successfully deleted an entry.\" << endl;
   }

}

void choice4(book books[], int size)

{

   if (size == 0)

       cout << \"Array is empty. Read the data first.\" << endl;

   else

   {

       int isbn;

       int option;

       int qty;

       cout << \"\ Enter the ISBN of the book: \";

       cin >> isbn;

       cout << \"1. Increment\" << endl;

       cout << \"2. Decrement\" << endl;

       cout << \"3. Add New\" << endl;

       cout << \"Enter your option: \";

       cin >> option;

       cout << \"Enter the quantity: \";

       cin >> qty;

       for (int i = 0; i < size-1; i++)

       {

           if (books[i].ISBN == isbn)

           {

               if (option == 1)

                   books[i].Quantity += qty;

               else if (option == 2)

               {

                   books[i].Quantity -= qty;

                   if (books[i].Quantity)

                       books[i].Quantity = 0;

               }

               else if (option == 3)

                   books[i].Quantity = qty;

               break;

           }

       }

       cout << \"You have successfully updated the array.\" << endl;

   }

}

void choice5(book books[], int size)

{

   for (int i = 1; i < size-1; i++)

   {

       book current = books[i];

       int j = i;

       while (j > 0 && (books[j - 1].Title).compare(current.Title) > 0)

       {

           books[j] = books[j - 1];

           j--;

       }

       books[j] = current;

   }

   if (size != 0)

       cout << \"You have successfully sorted the array.\" << endl;

   else

       cout << \"Array is empty. Read the data first.\" << endl;

}

void choice6(book books[], int size)

{

   for (int i = 0; i < size-1; i++)

   {

       cout << endl;

       cout << \"Book Number: \" << (i + 1) << endl;

       cout << \"ISBN: \" << books[i].ISBN << endl;

       cout << \"Author: \" << books[i].Author << endl;

       cout << \"Title: \" << books[i].Title << endl;

       cout << \"Quantity: \" << books[i].Quantity << endl;

       cout << \"Price: $\" << books[i].price << endl;

   }

   if (size != 0)

       cout << \"You have successfully printed the array.\" << endl;

   else

       cout << \"Array is empty. Read the file first.\" << endl;

}

void choice7(book books[], int size)

{

   ofstream outFile;

   outFile.open(\"finalData.dat\");

   for (int i = 0; i < size-1; i++)
   {
       outFile << \"Book Number: \" << (i + 1) << endl;

       outFile << \"ISBN: \" << books[i].ISBN << endl;

       outFile << \"Author: \" << books[i].Author << endl;

       outFile << \"Title: \" << books[i].Title << endl;

       outFile << \"Quantity: \" << books[i].Quantity << endl;

       outFile << \"Price: $\" << books[i].price << endl << endl;

   }

   if (size != 0)

       cout << \"You have successfully printed the array.\" << endl;

   else

       cout << \"Array is empty. Read the file first.\" << endl;

   outFile.close();

}

// File: Boookstore.cpp

int main()

{

   const int MAX_SIZE = 100;

   int size = 0;

   int choice;

   book books[MAX_SIZE];

   do

   {

       cout << \"1: Read inventory forn file\" << endl;

       cout << \"2: Add an entry\" << endl;

       cout << \"3: Delete an entry\" << endl;

       cout << \"4: Update an entry\" << endl;

       cout << \"5: Sort inventory\" << endl;

       cout << \"6: Display Inventory\" << endl;

       cout << \"7: Write inventory to file and exit\" << endl;

       cout << \"Enter your choice: \";

       cin >> choice;

       switch (choice)

       {
       case 1:
           choice1(books, size, MAX_SIZE);

           break;

       case 2:

           choice2(books, size, MAX_SIZE);

           break;

       case 3:

           choice3(books, size);

           break;

       case 4:

           choice4(books, size);

           break;

       case 5:

           choice5(books, size);

           break;

       case 6:

           choice6(books, size);

           break;
       case 7:
           choice7(books, size);
           cout << \"Thank you.\" << endl;
           break;

       default:
           cout << \"Invalid choice!\" << endl;

       }
       cout << endl;
   } while (choice != 7);

   return 0;

}


=======================================================

atol --> you need to use when you declared a variable as double

atoi --> for integer

use library : #include <stdlib.h>

==================

While reading file you are not reading file in right direction -->

mean to say you can copy the values which you got from file any where but when you are reading you should assign that particualr line of string or value to particular struct vvalue.

so you missed :-

while(inFile && size < MAX_SIZE)

{

getline(inFile, str); // you have to read at last

books[size].ISBN = atoi(str.c_str());

getline(inFile, books[size].Title);

getline(inFile, books[size].Author);

getline(inFile, books[size].publisher);

getline(inFile, str);

books[size].Quantity = atoi(str.c_str());

  

getline(inFile, str);

books[size].price = atoi(str.c_str());

  

getline(inFile, str); // there is no pointing of giving this getline once again

size++;

}

AND
one more is while writing file you should not give i<size you must give i<size-1 [ this is applicable for all for loops ]

=========================================

Please correct the above fixes and let me know if you still facing issue.
and please mention at what point you are facing issue[like choice 1/2/3/3..etc]

so that i can directly go to it to fix it.

===================================

Thanks

find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40
find errors of my program and fix it please. need to read file from pc create a inventory.txt and put those in it: My First Book Mark Lusk Pearson Publishing 40

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site