Need help with installation of program

Started by
3 comments, last by nobodynews 9 years, 12 months ago

I have created a simple console application that allows a user to manage a product inventory. After the user quits, it saves the data into a txt file. When the user opens the application again, it reads the information from the text file. I am using Inno to "distribute" the application. My question is, the program works, however, after installing it, it doesn't create the txt file, thus it can't read from it. Could this be a simple path finding problem? If so, how can I fix this? Below is the code in question:

side note: this is an application I developed but have to write a manual for it for my technical writing class. Thus, the reason for using Inno.


// File Name: ProductInventory.cpp
//
// Author: Travis Alt
// Date: 3/31/2014
// Assignment Number: 5
// CS 2308.256 Spring 2014
// Instructor: Jill Seaman
//
// An object that holds an array of Products. You can
// add or remove a product, sort the array by product name,
// get the total quantity in the inventory, or display the
// inventory in a table format.

#include <iostream>
#include <fstream>
#include "ProductInventory.h"

using namespace std;

ProductInventory::ProductInventory()
{
	count = 0;
	capacity = 0;

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

	if (inFile.is_open())
	{
		Product product;
		string productName, locator;
		int quantity;
		double price;

		while (getline(inFile, productName))
		{
			//getline(inFile, productName);
			inFile >> locator;
			inFile >> quantity;
			inFile >> price;

			product.setProductName(productName);
			product.setLocator(locator);
			product.setQuantity(quantity);
			product.setPrice(price);
			inFile.ignore(50, '\n');

			addProduct(product);
		}
	}
	else
	{
		inFile.close();
		ofstream outFile;
		outFile.open("inventory.txt");
		outFile.close();
	}
}

//***********************************************************
// ProductInventory: constructor of the product inventory
//
// size: size of the array
// returns nothing
//***********************************************************
ProductInventory::ProductInventory(int size)
{
	//inventory = new vector<Product>;
	count = 0;
	capacity = size;

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

	if (inFile.is_open())
	{
		Product product;
		string productName, locator;
		int quantity;
		double price;

		while ((inFile.peek() != '\n') && (getline(inFile, productName)))
		//while (getline(inFile, productName))
		{
			inFile >> locator;
			inFile >> quantity;
			inFile >> price;

			product.setProductName(productName);
			product.setLocator(locator);
			product.setQuantity(quantity);
			product.setPrice(price);

			addProduct(product);
		}
	}
	else
	{
		inFile.close();
		ofstream outFile;
		outFile.open("inventory.txt");
		outFile.close();
	}
}

//***********************************************************
// addProduct: adds a new product to the array. If the array is full
// it calls resize to double the size of the array.
//
// product: the product to be added
// returns false if there is a product with the same name and location,
// otherwise returns true if the product was added
//***********************************************************
bool ProductInventory::addProduct(Product product)
{
	/*if (count == capacity)
	{
		resize();
	}*/
	for (int x = 0; x < count; x++)
	{
		if (inventory[x].getProductName() == product.getProductName() &&
			inventory[x].getLocator() == product.getLocator())
		{
			return false;
		}

	}
	inventory.push_back(product);
	sortInventory();
	writeToFile();
	count++;
	return true;
}

//***********************************************************
// removeProduct: removes the product from the list by hiding it
// at the end of the array until it is replaced by another product
//
// productName: name of the product to be removed
// locator: the location of the product to be removed
// return true if the product was removed, else return false if it
// can't find the product and location.
//***********************************************************
bool ProductInventory::removeProduct(Product product)
{
	int index = 0; // index of where the product is in the array
	for (int x = 0; x < count; x++)
	{
		if (product.getProductName() == inventory[x].getProductName() && product.getLocator() == inventory[x].getLocator())
		{
			index = x;

			inventory.erase(inventory.begin() + index);
			count--;
			return true;
		}
	}
	return false;
}

//***********************************************************
// showInventory: displays the current inventory in the form of
// a table
//
// returns void
//***********************************************************
void ProductInventory::showInventory()
{
	sortInventory();

	cout << "\nLocation	Quantity	Price	        Name" << endl;
	cout << "--------------------------------------------------------------------------" << endl;

	for (int x = 0; x < count; x++)
	{
		cout << inventory[x].getLocator() << "\t\t";
		cout << inventory[x].getQuantity() << "\t\t";
		cout << inventory[x].getPrice() << "\t\t";
		cout << inventory[x].getProductName() << endl;
	}
}

