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

Storing in a Stucture

P: 9
Say I have this code that is printing out:
Expand|Select|Wrap|Line Numbers
  1.     sum = convertSixteen(bp_dat);
  2.     outFile<<sum;
  3.     counter++;
  4.     if (sum ==10)
  5.         outFile<<setw(25)<<"ABMOC"<<endl<<endl;
  6.     else if (sum == 11)
  7.         outFile<<endl<<"A2C2"<<endl<<endl;
  8.     else if (sum == 12)
  9.         outFile<<endl<<"Battery CP"<<endl<<endl;
  10.     else 
  11.         outFile<<endl<<"Undefined"<<endl<<endl;
  12.  
  13.     offset+=16;
  14.     sum = convertSixteen(bp_dat);
  15.     outFile<<sum;
  16.     counter++;
  17.     if (sum ==0)
  18.         outFile<<setw(25)<<"Live Mode"<<endl<<endl;
  19.     else if (sum == 1)
  20.         outFile<<setw(25)<<"Simulation Mode"<<endl<<endl;
  21.     else 
  22.         outFile<<setw(25)<<"Undefined"<<endl<<endl;
  23.  
  24.     offset+=16;
  25.     sum = convertThirtyTwo(bp_dat);
  26.     outFile<<"TADIL-B DLRP"<<endl;
  27.     outFile<<sum<<setw(25)<<"Latitude"<<endl<<endl;
  28.     counter++;
  29.  
  30.  
  31.     offset+=32;
  32.     sum = convertThirtyTwo(bp_dat);
  33.     outFile<<sum<<setw(25)<<"Longitude"<<endl<<endl;
  34.     counter++;
  35.  
  36.  
  37.     offset+=32;
  38.     sum = convertThirtyTwo(bp_dat);
  39.     outFile<<"TADIL-B SCC"<<endl;
  40.     outFile<<sum<<setw(25)<<"Latitude"<<endl<<endl;
  41.     counter++;
  42.  
  43.     offset+=32;
  44.     sum = convertThirtyTwo(bp_dat);
  45.     outFile<<sum<<setw(25)<<"Longitude"<<endl<<endl;
  46.     counter++;
  47.  
  48.     offset+=32;
  49.     sum = convertThirtyTwo(bp_dat);
  50.     outFile<<"FAAD C2I DLRP"<<endl;
  51.     outFile<<sum<<setw(25)<<"Latitude"<<endl<<endl;
  52.     counter++;
  53.  
  54.     offset+=32;
  55.     sum = convertThirtyTwo(bp_dat);
  56.     outFile<<sum<<setw(25)<<"Longitude"<<endl<<endl;
  57.     counter++;
  58.  
  59.  
  60.     offset+=32;
  61.     sum = convertThirtyTwo(bp_dat);
  62.     outFile<<"FAAD C2I MASTER Grid Vertices"<<endl;
  63.     outFile<<sum<<setw(25)<<"North-East vertex Latitude"<<endl<<endl;
  64.     counter++;
  65.  
  66.     offset+=32;
  67.     sum = convertThirtyTwo(bp_dat);
  68.     outFile<<sum<<setw(25)<<"North-East vertex Longitude"<<endl<<endl;
  69.     counter++;
  70.  
  71.     offset+=32;
  72.     sum = convertThirtyTwo(bp_dat);
  73.     outFile<<sum<<setw(25)<<"South-East vertex Latitude"<<endl<<endl;
  74.     counter++;
  75.  
  76.     offset+=32;
  77.     sum = convertThirtyTwo(bp_dat);
  78.     outFile<<sum<<setw(25)<<"South-East vertex Longitude"<<endl<<endl;
  79.     counter++;
  80.  
  81.     offset+=32;
  82.     sum = convertThirtyTwo(bp_dat);
  83.     outFile<<sum<<setw(25)<<"South-West vertex Latitude"<<endl<<endl;
  84.     counter++;
  85.  
  86.     offset+=32;
  87.     sum = convertThirtyTwo(bp_dat);
  88.     outFile<<sum<<setw(25)<<"South-West vertex Longitude"<<endl<<endl;
  89.     counter++;
  90.  
  91.     offset+=32;
  92.     sum = convertThirtyTwo(bp_dat);
  93.     outFile<<sum<<setw(25)<<"North-West vertex Latitude"<<endl<<endl;
  94.     counter++;
  95.  
  96.     offset+=32;
  97.     sum = convertThirtyTwo(bp_dat);
  98.     outFile<<sum<<setw(25)<<"North-West vertex Longitude"<<endl<<endl;
  99.     counter++;
  100.  
  101.     outFile<<"FAAD C2I AOI"<<endl;
  102.  
  103.     offset+=32;
  104.     sum = convertThirtyTwo(bp_dat);
  105.     outFile<<sum<<setw(25)<<"Altitude"<<endl<<endl;
  106.     counter++;
  107.  
  108.     offset+=32;
  109.     sum = convertThirtyTwo(bp_dat);
  110.     outFile<<sum<<setw(25)<<"Number of Points"<<endl<<endl;
  111.     counter++;
  112.  
  113.  
  114.     if (sum >=1)
  115.  
  116.         tempsum = sum;
  117.  
  118.  
  119.         for ( j = 0; j<tempsum; j++)
  120.         {
  121.  
  122.             offset+=32;
  123.             sum = convertThirtyTwo(bp_dat);
  124.             outFile<<sum<<setw(25)<<"Latitude"<<endl<<endl;
  125.  
  126.             offset+=32;
  127.             sum = convertThirtyTwo(bp_dat);
  128.             outFile<<sum<<setw(25)<<"Longitude"<<endl<<endl;
  129.             counter++;
  130.  
  131.         }
  132.  
  133.  
  134. }
  135.  
  136.  
How can I store what is being printed out in a Structure or as an object.?
Jun 7 '07 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Use a stringstream.

Use the << operator you have to load the stringstream.

Then write an >> operator for your class or struct object and use the stringstream as the istream.

Here's one for a hypothetical Date object:
Expand|Select|Wrap|Line Numbers
  1. class Date
  2. {
  3.     private:
  4.        int month;
  5.        int day;
  6.        int year;
  7.     public:
  8.        friend istream& operator>>(istream& is, Date& dt);
  9. };
  10. istream& operator>>(istream& is, Date& dt)
  11. {
  12.     is >> dt.month >> dt.day >> dt.year
  13. }
  14.  
  15. int main()
  16. {
  17.    stringstream ss;
  18.    Date d;
  19.    ss << 7 << ' ' << 4 << ' ' << 1776 << endl;
  20.    ss >> d;
  21. }
  22.  
Remember to read back assumes you know the format of what was written. You may need field delimiters for playback.
Jun 8 '07 #2

Post your reply

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