In following code itemdescription value is stored properly in file but the float values are stored as different symbols such as @,@A etc.
If i want to store float values also as it is...what should I do??
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <conio.h>
- #include <fstream>
- #include <string>
- using namespace std;
- class Inventory
- {
- private:
- char ItemDescription[6];
- float Stock;
- float Quantity;
- float Price;
- public:
- void DataEntry();
- };
- void Inventory::DataEntry()
- {
- char desc[6];
- float inNum;
- cout<<"PLEASE ENTER A ITEM DESCRIPTION: ";
- cin.getline(desc,10);
- strcpy(ItemDescription , desc);
- cout<<endl<<"ENTER THE STOCK NUMBER OF ITEM: ";
- cin>>inNum;
- if (inNum<0||inNum>999)
- {
- throw ("** Invalid Stock Number **");
- return;
- }
- else
- {
- //::_itoa((int)inNum,(char *)Stock,10);
- Stock=inNum;
- }
- cin.get();
- cout<<"ENTER HOW MANY ITEMS ARE: ";
- cin>>inNum;
- if (inNum<0)
- {
- throw ("** Invalid quantity **");
- return;
- }
- else
- {
- Quantity=inNum;
- }
- cin.get();
- cout<<"ENTER THE PRICE OF ITEM: ";
- cin>>inNum;
- if (inNum<0||inNum>500)
- {
- throw ("** Invalid Price **");
- return;
- }
- else
- {
- Price=inNum;
- }
- cin.get();
- return;
- }
- int main()
- {
- Inventory Num;
- const int numItem=3;
- try
- {
- Num.DataEntry();
- }
- catch(const char msg[])
- {
- cout<<"***************************"<<endl;
- cout<<"\n********FATAL ERROR********"<<endl;
- cout<<"Error: "<<msg<<endl;
- cin.get();
- cin.get();
- return 0;
- }
- Inventory Item[numItem];
- ofstream dataFile("itemInventory.txt");
- for(int x=1;x<numItem;x++)
- {
- try
- {
- Item[x].DataEntry();
- }
- catch (const char msg[])
- {
- cout << "Error: " << msg <<endl;
- }
- dataFile.write(( char*)(&Item[x]),sizeof(Item[x]));
- }
- cout<<"DATA ENTRY COMPLETE."<<endl;
- cin.get();
- return 0;
- }