By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

How float values stored in file??

P: 13
hi all,

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
  1.  
  2.  
  3. #include <iostream>
  4. #include <conio.h>
  5. #include <fstream>
  6. #include <string>
  7. using namespace std;
  8.  
  9. class Inventory
  10. {
  11. private:
  12.     char ItemDescription[6];
  13.     float Stock;        
  14.     float Quantity;
  15.     float Price;
  16. public:
  17.      void DataEntry();
  18.  
  19. };
  20.  
  21. void Inventory::DataEntry()
  22. {
  23.     char desc[6];
  24.     float inNum;
  25.  
  26.     cout<<"PLEASE ENTER A ITEM DESCRIPTION: ";
  27.     cin.getline(desc,10);
  28.     strcpy(ItemDescription , desc);
  29.  
  30.     cout<<endl<<"ENTER THE STOCK NUMBER OF ITEM: ";
  31.     cin>>inNum;
  32.     if (inNum<0||inNum>999)
  33.     {
  34.         throw ("** Invalid Stock Number **");
  35.         return;
  36.     }
  37.     else
  38.     {   
  39.          //::_itoa((int)inNum,(char *)Stock,10);
  40.         Stock=inNum;
  41.     }
  42.  
  43.     cin.get();
  44.     cout<<"ENTER HOW MANY ITEMS ARE: ";
  45.     cin>>inNum;
  46.     if (inNum<0)
  47.     {
  48.       throw ("** Invalid quantity **"); 
  49.       return; 
  50.     } 
  51.     else
  52.     {    
  53.         Quantity=inNum;
  54.     }
  55.     cin.get();
  56.     cout<<"ENTER THE PRICE OF ITEM: ";
  57.     cin>>inNum;
  58.     if (inNum<0||inNum>500)
  59.     {
  60.         throw ("** Invalid Price **"); 
  61.         return; 
  62.     } 
  63.     else
  64.     {   
  65.         Price=inNum;
  66.     }
  67.     cin.get();
  68.     return; 
  69. }
  70.  
  71. int main()
  72. {
  73.     Inventory Num;
  74.     const int numItem=3;
  75.     try
  76.     {
  77.         Num.DataEntry();
  78.     }
  79.     catch(const char msg[])
  80.     {
  81.         cout<<"***************************"<<endl;
  82.         cout<<"\n********FATAL ERROR********"<<endl;
  83.         cout<<"Error: "<<msg<<endl;
  84.         cin.get();
  85.         cin.get();
  86.         return 0;
  87.     }
  88.     Inventory Item[numItem];
  89.     ofstream dataFile("itemInventory.txt");
  90.     for(int x=1;x<numItem;x++)
  91.     {
  92.         try 
  93.         {
  94.                Item[x].DataEntry();
  95.         } 
  96.  
  97.         catch (const char msg[])
  98.         {
  99.             cout << "Error: " << msg <<endl;
  100.         }
  101.  
  102.         dataFile.write(( char*)(&Item[x]),sizeof(Item[x]));
  103.     }
  104.  
  105.     cout<<"DATA ENTRY COMPLETE."<<endl;
  106.  
  107.     cin.get();
  108.     return 0;
  109. }
  110.  
Jun 20 '07 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Your code has some bugs in it.
1) the DataEntry function does a getline of 10 into an array of 6. Crashes here.
2) your file write does a really ugly C-style cast where you lie to the compiler by telling it the address of an Inventory object is a char*. Big no-no.

I added an inserter (operator<<) to your code to avoid the cast and fixed the getline of 10.

The fixed code is below and the output file has your expected data.

I didn't check for validity.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7. class Inventory
  8. {
  9. private:
  10.     char ItemDescription[6];
  11.     float Stock;        
  12.     float Quantity;
  13.     float Price;
  14. public:
  15.      void DataEntry();
  16.      friend ostream& operator<<(ostream& os, const Inventory& rhs);
  17.  
  18. };
  19. ostream& operator<<(ostream& os, const Inventory& rhs)
  20. {
  21.    os << rhs.ItemDescription << " " << rhs.Price << " "
  22.         << rhs.Quantity << " " << rhs.Stock << endl;
  23.    return os;
  24. }
  25. void Inventory::DataEntry()
  26. {
  27.     char desc[6];
  28.     float inNum;
  29.  
  30.     cout<<"PLEASE ENTER A ITEM DESCRIPTION: ";
  31.     //cin.getline(desc,10);
  32.     cin.getline(desc,6);
  33.     strcpy(ItemDescription , desc);
  34.  
  35.     cout<<endl<<"ENTER THE STOCK NUMBER OF ITEM: ";
  36.     cin>>inNum;
  37.     if (inNum<0||inNum>999)
  38.     {
  39.         throw ("** Invalid Stock Number **");
  40.         return;
  41.     }
  42.     else
  43.     {   
  44.          //::_itoa((int)inNum,(char *)Stock,10);
  45.         Stock=inNum;
  46.     }
  47.  
  48.     cin.get();
  49.     cout<<"ENTER HOW MANY ITEMS ARE: ";
  50.     cin>>inNum;
  51.     if (inNum<0)
  52.     {
  53.       throw ("** Invalid quantity **"); 
  54.       return; 
  55.     } 
  56.     else
  57.     {    
  58.         Quantity=inNum;
  59.     }
  60.     cin.get();
  61.     cout<<"ENTER THE PRICE OF ITEM: ";
  62.     cin>>inNum;
  63.     if (inNum<0||inNum>500)
  64.     {
  65.         throw ("** Invalid Price **"); 
  66.         return; 
  67.     } 
  68.     else
  69.     {   
  70.         Price=inNum;
  71.     }
  72.     cin.get();
  73.     return; 
  74. }
  75.  
  76. int main()
  77. {
  78.     Inventory Num;
  79.     const int numItem=3;
  80.     try
  81.     {
  82.         Num.DataEntry();
  83.     }
  84.     catch(const char msg[])
  85.     {
  86.         cout<<"***************************"<<endl;
  87.         cout<<"\n********FATAL ERROR********"<<endl;
  88.         cout<<"Error: "<<msg<<endl;
  89.         cin.get();
  90.         cin.get();
  91.         return 0;
  92.     }
  93.     Inventory Item[numItem];
  94.    // ofstream dataFile("itemInventory.txt");
  95.     ofstream dataFile("c:\\scratch\\instructor\\itemInventory.txt");
  96.     for(int x=1;x<numItem;x++)
  97.     {
  98.         try 
  99.         {
  100.             Item[x].DataEntry();
  101.         } 
  102.  
  103.         catch (const char msg[])
  104.         {
  105.             cout << "Error: " << msg <<endl;
  106.         }
  107.  
  108.         dataFile << Item[x];
  109.         //dataFile.write(( char*)(&Item[x]),sizeof(Item[x]));
  110.     }
  111.  
  112.     cout<<"DATA ENTRY COMPLETE."<<endl;
  113.  
  114.     cin.get();
  115.     return 0;
  116. }
  117.  
Jun 20 '07 #2

P: 13
Thanks,
It's working fine now...I learn many things from this example...
Jun 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.