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;
}

Recommended Answers

All 6 Replies

You are displaying the screen with cout rather than writing to file with a user declared stream object.

You are only outputting a single Product, not a group of Prtoducts.

There are other syntax errors in the code as well, but these are the big ones I see in the output section which is what you asked about.

Thanks for the help.

I had it just temporarily setup to output to the screen so I could at least make sure the program worked.

Could you help me out with any of the other errors you saw?

Thank you.

Here is what I have edited so far, any insight would be much appreciated.

#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; 
};

void read_input (Product pd[], int counter); //Read data from file
void bubbleSort2(Product pd[], int counter); //Sort data from file
void calc(Product pd[], int counter); // Add up array to determine value
void output(Product pd[]); //Output inventory to file


int main()
{
	Product pd[1000]; // array to hold up to 1000 products
	
	int counter = 0;

	read_input(pd, counter);
	bubbleSort2(pd, counter);
	calc(pd, counter);
	output(pd);
	
	return 0;
}


void read_input (Product pd[], int counter)
{
	int n = 0;
	ifstream in_f;                // The input file
	
	in_f.open ( "~/Project12/build/info.dat" ); 
	
	while ( ! in_f.eof() )
		
		in_f >> pd[n].number >> pd[n].name >> pd[n].price  >> pd[n].qty;
		n++;
	counter++;
	
	
}


void bubbleSort2(Product pd[], int counter) 
{
    bool doMore;
    do {
        doMore = false;  // assume this is last pass over array
        for (int i=0; counter<-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);
}

void calc(Product pd[], int counter) // Add up array to determine value
{
	 for (int i=0; counter<-1; i++)
		 pd[i].value += pd[i].qty * pd[i].price;
					
}

void output(Product pd[]) //Output inventory to file
{
	int i = 0;
	
	fstream file_op("~/Project12/build/inventory.dat",ios::out);
	
	file_op.close();
	
	file_op<< pd[i].number << pd[i].name << pd[i].price  << pd[i].qty << pd[i].value;
}

Ok Ive made some more changes:

#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; 
};

void read_input (Product pd[], int counter); //Read data from file
void bubbleSort2(Product pd[], int counter); //Sort data from file
void calc(Product pd[], int counter); // Add up array to determine value
void output(Product pd[], int counter); //Output inventory to file


int main()
{
	Product pd[1000]; // array to hold up to 1000 products
	
	int counter = 0;

	read_input(pd, counter);
	bubbleSort2(pd, counter);
	calc(pd, counter);
	output(pd, counter);
	
	return 0;
}


void read_input (Product pd[], int counter)
{
	int n = 0;
	ifstream in_f;                // The input file
	
	in_f.open ( "~/Project12/build/info.dat" ); 
	
	while ( ! in_f.eof() )
		
		in_f >> pd[n].number >> pd[n].name >> pd[n].price  >> pd[n].qty;
		n++;
	counter++;
	
	
}


void bubbleSort2(Product pd[], int counter) 
{
    bool doMore;
    do {
        doMore = false;  // assume this is last pass over array
        for (int i=0; i<=counter; 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);
}

void calc(Product pd[], int counter) // Add up array to determine value
{
	 for (int i=0; i<=counter; i++)
		 pd[i].value += pd[i].qty * pd[i].price;
					
}

void output(Product pd[], int counter) //Output inventory to file
{
	fstream file_op("~/Project12/build/inventory.dat",ios::out);
	
	for (int i=0; i<=counter; i++)	
	file_op<< pd[i].number << pd[i].name << pd[i].price  << pd[i].qty << pd[i].value;
	
	file_op.close();
}

First, only write and test one function at a time. So that's all I'm going to correct at a time.

while ( ! in_f.eof() )
   in_f >> pd[n].number >> pd[n].name >> pd[n].price  >> pd[n].qty;
   n++;
   counter++;

Three problems here.

First, without opening and closing curly braces the body of a loop will be only the first statement after loop or if statement is declared. Since I assume you want to increment n and counter each time through the loop, make sure they are included .

Second, the value of counter at the end of read_input() will not be visible back in main() because it is passed to read_input() by value, not by reference.

Third, don't use eof() in conditional statements. It will cause an error sooner or later, and if you're lucky your program will crash or hang rather than just perpetuate the error. Use the folllowing syntax instead:

while ( in_f >> pd[n].number)
{
   in-F >> pd[n].name >> pd[n].price  >> pd[n].qty;

Now with those corrections made comment out all the other function calls in main() and output the value of counter in main() to be sure it's correct and output the values read into the pd[n] to be sure it's reading values in correctly before removing the debugging statements and proceeding to the next function. If you don't want to put the debugging output statements into the program and then remove them, then use a debugger to monitor the variables to be sure everything is correct.

Thanks for the info. I changed my code and got rid of the eof.

Could you help me debug the rest?

I tried to test the output of pd[n] but it wouldnt allow it.

Here's how you do it.

Don't bother looking at the final output yet. Just look at the next function in the sequence. In this case output the prices from each input to be sure the input has been sorted correctly first. When you're sure it's sorting correctly and there are no runtime errors, then, and only then remove the debugging code you may have used and move on to the next function. Do not, I repeat for the third time, do not even bother with the last function's output until you're sure everything else is working right before you get there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.