1

Please help, I can compile this program as long as I don't put in a wrong number. The numbers are supposed to be between 100-999. If I type in anything else, it lets me input the information but I can't access anything after that. Ex: i type in info for stock number 100. It stores the info and I then display it, I see the info. I then type in info for 4... which I shouldn't be able to and then try to view 4, it tells me it doesn't exist. However after that, I try to view 100 again, and it tells me it doesn't exist.

#include "stock.h"
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void Display(fstream &information)
{
    int Stock;
    cout << "Enter a Stock Number:";
    cin >> Stock;

    information.seekg ((Stock - 100 ) * sizeof(stock));

    stock storage;
    information.read (reinterpret_cast< char * >( &storage), sizeof (stock));

    if (storage.getstock() != 0)
    {
        cout << "Stock Number = " << storage.getstock() << endl;
        cout << "Current Stock = " << storage.getCurrentstock() << endl;
        cout << "Change in price = " << storage.getChangestock() <<endl;
        cout << "Highest stock price = " << storage.getHigheststock() <<endl;
        cout << "Lowest stock price = " <<storage.getLoweststock() << endl;
    }
    else 
        cout << "The stock number does not exist:\n";
}

void Add(fstream &information)
{
    int Stock;
    double number;
    cout << "Enter a Stock Number:";
    cin >> Stock;

    information.seekg ((Stock - 100 ) * sizeof(stock));

    stock storage;
    information.read (reinterpret_cast< char * >( &storage), sizeof (stock));

    if (storage.getstock() == 0)
    {
        storage.setstock(Stock);
        cout << " Enter Current Stock:\n";
        cin >> number;
        storage.setCurrentstock(number);
        cout << "Enter change in price: ";
        cin >> number;
        storage.setChangestock(number);
        cout << "Enter Highest stock price:\n";
        cin >> number;
        storage.setHigheststock(number);
        cout << "Enter Lowest stock price:\n";
        cin >> number;
        storage.setLoweststock(number);

        information.seekp ((Stock - 100 ) * sizeof(stock));

        information.write( reinterpret_cast< const char * >( &storage), sizeof(stock));
    }
    else //(storage.getstock() != 0)
        cout << "The stock number already exists:\n";
}

void Delete(fstream &information)
{
    int Stock;
    cout << "Enter a Stock Number:";
    cin >> Stock;

    information.seekg ((Stock - 100 ) * sizeof(stock));

    stock storage;
    information.read (reinterpret_cast< char * >( &storage), sizeof (stock));

    if (storage.getstock() != 0)
    {
        stock blank;

    information.seekp ((Stock - 100 ) * sizeof(stock));

    information.write( reinterpret_cast< const char * >( &blank), sizeof(stock));
    }
    else 
        cout << "The stock does not exist:\n";
}

