473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How float values stored in file??

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
2 2203
weaknessforcats
9,208 Expert Mod 8TB
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
Thanks,
It's working fine now...I learn many things from this example...
Jun 21 '07 #3

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

Similar topics

9
by: franzkowiak | last post by:
Hello, I've read some bytes from a file and just now I can't interpret 4 bytes in this dates like a real value. An extract from my program def l32(c): return ord(c) + (ord(c)<<8) +...
8
by: vijay | last post by:
Hello, What happens to float variable in loops. For example, float f=8.7; if(f<8.7) printf("less"); else if(f==8.7) printf("equal"); else if(f>8.7)
2
by: Chris | last post by:
Hi The following code is giving strange results........... float fTest = 536495.61f; cout << _T("float: ") << fTest << endl; int dec, sign; char* pszTest = _fcvt(fTest, 10, &dec, &sign);...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
6
by: Steve | last post by:
Hi, I've developed a testing application that stores formatted results in a database. Recently it was requested that I add the ability to generate graphs from the raw, un formatted test results...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
23
by: JDT | last post by:
Hi, It seems that using floats as the first tupelo for an STL map (shown below) can cause problems but I don't remember what the problems were. Is it common practice to use a structure like...
35
by: bala.pandu | last post by:
Hello Everyone, When i am assigning a float value and try to print the same, i found the value stored is not the exact value which i gave. int main() { float f1; printf("Enter a float value...
1
by: veremue | last post by:
I want to store a float/double as String. I have an input file with a column with float values i.e. have the form 1.4E15. I want to store these values as Strings. Here is my code which is resulting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.