samedi 18 mars 2023

Why my 4th Function is not providing the desired output? [closed]

I need to make any Inventory Management Application program using structures. At the start of program, data from files is loaded into arrays of structure. Then the user is provided with a menu and user's selection calls different functions like print stock, search Item, print sales, print sale Report, print purchases and purchase report. I need help with last three functions as I don't have any idea what to do. The Purchase functions should provide this output as depicted in the pic. `

`#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;`

`//Structures Declaration.
struct StockRecords
{
int code;
string name;
string type;
string manufac;
double price;
double discount;
int quantity;
};
struct SalesRecord
{
int invoiceNo;
string dateTime;
string name;
int numberOfItems;
double amount;
};
struct PurchaseRecord
{
int invoiceNo;
string supplier;
string prodName;
string type;
string manufac;
double price;
double discount;
int quantity;
};

//Global Constants Indicating Array Size.
const int X = 7, Y = 7, Z = 5;

////Function for loadind stock records from file into an array
void loadStock(StockRecords stock[X][Y])
{
//Creating an ifStream object to read from files.
ifstream inFile;
//Opening the file.
inFile.open("Stock.DB");
if(!inFile)
{
cout << "Error.";
}
 else
{
for(int i=0; i<X; i++)
{
inFile >> stock[i][0].code >> stock[i][1].name >> stock[i][2].type >> stock[i][3].manufac 
 >> stock[i][4].price >> stock[i][5].discount >> stock[i][6].quantity;
}
}
 //Closing the file.
    inFile.close();
}
/******************************************
 1. Function for Printing Stock on Console.
******************************************/
void printStock (StockRecords stock[X][Y])
{
    system("cls");
    cout << "________________________________________________________________________________________________________\n\n";
    cout << "                                         ==STOCK REPORT==         \n";
    cout << "________________________________________________________________________________________________________\n\n";
    cout << "Code"<< setw(14)<<"Name"<< setw(15)<<"Type"<< setw(22)<<"Manufacturer"<< setw(7)<<"Price"
         << setw(19) << "Discount(%)"<< setw(16)<<"Quantity" << endl << endl<< left;;
    cout << fixed << showpoint << setprecision(2);
    for (int i=0; i<X; i++)
    {
        cout << setw(14)<< stock[i][0].code << left<< setw(15) << stock[i][1].name << left<< setw(14) << stock[i][2].type << left 
                    << setw(14) << stock[i][3].manufac << left << setw(16) << stock[i][4].price << left << setw(16) 
                    << stock[i][5].discount << left << setw(16) << stock[i][6].quantity;
        cout << endl;
    }
    cout << "________________________________________________________________________________________________________\n\n";
}   

/***************************************************
  2. Function for Searching for an Item in an array.
***************************************************/
void searchItem (StockRecords stock[X][Y])
{
    stringstream x;
    string option;
    system("cls");
    cout << "_______________________________________________________________________\n\n";
    cout << "                        ==ITEM SEARCH==         \n";
    cout << "_______________________________________________________________________\n\n";
    cout << "                    ENTER PRODUCT NAME OR CODE::  ";
    cin >> option;
    cout << "_______________________________________________________________________\n\n";
    
    
    
    for(int i = 0; i<X; i++)
    {
        x << stock[i][0].code;
        if(x.str () == option || stock[i][1].name == option)
        {
            cout << stock[i][0].code << "\t" << stock[i][1].name << "\t" << stock[i][2].type << "\t" << 
                    stock[i][3].manufac << "\t" << stock[i][4].price << "\t" << stock[i][5].discount << 
                    "\t" << stock[i][6].quantity;
        }   
    }   
}
/**********************************************
  3. Function for Printing Sales Transactions.
**********************************************/
void sale (StockRecords stock[X][Y], SalesRecord sales[Z])
{
    //Creating an ofStream object to write to files.
    ofstream outFile;
    
    stringstream x;
    string name, option;
    int invoice, qty, item = 1;
    double totalBill = 0, total;
    char choice;
    system("cls");
    
    cout << "_______________________________________________________________________\n\n";
    cout << "                        ==SALE TRANSACTION==         \n";
    cout << "                          CUSTOMER: ";
    
    cin >> name;
    cout << "\n\n                          Invoice No: ";
    cin >> invoice;
    
    cout << "_______________________________________________________________________\n\n";
    do
    {
        //system("cls");
        cout << "\n\n                    ENTER PRODUCT NAME OR CODE::  ";
        cin >> option;
        cout << "\n                    ENTER QUANTITY::  ";
        cin >> qty;
        cout << "\n                    ADD ANOTHER (Y/N)::  ";
        cin >> choice;
        cout << "_______________________________________________________________________\n\n";
        cout << "S.NO\tItem Name\tUnits\tPrice\tTotal\n\n";
        
        for(int i = 0; i<X; i++)
        {
            x << stock[i][0].code;
            if(x.str () == option || stock[i][1].name == option)
            {
                total = stock[i][4].price * qty;
                totalBill += total;
                cout << item << "\t" << stock[i][1].name << "\t" 
                     << stock[i][6].quantity << "\t" << stock[i][4].price<< "\t" << total << endl;
item++;
/*sales[i].invoiceNo = invoice;  
sales[i].name = name;
sales[i].numberOfItems = item;
sales[i].amount = totalBill;*/
sales[item - 2].invoiceNo = invoice;
sales[item - 2].name = name;
sales[item - 2].numberOfItems = item - 1;
sales[item - 2].amount = totalBill;
            }
        }
cout << "\n Total Bill............" << totalBill; 
//Opening the file.
 outFile.open("Sale.DB");
if(!outFile)
{
cout << "Error.";
}
outFile << "Invoice No."<< setw(18)<<left<<"Date-Time"<< setw(18)<<"Customer Name"<< setw(17)
<<"No-Of-Items"<< setw(10)<<"Amount" << endl << endl;

outFile << setw(12) << left << sales[item - 2].invoiceNo
<< setw(18) << "3/20/2021-9:39PM"
<< setw(18) << sales[item - 2].name
<< setw(17) << sales[item - 2].numberOfItems
<< setw(10) << fixed << setprecision(2) << sales[item - 2].amount
<< endl;
outFile.close();        
 }while (choice != 'N' && choice != 'n'); 
}

/**************************************************
  4. Function for Printing Sales Report on Console.
**************************************************/
void salesReport(SalesRecord sales[Z])
{
system("cls");
//Creating an ifStream object to read from files.
ifstream inFile;
//Opening the file.
inFile.open("Sale.DB");
if(!inFile)
{
cout << "Error.";
}
cout << "_______________________________________________________________________\n\n";
cout << "                        ==SALES REPORT==         \n";
cout << "_______________________________________________________________________\n\n";
    
cout << "Invoice No."<< setw(13)<<"Date-Time"<< setw(18)<<"Customer Name"<< setw(17)
<<"No-Of-Items"<< setw(10)<<"Amount" << endl << endl<< left;
while(inFile >> sales[0].invoiceNo >> sales[1].dateTime >> sales[2].name 
>> sales[3].numberOfItems >> sales[4].amount);
{
cout << fixed << showpoint << setprecision(2);
    
cout << setw(14)<< sales[0].invoiceNo << left<< setw(15) << sales[1].dateTime << left<< setw(24) 
 << sales[2].name << left << setw(14) << sales[3].numberOfItems  << left << setw(16) << sales[4].amount;
cout << endl;
}
//Closing the file.
inFile.close();
}
/**************************************************
  5. Function for Printing Purchases on Console.
**************************************************/
void purchase (StockRecords stock[X][Y])
{
StockRecords *sptr;
string sup;
int invoice;
char opt;
system ("cls");
cout << "_______________________________________________________________________\n\n";
cout << "==PURCHASE==         \n";
cout << "SUPPLIER: ";
    
cin >> sup;
cout << "\n\n Invoice No: ";
cin >> invoice;
do
    {
cout << "_______________________________________________________________________\n\n";
cout << "PRODUCT NAME::   " 
cin >>  stock[0][0].name;
cout << "\nTYPE:: " << stock[0][1].type;
cout << "\n MANUFACTURES::   " << stock[X][Y].manufac;
cout << "\nRETAILPRICE::   " << stock[X][Y].price;
//cout << "COST:: " << stock[X][Y].name;
cout << "\nDISCOUNT(%)::   " << stock[X][Y].discount;
cout << "\nQTY::   " << stock[X][Y].quantity;
cout << endl << endl;
cout << "ADD ANOTHER? (Y/N)::   ";
cin >> opt;
}while (opt != 'N' && opt != 'n');
}
`/*void purchaseReport (StockRecords stock[], int x);*/`

{
int choice;
//Constant indicating the size of structure array.
    
    
//Declaring StockRecords Structure array.
StockRecords stock[X][Y];
SalesRecord sales[Z];
    
//Function Call to load stock from file into an array.
loadStock(stock);
    
    //do{
cout << " ____________________________________________\n\n";
cout << "==Test Departmental Store==         \n";
cout << "  ____________________________________________\n\n";
cout << "  Main Menu                  \n\n";
cout << "1-Print Stock                     \n";
cout << " 2-Search Item                     \n";
cout << "3-Sale                     \n";
cout << " 4-Sale Report                     \n";
cout << " 5-Purchase                    \n";
cout << "  6-Purchase Report                     \n";
cout << "  7-Exit                     \n\n\n";
cout << "  Date & Time: Day, Month, Date, Time, Year.\n";
cout << "  ____________________________________________\n  ";
cout << "  ENTER YOUR CHOICE (Menu No)::  ";
cin >> choice;
cout << "\n\n ____________________________________________"; 
//Validating the Input.
while (choice < 1 || choice > 7)
{
cout << "\n\nInvalid Input Entered.\n Enter Again: ";
cin >> choice;
}
switch (choice)
{
case 1:
{ 
printStock(stock);     //Function Call to print stock on console.
break;   
 }
case 2: 
{
searchItem(stock);     //Function Call to search for item in the array.
break;   
}
case 3:
{ 
sale(stock,sales);     //Function Call doing the sales.
break; 
}
case 4: 
{
salesReport(sales);     //Function Call doing the sales.
break;
}
case 5:
{ 
purchase(stock);
break;
 }
/*case 6: 
{
            
}*/
        
    
} 
//}while (choice != 7);


return 0;
    
}`

This is my source code. Kindly point out my errors and guide me what to do for the last three functions. The required functionality of program is shown in snapshot FUNCTIONALITY REQUIREMENTS The Program is supposed to provide the following outputs as shown in snapshots

Aucun commentaire:

Enregistrer un commentaire