//***********************************************************
// getTotalQuantity: adds up the quantity of each product in the array
//
// returns total number of products in the array
//***********************************************************
int ProductInventory::getTotalQuantity()
{
	int totalQuantity = 0; // initial size of the total quantity

	for (int x = 0; x < count; x++)
	{
		totalQuantity += inventory[x].getQuantity();
	}
	return totalQuantity;
}

//***********************************************************
// sortInventory: reorders the array by name from A-Z
//
// returns void
//***********************************************************
void ProductInventory::sortInventory()
{
	for (int i = 0; i < count - 1; i++)
	{
		for (int x = 0; x < count - 1; x++)
		{
			if (inventory[x] > inventory[x + 1])
			{
				Product temp = inventory[x + 1];
				inventory[x + 1] = inventory[x];
				inventory[x] = temp;
			}
			else
			{
				continue;
			}
		}
	}
}

//***********************************************************
// resize: doubles the current size of the array if there is
// not enough space to add more products
//
// returns void
//***********************************************************
void ProductInventory::resize()
{
	int newSize = capacity * 2;

	Product *newInventory = new Product[newSize];

	for (int x = 0; x < count; x++)
	{
		newInventory[x] = inventory[x];
	}

	capacity = newSize;
	//delete[] inventory;
	//inventory = newInventory;
}

void ProductInventory::writeToFile()
{
	ofstream outFile;

	outFile.open("inventory.txt");

	for (int x = 0; x < count; x++)
	{
		outFile << inventory[x].getProductName() << endl << inventory[x].getLocator() << endl << inventory[x].getQuantity() << endl << inventory[x].getPrice() << endl;
	}

	outFile.close();

	outFile.open("inventory_print.txt");

	outFile << "Location	Quantity	Price	        Name" << endl;
	outFile << "--------------------------------------------------------------------------" << endl;

	for (int x = 0; x < inventory.size(); x++)
	{
		outFile << inventory[x].getLocator() << "\t";
		outFile << inventory[x].getQuantity() << "\t\t";
		outFile << inventory[x].getPrice() << "\t\t";
		outFile << inventory[x].getProductName() << endl;
	}
	outFile.close();
}

bool ProductInventory::adjustQuantity(Product product)
{
	for (int x = 0; x < inventory.size(); x++)
	{
		if (inventory[x].getProductName() == product.getProductName() && inventory[x].getLocator() == product.getLocator())
		{
			inventory[x].setQuantity(product.getQuantity());
			return true;
		}
		else
			return false;
	}

	return false;
}

bool ProductInventory::adjustPrice(Product product)
{
	for (int x = 0; x < inventory.size(); x++)
	{
		if (inventory[x].getProductName() == product.getProductName() && inventory[x].getLocator() == product.getLocator())
		{
			inventory[x].setPrice(product.getPrice());
			return true;
		}
		else
			return false;
	}

	return false;
}

double ProductInventory::getTotalPrice()
{
	double totalPrice = 0.0;

	for (int x = 0; x < inventory.size(); x++)
	{
		totalPrice += inventory[x].getPrice();
	}

	return totalPrice;
}

Cpl Alt, Travis A

USMC

Advertisement
Where does it get installed? How are you invoking the program? Do you know the working directory of the program when it is running? Does it work if you start the program, perhaps via the command line, from a location such as your home directory?

Installing programs often places them in security privileged locations that are shared between users. For security purposes, and to prevent unexpected issues with multi-user systems, programs are typically not allowed to write files in the same location they are installed.

User data should be stored in the users home directory somewhere. Different platforms have different conventions. An alternative is to allow the user to specify the location of the database, perhaps as a command line parameter.

I didn't even think about the permissions. I was having the installer put it into the program files folder in the windows directory. Could I perhaps have the program write to the public folder in users? If so would the following statement be correct?:


outFile.open("C:\\Users\\Public\\Documents\\ProductManager\\inventory.txt");

Cpl Alt, Travis A

USMC

Forgot to mention that I am targeting Windows users. In the manual, I am going to instruct the users how to use it in Linux via makefile due to the lack of information I was finding on the internet for packaging distros.

Cpl Alt, Travis A

USMC

I didn't even think about the permissions. I was having the installer put it into the program files folder in the windows directory. Could I perhaps have the program write to the public folder in users? If so would the following statement be correct?:


outFile.open("C:\\Users\\Public\\Documents\\ProductManager\\inventory.txt");

What you really want to do is query windows for the appropriate path to use. Look into using the function SHGetKnownFolderPath with a KNOWNFOLDERID of "FOLDERID_Documents". Then you'll for sure use the correct path for each user instead of hoping its what you expect.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement