Hi there,
I am currently working on a program that reads an inventory list from a file, sorts the list, and then determines the value of the inventory based on the price and the quantity on hand.
After these calculations I need the program to write the output to a file.
I am having problems with the output.
Here is the code ive come up with this far with comments to show you what I'm thinking.
//Program using array of structs to read inventory, sort it, calculate total
//value of products and output into file.
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
struct Product //global struct declaration
{
char number[6]; // 6 char code for part number
char name[16]; // 16 char code for the product name
int price; // price of the product in dollars.
int qty; // quantity on hand in inventory
int value; //Value of inventory (not sure if it should be here)
};
void read_input (Product pd[]); //Read data from file
void bubbleSort2(Product pd[]); //Sort data from file
int value(Product pd[]); // Add up inventory to determine value
void output(Product pd[]); //Output inventory with value to file
int main()
{
Product pd[1000]; // array to hold up to 1000 products
read_input(pd);
bubbleSort2(pd);
value(pd);
output(pd);
return 0;
}
void read_input (Product pd[])
{
int n = 0;
ifstream in_f; // The input file
in_f.open ( "info.dat" );
while ( ! in_f.eof() )
in_f >> pd[n].number >> pd[n].name >> pd[n].price >> pd[n].qty;
n++;
}
void bubbleSort2(Product pd[])
{
bool doMore;
do {
doMore = false; // assume this is last pass over array
for (int i=0; i<-1; i++) {
if (pd[i].price > pd[i+1].price) {
// exchange elements
Product temp = pd[i]; pd[i] = pd[i+1]; pd[i+1] = temp;
doMore = true; // after exchange, must look again
}
}
} while (doMore);
}
int value(Product pd[]) // Add up array to determine value
{
float total = 0;
int n = 0;
total += pd[n].qty * pd[n].price;
return total;
}
void output(Product pd[], int total) //Output inventory to file (help)
{
int n=0;
cout << pd[n].number << pd[n].name << pd[n].price << pd[n].qty;
}