Hi guys,

I've been following Steve Heller's online book on C++ programming and have so far found it very useful. However I have got stuck on a particular part of the book, and would really like some input from you guys

Basically I'm learning about creating and using classes. In his example program Steve gradually builds a Stock Item program which uses a StockItem class to do various things. I've gotten to the point where the program reads the entries of stock from another file and puts them into a vector. It will probably be more useful if I post the source code for you It consists of three main files.

Item2.h

Code:
class StockItem
{
public:
    StockItem();

    StockItem(std::string Name, short InStock, short Price, 
    std::string Distributor, std::string UPC);

    void Display();
    void Read(std::istream& is);

private:
    short m_InStock;
    short m_Price;
    std::string m_Name;
    std::string m_Distributor;
    std::string m_UPC;
};
item2.cpp

Code:
#include <iostream>  
#include <fstream>
#include <string>
#include "item2.h"
using namespace std;

StockItem::StockItem()
: m_InStock(0), m_Price(0), m_Name(), 
  m_Distributor(), m_UPC()
{
}

StockItem::StockItem(string Name, short InStock, 
short Price, string Distributor, string UPC)
: m_InStock(InStock), m_Price(Price), m_Name(Name), 
  m_Distributor(Distributor), m_UPC(UPC)
{
}

void StockItem::Display()
{
  cout << "Name: ";
  cout << m_Name << endl;
  cout << "Number in stock: ";
  cout << m_InStock << endl;
  cout << "Price: ";
  cout << m_Price << endl;
  cout << "Distributor: ";
  cout << m_Distributor << endl;
  cout << "UPC: ";
  cout << m_UPC << endl;
  cout << endl;
}

void StockItem::Read(istream& is)
{
  getline(is,m_Name);
  is >> m_InStock;
  is >> m_Price;
  is.ignore();
  getline(is,m_Distributor);
  getline(is,m_UPC);
}
item2tst.cpp

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Vec.h"
#include "ITEM2.h"

using namespace std;

int main()
{
    ifstream ShopInfo("SHOP2.in");
    vector<StockItem> AllItems(100);
    short i;
    short InventoryCount;

    for (i = 0; i < 100; i ++)
        {
        AllItems[i].Read(ShopInfo);
        if (ShopInfo.fail() != 0)
            break;
        }

    InventoryCount = i;

    for (i = 0; i < InventoryCount; i ++)
        {
        AllItems[i].Display();
        }

    return 0;
}
Now my specific problem with this is the part where the program reads the "shop2.in" file. Firstly, I have never heard of an .in file and so I opened the program in notepad. It's basically a list of entries for the StockItem class. I'm using Microsoft Visual C++ to build the program and when I tried to import the shop2.in file it gave me an error/warning message telling me that it did not recognize the file type. I imported it anyway into the "Resource files" directory and ran the program (I later tried putting it in the source files and header files directories but was having the same problem)

To my surprise the program compiled and linked without any errors. But when I went to run the program I was just presented with a blank screen.

My guess is that the program wasn't reading the "shop2.in" file correctly and so the break command was triggering instantly (that is just my guess as a novice though).

I'm at a bit of a loose end now trying to figure out how to make this work. Due to my pedantic nature I don't want to continue through the book until I'm able to make this program work.

Many thanks for any help you can give me

Kind Regards,

Stonehambey