void Change(fstream &information)
{
    int Stock;
    cout << "Enter a Stock Number:";
    cin >> Stock;

    information.seekg ((Stock - 100 ) * sizeof(stock));

    stock storage;
    information.read (reinterpret_cast< char * >( &storage), sizeof (stock));

    if (storage.getstock() != 0)
    {   
    cout << "Choose a number from the following options:\n";
    cout << "1. Change Stock number:\n";
    cout << "2. Change Current Price of stock:\n";
    cout << "3. Change in price of stock:\n"; 
    cout << "4. Change the Highest price of stock:\n";
    cout << "5. Change the Lowest price of stock:\n";
    cout << "6. Exit:\n";

    int number;
    cin >> number;
    double value = 0;
        if (number !=6)
            {
                stock space;
                switch (number)
                {
                    case 1:
                        cout << "Enter a Stock number:\n";
                        cin >> number;
                        information.seekg ((number - 100 ) * sizeof(stock));

                        information.read (reinterpret_cast< char * >( &space), sizeof (stock));
                        if (space.getstock() == 0)
                        {
                            storage.setstock(number);
                            information.seekp ((number - 100 ) * sizeof(stock));

                        information.write (reinterpret_cast< char * >( &storage), sizeof (stock));

                        information.seekp ((Stock - 100 ) * sizeof(stock));
                        information.write (reinterpret_cast< char * >( &space), sizeof (stock));
                        Stock = 0;
                        }
                        else
                            cout << "The Stock Code already exists";
                        break;
                    case 2:
                        cout << "Enter new Current Price of stock:\n";
                        cin >> value;
                        storage.setCurrentstock(value);
                        break;
                    case 3:
                        cout << "Enter Change in price of stock:\n";
                        cin >> value;
                        storage.setChangestock(value);
                        break;
                    case 4:
                        cout << "Enter new highest price of stock:\n";
                        cin >> value;
                        storage.setHigheststock(value);
                        break;
                    case 5:
                        cout << "Enter new Lowest price of stock:\n";
                        cin >> value;
                        storage.setLoweststock(value);
                        break;
                    default:
                        break;
                }
                if (Stock != 0)
                {
                    information.seekp ((Stock - 100 ) * sizeof(stock));

                    information.write( reinterpret_cast< const char * >( &storage), sizeof(stock));
                }
        }
    }
        else
    cout << "The stock does not exist:\n";
}
void copy(fstream &temp)
{
    ofstream outArray ("Temporary file", ios::out | ios::binary);
        stock stockData;
        for (int i=100 ; i < 999; i++ )
        {
            temp.seekg ((i - 100 ) * sizeof(stock));
            temp.read( reinterpret_cast< char * >(&stockData), sizeof(stock) );

            outArray.write( reinterpret_cast<char * >( &stockData ), sizeof (stock));
        }
        outArray.close();
}
void update(fstream &file)
{
    stock storage, space;
    fstream temp("Temporary file", ios::out | ios::in | ios::binary);
        for (int i=100 ; i < 999; i++ )
            {
                temp.seekg ((i - 100 ) * sizeof(stock));
                temp.read( reinterpret_cast<char * >( &space ), sizeof (stock));

                file.seekg ((i - 100 ) * sizeof(stock));
                file.read( reinterpret_cast<char * >( &storage ), sizeof (stock));

                if (storage.getstock() !=0 && space.getstock() == storage.getstock())
                {
                    storage.setChangestock(space.getChangestock() - storage.getChangestock());
                    if (space.getHigheststock() > storage.getHigheststock())
                        storage.setHigheststock(space.getHigheststock());
                    if (space.getLoweststock() < storage.getLoweststock())
                        storage.setLoweststock(space.getLoweststock());
                    file.seekp ((i - 100 ) * sizeof(stock));
                    file.write ( reinterpret_cast< const char * >( &storage ), sizeof (stock));
                }
            }
        temp.close();
}
void display(fstream &temp)
{
        stock storage;
        for (int i=100 ; i < 999; i++ )
        {
            temp.seekg ((i - 100 ) * sizeof(stock));
            temp.read( reinterpret_cast< char * >(&storage), sizeof(stock) );

            if(storage.getstock() != 0)
            {
            cout << setw(17) << "Stock Number = "
                << setw(17) << "Current Stock = "
                << setw(17) << "Change in price = "
                << setw(17) << "Highest stock price = "
                << setw(17) << "Lowest stock price = " << endl;

        cout << setw(17) << storage.getstock() <<
            setw(17) << storage.getCurrentstock() <<
            setw(17) << storage.getChangestock() <<
            setw(17) << storage.getHigheststock() <<
            setw(17) << storage.getLoweststock() << endl;
            }
        }
}
int main()
{
    char Array[25];

    cout << "Enter a name for the file:";

    cin.getline(Array, 25);
    cin.clear();

    ifstream Data( Array, ios::in | ios::binary);

    if (!Data) 
    {
        ofstream outArray (Array, ios::out | ios::binary);
        stock stockData;
        for (int i=100 ; i < 999; i++ )
            outArray.write( reinterpret_cast< const char * >( &stockData ), sizeof (stock));
        outArray.close();
    }
    Data.close();

int choice;

    fstream file( Array, ios::in | ios::out | ios::binary);
    copy(file);
    do
    {
    cout << "Choose a number from the following options:\n";
    cout << "1. Display Stock number information:\n";
    cout << "2. Add a new Stock:\n";
    cout << "3. Delete a stock:\n";
    cout << "4. Change a stock's data:\n";
    cout << "5. Exit Program:\n";

    cin >> choice;
    switch (choice)
    {
    case 1:
        Display(file);
        break;
    case 2:
        Add(file);
        break;
    case 3:
        Delete(file);
        break;
    case 4:
        Change(file);
        break;
    default:
        break;
    }
    }
    while (choice != 5);

    update(file);
    display(file);
    system("pause");



}

