Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a cpp program that I need help debugging. So, whats happening is that I r

ID: 3885758 • Letter: I

Question

I have a cpp program that I need help debugging. So, whats happening is that I run the program and it doesn't formatt the inventory.txt file correctly and if I add more items it overwrites the old items. Please, help me debugg this as its getting really frustrating.

HERE IS THE PROGRAM DESIRED PURPOSE:

You are the owner of a new store and need to keep track of your inventory. Since you are a seasoned C++ programmer, you are going to write a C++ program to perform this function.

Define a structure that will hold a a part number (integer), description of the item (string), a quantity (integer), and price per item (float), and a total cost.

Your program is to check to see if the file called “inventory.txt” already exists and if it does, read it into an array of structures. Otherwise, it will create a new file. The program should have a menu that asks the user to 1) print parts 2) enter a new part 3) modify a part 4) print inventory total 4) exit the program. If the user enters 1, all of the parts will be printed to the screen. If the user enters 2, the program will ask the user for the description, quantity, and price per item and calculate the total cost. If the user selects 3, the program should ask the user for the part number to be modified and all of the new information for that part. If the user selects 4, you are to print the total cost of the inventory items to the screen. When the user selects 5 to exit the program, you are to write each record to the text file “inventory.txt”.

Structure member definitions:

part number is an integer that can hold part numbers from 1 to 100 – generated by your program.

item is a string that may have spaces

quantity is an integer that will contain how many of the part is in inventory

unitprice will be a float that will contain the cost per item

totCost will be a float that will hold the total cost of the item which the program will calculate

Requirements:

Create an array of structures containing the structure members as defined above

The program needs to be able to handle data for up to 100 parts.

Read the data from the file “inventory.txt” and save each part record in a structure in your array.

The part name should be able to contain spaces.

HERE IS WHAT IS EXPECTED OF THE OUTPUT:

Sample output if 1 selected

1 hammer 2 $ 10.00 $ 20.00

2 drill press 1 $100.00 $100.00

Sample input if 2 selected

Enter item: block sander

Enter quantity: 3

Enter cost: $50.00

Sample input if 3 selected

Enter part to modify: 1

1 hammer 2 $ 10.00 $ 20.00

Enter item: hammer

Enter quantity: 3

Enter cost: $10.00

Sample output if 4 selected

Total cost of inventory: $290

#########################

__CODE__STARTS__HERE__

#########################

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

struct inventory

{

int prtNum, quantity;

string description;

float unitPrice, totCost;

};

int main(void)

{

  

int choice, part, cost, quant, n = 30;

string item;

float tot=0;

ofstream outfile;

ifstream in;

bool flag = true;

while (flag)

{

cout<<" 1. PRINT OUT INVENTORY ITEMS ";

cout<<"2. ENTER A NEW PART ";

cout<<"3. MODIFY A PART ";

cout<<"4. DISPLAY INVENTORY TOTAL VALUE IN $ ";

cout<<"5. EXIT ";

cout << " Enter your choice :";

cin >> choice;

  

inventory st[100];

  

switch (choice)

{

case 1:

in.open("inventory.txt");

if (in.is_open())

{

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

{

in >> st[i].prtNum >> st[i].description

>> st[i].quantity >> st[i].unitPrice >> st[i].totCost;

}

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

{

cout << st[i].prtNum << " ";

cout << st[i].description << " ";

cout << st[i].quantity << " ";

cout << "$" << st[i].unitPrice << " ";

cout << "$" << st[i].totCost << " " << endl;

}

in.close();

}

else

{

cout << "Failed to open the file inevntory.txt" << endl;

}

break;

  

case 2:

cout << " Enter item: ";

cin.ignore();

getline (cin, item);

cout << " Enter qunatity: ";

cin >> quant;

cout << " Enter cost: ";

cin >> cost;

n+=1;

  

outfile.open("inventory.txt", ios::trunc);

if (outfile.is_open())

{

outfile <<" "<< n <<" "<< item <<" "

<< quant <<" "<< cost <<" "<< quant*cost;

  

st[n].prtNum = n;

st[n].description = item;

st[n].quantity = quant;

st[n].unitPrice = cost;

st[n].totCost = cost*quant;

outfile.close();

}

else

{

cout << "Failed to open file inventory.txt"<<endl;

}

break;

  

case 3:

in.open("inventory.txt");

if (in.is_open())

{

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

{

in >> st[i].prtNum >> st[i].description

>> st[i].quantity >> st[i].unitPrice >> st[i].totCost;

}

}

cout << " Enter part to modify: ";

cin >> part;

cout << st[part-1].prtNum << " " <<

st[part-1].description << " " << st[part-1].quantity <<

" " << st[part-1].unitPrice << " " << st[part-1].totCost;

cout << " Enter item: ";

cin.ignore();

getline (cin, item);

cout << " ENter Quantity: ";

cin >> quant;

cout << " Enter Cost:$ ";

cin >> cost;

  

st [part-1].description = item;

st [part-1].quantity = quant;

st [part-1].unitPrice = cost;

st [part-1].totCost = quant*cost;

  

outfile.open("inventory.txt");

if (outfile.is_open())

{

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

{

outfile << st[i].prtNum << " ";

outfile << st[i].description << " ";

outfile << st[i].quantity << " ";

outfile << "$" << st[i].unitPrice << " ";

outfile << "$" << st[i].totCost << " ";

outfile << " ";

}

outfile.close();

}

else if (!in.good())

{

cout << " File doesn't exist";

}

break;

  

case 4:

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

{

tot+= st[i].totCost;

}

  

cout << "Total cost of inventory: $"<<tot<< endl;

break;

  

case 5:

flag = false;

break;

}

}

  

return 0;

  

}

