Data file problem. Probably really easy, but I don't see...

This is a discussion on Data file problem. Probably really easy, but I don't see... within the C++ Programming forums, part of the General Programming Boards category; This is an excersize, and I'm clueless at this point. My data file looks like this: Code: widget,100,1000,100000, thingamajig,100,1000,100000, doohickey,100,1000,100000, ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Data file problem. Probably really easy, but I don't see...

    This is an excersize, and I'm clueless at this point.

    My data file looks like this:

    Code:
    widget,100,1000,100000,
    thingamajig,100,1000,100000,
    doohickey,100,1000,100000,
    steel,100,1000,100000,
    rubber,100,1000,100000,
    aluminum,100,1000,100000,
    wood,100,1000,100000,
    gold,100,1000,100000,
    silver,100,1000,100000,
    platinum,100,1000,100000,
    copper,100,1000,100000,
    bronze,100,1000,100000,
    When I run the program, it looks like this:

    Code:
    widget,100,1000,100000
    100000,0,100,0
    1000,100000,0,0
    100,1000,100000,1e+008
    
    steel,100,1000,100000
    100000,0,100,0
    1000,100000,0,0
    100,1000,100000,1e+008
    
    wood,100,1000,100000
    100000,0,100,0
    1000,100000,0,0
    100,1000,100000,1e+008
    Press any key to continue
    My source looks like this:

    Code:
    #include <fstream.h> //header for cout and cin
    #include <string.h> // header for strings
    #include <windows.h> // required for system("cls")
    #include <conio.h>
    #include "inventory.h"
    
    // The HANDLE (for color coded text)
    HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    void cyan(char);
    void red(char);
    void info(int);
    
    void cyan(char clrTxt[100])
    {
    	SetConsoleTextAttribute(stdOut, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
    	cout << clrTxt << flush;
    }
    
    void red(char clrTxt[100])
    {
    	SetConsoleTextAttribute(stdOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
    	cout << clrTxt << flush;
    }
    
    void cls()
    
    {
    
       COORD coordScreen = { 0, 0 };
    
       DWORD cCharsWritten;
    
       CONSOLE_SCREEN_BUFFER_INFO csbi;
    
       DWORD dwConSize;
    
       HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
    
    
       GetConsoleScreenBufferInfo(hConsole, &csbi);
    
       dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    
       FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
    
       GetConsoleScreenBufferInfo(hConsole, &csbi);
    
       FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
    
       SetConsoleCursorPosition(hConsole, coordScreen);
    
    }
    
    main()
    {
    	item stock[12];
    
    	cyan("");
    	ifstream infile;
    
    	int i = 0;
    	int j = 0;
    
    	char dummybuffer[26];
    	char sSearch[15];
    	int iSearch;
    	char ch;
    
    	ios::sync_with_stdio();
    
    	infile.open("INVENTORY.DAT",ios::in);
    
    	if (!infile) {cout << "ERROR" << endl << endl; return 1;}
    	
    	for (i=0; (!infile.eof()) && (i <= 12); i++)
    	{
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (!infile.eof()) )
    		{
    			dummybuffer[j++] = ch;
    			//cout << ch << '\t' << j << '\t' << dummybuffer[j] << endl;
    			ch = infile.get();
    		}
    		dummybuffer[j] = '\0';  //string terminator
    		stock[i].SetName(dummybuffer);	//sets the name of the item
    		
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (!infile.eof()) )
    		{
    			dummybuffer[j++] = ch;
    			ch = infile.get();
    		}
    		dummybuffer[j] = '\0';  //string terminator
    		stock[i].ChangeQuan(atoi(dummybuffer));	//changes how many of the item
    		
    		j=0;
    		ch = infile.get();
    		while ( (ch != ',') && (!infile.eof()) )
    		{
    			dummybuffer[j++] = ch;	
    			ch = infile.get();
    		}
    		dummybuffer[j] = '\0';  //string terminator
    		stock[i].ChangeValue(atof(dummybuffer));
    
    	}
    
    	infile.close();			// end of outputting to structure
    
    	for (i=0; i<12; i++)	// this for loop outputs everything
    	{
    		cout << stock[i].name << "," << stock[i].GetQuan() << "," << stock[i].GetSValue() << "," << stock[i].GetValue() << endl;
    	}
    	return 0;
    }
    My class looks like this:

    Code:
    #ifndef _INVENTORY_H
    #define _INVENTORY_H
    
    #include <iostream.h>
    #include <stdlib.h>
    
    class item
    {
     public:
    	  // constructors
    	  item();                         // default constructor
    	  item(const item &);           // copy constructor
    
    	  // member functions
    	  void SetName(char[]);
    	  void ChangeQuan(long);	//change the quantity
    	  void ChangeValue(double);	//change the value
    	  int GetQuan();			//get the quantity
    	  double GetSValue();		//get the value of just one of the item
    	  double GetValue();		//get the total value
    	  char name[15];
    
     private:
    	  // data
    	  int stockn;			//how many of item
    	  double svalue;		//value of one of the item
    	  double tvalue;		//the net value of all of the item
    //	  char name[15];
    };
    
    // default constructor
    item::item()
    {
      stockn = 0;
      svalue = 0.00;
      tvalue = 0.00;
    }
    
    // copy constructor
    item::item(const item & Object)
    {
      stockn = Object.stockn;
      svalue = Object.svalue;
      tvalue = Object.tvalue;
    }
    
    // Method to set the name of an item
    void item::SetName(char IncomingString[15])
    {
      strcpy(name,IncomingString);
    }
    
    // Method to set the number of an item
    void item::ChangeQuan(long IncomingNumber)
    {
      stockn = stockn + IncomingNumber;
      tvalue = stockn*svalue;
    }
    
    // Method to set the value of the item
    void item::ChangeValue(double IncomingNumber)
    {
      svalue = IncomingNumber;
      tvalue = stockn*svalue;
    }
    
    // Method to get the quantity of the item
    int item::GetQuan()
    {
      return(stockn);
    }
    
    // Method to get the value of an item
    double item::GetSValue()
    {
      return(svalue);
    }
    
    // Method to get the value of an item
    double item::GetValue()
    {
      return(tvalue);
    }
    
    #endif
    Any ideas? Basically, I want it to input data from the datafile, then output it to the screen as it is in the datafile. I need it separate because the data will change when the program is finished.

    Also, does #define work like this:

    #define widget = 0

    so that stock[widget] would mean EXACTLY stock[0]???

    That way I don't need to make a note of which number is which item in the datafile.


    Thanks so much, any help fixing that bug would be much appreciated!

  2. #2
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    #define works like this:

    #define widget 0

    as for the rest of it, im too lazy to read through all of that :P
    I came up with a cool phrase to put down here, but i forgot it...

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You have four values per line, but you're only readling three. Either add a getline() to skip to the next line:
    Code:
    	infile.getline(dummybuffer,26);
    Code:
    Or add a dummy loop:
    		ch = infile.get();
    		while ( (ch != ',') && (!infile.eof()) )
    			ch = infile.get();

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Where would I put the dummy loop?

    [EDIT]Nevermind... I found the problem... thanks Skippy, you were exactly right!

    Code:
    widget,100,1000,
    thingamajig,100,1000,
    doohickey,100,1000,
    steel,100,1000,
    rubber,100,1000,
    aluminum,100,1000,
    wood,100,1000,
    gold,100,1000,
    silver,100,1000,
    platinum,100,1000,
    copper,100,1000,
    bronze,100,1000,
    If I change my data file to that, then it works... I had the total value as an extra part of the datafile[/EDIT]
    Last edited by Trauts; 11-14-2002 at 09:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data file problem use FILE, loop, and if-else
    By Cyberman86 in forum C Programming
    Replies: 3
    Last Post: 03-25-2009, 11:52 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21