Class Stock

#include "stock.h"

stock::stock(int stockData, double CurrentStock, double ChangeStock, double HighestStock, double LowestStock)

{
    setstock (stockData);
    setCurrentstock (CurrentStock);
    setChangestock (ChangeStock);
    setHigheststock (HighestStock);
    setLoweststock (LowestStock);
}

void stock::setstock(int stockData)
{
    stockdata = stockData;
}

int stock::getstock()
{
    return stockdata;
}

void stock::setCurrentstock(double CurrentStock)
{
    Currentstock = CurrentStock;
}

double stock::getCurrentstock()
{
    return Currentstock;
}

void stock::setChangestock(double ChangeStock)
{
    Changestock = ChangeStock;
}

double stock::getChangestock()
{
    return Changestock;
}

void stock::setHigheststock (double HighestStock)
{
    Higheststock = HighestStock;
}

double stock::getHigheststock()
{
    return Higheststock;
}

void stock::setLoweststock (double LowestStock)
{
    Loweststock = LowestStock;
}

double stock::getLoweststock()
{
    return Loweststock;
}

Header file

# include <iostream>
using namespace std;

class stock
{
public:
    stock( int = 0, double = 0, double = 0, double = 0, double = 0 );

    void setstock (int);
    int getstock ();

    void setCurrentstock (double);
    double getCurrentstock();

    void setChangestock (double);
    double getChangestock ();

    void setHigheststock (double);
    double getHigheststock ();

    void setLoweststock (double);
    double getLoweststock ();

private:
    int stockdata;
    double Currentstock;
    double Changestock;
    double Higheststock;
    double Loweststock;

};

Edited by diafol: fixed formatting

Votes + Comments
Thanks for using code tags correctly :)
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by danzona
0

I'm sure you already know what the problem is, but here is the Display() code:

void Display(fstream &information)
{
	int Stock;
	cout << "Enter a Stock Number:";
	cin >> Stock;
	
	information.seekg ((Stock - 100 ) * sizeof(stock));
	
	stock storage;
	information.read (reinterpret_cast< char * >( &storage), sizeof (stock));

	if (storage.getstock() != 0)
	{
		cout << "Stock Number = " << storage.getstock() << endl;
		cout << "Current Stock = " << storage.getCurrentstock() << endl;
		cout << "Change in price = " << storage.getChangestock() <<endl;
		cout << "Highest stock price = " << storage.getHigheststock() <<endl;
		cout << "Lowest stock price = " <<storage.getLoweststock() << endl;
	}
	else 
		cout << "The stock number does not exist:\n";
}

When the user enters 4 for the Stock Number, the program tries to access
information.seekg(-3840) (the size of the struct is 40 bytes).

I think that this is messing up the fstream. I added information.clear() to the end of the method, thusly:

void Display(fstream &information)
{
	int Stock;
	cout << "Enter a Stock Number:";
	cin >> Stock;
	
	information.seekg ((Stock - 100 ) * sizeof(stock));
	
	stock storage;
	information.read (reinterpret_cast< char * >( &storage), sizeof (stock));

	if (storage.getstock() != 0)
	{
		cout << "Stock Number = " << storage.getstock() << endl;
		cout << "Current Stock = " << storage.getCurrentstock() << endl;
		cout << "Change in price = " << storage.getChangestock() <<endl;
		cout << "Highest stock price = " << storage.getHigheststock() <<endl;
		cout << "Lowest stock price = " <<storage.getLoweststock() << endl;
	}
	else 
		cout << "The stock number does not exist:\n";
    information.clear();
}

This seemed to make the problem go away. But it would be pretty simple to test the value input by the user before making the call to information.seekg(). Which is a much better solution.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.