Explanation / Answer

Hi

I have modified the code and it is working fine now. Please find the attached updated code below.


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct inventory
{
int prtNum, quantity;
string description;
float unitPrice, totCost;
};

int main(void)
{
  
int choice, part, cost, quant, n = 0;
string item;
float tot=0;
ofstream outfile;
ifstream in;
bool flag = true;
inventory st[100];

while (flag)
{
cout<<" 1. PRINT OUT INVENTORY ITEMS ";
cout<<"2. ENTER A NEW PART ";
cout<<"3. MODIFY A PART ";
cout<<"4. DISPLAY INVENTORY TOTAL VALUE IN $ ";
cout<<"5. EXIT ";
cout << " Enter your choice :";
cin >> choice;
  

  
switch (choice)
{
case 1:
in.open("inventory.txt");
if (in.is_open())
{
for (int i = 0; i < n; i++)
{
in >> st[i].prtNum >> st[i].description
>> st[i].quantity >> st[i].unitPrice >> st[i].totCost;
}
for (int i = 0; i < n; i++)
{
cout << st[i].prtNum << " ";
cout << st[i].description << " ";
cout << st[i].quantity << " ";
cout << "$" << st[i].unitPrice << " ";
cout << "$" << st[i].totCost << " " << endl;
}
in.close();
}
else
{
cout << "Failed to open the file inevntory.txt" << endl;
}
break;
  
case 2:
cout << " Enter item: ";
cin.ignore();
getline (cin, item);
cout << " Enter qunatity: ";
cin >> quant;
cout << " Enter cost: ";
cin >> cost;

  
outfile.open("inventory.txt", std::ios_base::app);

if (outfile.is_open())
{
outfile <<" "<< n <<" "<< item <<" "
<< quant <<" "<< cost <<" "<< quant*cost;
  
st[n].prtNum = (n+1);
st[n].description = item;
st[n].quantity = quant;
st[n].unitPrice = cost;
st[n].totCost = cost*quant;
outfile.close();
n+=1;
}
else
{
cout << "Failed to open file inventory.txt"<<endl;
}
break;
  
case 3:
in.open("inventory.txt");
if (in.is_open())
{
for(int i = 0; i < n; i++)
{
in >> st[i].prtNum >> st[i].description
>> st[i].quantity >> st[i].unitPrice >> st[i].totCost;
}
}
cout << " Enter part to modify: ";
cin >> part;
cout << st[part-1].prtNum << " " <<
st[part-1].description << " " << st[part-1].quantity <<
" " << st[part-1].unitPrice << " " << st[part-1].totCost;

cout << " Enter item: ";
cin.ignore();
getline (cin, item);
cout << " ENter Quantity: ";
cin >> quant;
cout << " Enter Cost:$ ";
cin >> cost;
  
st [part-1].description = item;
st [part-1].quantity = quant;
st [part-1].unitPrice = cost;
st [part-1].totCost = quant*cost;
  
outfile.open("inventory.txt");
if (outfile.is_open())
{
for(int i =0; i < n; i++)
{
outfile << st[i].prtNum << " ";
outfile << st[i].description << " ";
outfile << st[i].quantity << " ";
outfile << "$" << st[i].unitPrice << " ";
outfile << "$" << st[i].totCost << " ";
outfile << " ";
}
outfile.close();
}
else if (!in.good())
{
cout << " File doesn't exist";
}
break;
  
case 4:
for (int i = 0; i < n; i++)
{
tot+= st[i].totCost;
}
  
cout << "Total cost of inventory: $"<<tot<< endl;
break;
  
case 5:
flag = false;
break;
}
}
  
return 0;
  
}

Output:

1. PRINT OUT INVENTORY ITEMS                                                                                                                                                                         

2. ENTER A NEW PART                                                                                                                                                                                  

3. MODIFY A PART                                                                                                                                                                                     

4. DISPLAY INVENTORY TOTAL VALUE IN $                                                                                                                                                                

5. EXIT                                                                                                                                                                                              

                                                                                                                                                                                                     

Enter your choice :4                                                                                                                                                                                 

Total cost of inventory: $220                                                                                                                                                                        

                                                                                                                                                                                                     

1. PRINT OUT INVENTORY ITEMS                                                                                                                                                                         

2. ENTER A NEW PART                                                                                                                                                                                  

3. MODIFY A PART                                                                                                                                                                                     

4. DISPLAY INVENTORY TOTAL VALUE IN $                                                                                                                                                                

5. EXIT                                                                                                                                                                                              

                                                                                                                                                                                                     

Enter your choice :5

inventory.txt

0 qqq 2 $22 $44
1 eee 4 $44 $176