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!