View Single Post
Old 11-23-2004, 06:29 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
You are only guessing on how to calculate the prices, so the only thing I can do is guessing too. That I won't do. My credo stands:

Programming is communicating.

So that part I cannot do. You will need to ask your teacher how he interpretes the final price of the wood type. He is also responsible for defining "boardfoot".
I'll explain:
One of my first jobs long ago was junior coder at CMG Netherlands. That's a biggie, trust me on that. The job was simple: learn this book (telecommunication) and start coding.
I had no questions anymore. If I needed to know what is what, I learn the book at evening. And the next day I add another ten lines of code. Because I had the means of understanding telecom, I could move on.

Now, how can I or you know about the world of wood, if no one tought you or me.

As for your code, here is an improvement in standard (ISO) C++. It the *.txt file already exists, then new entries are ADDED to the file. run the program a few times then open hardwood.txt and see what's in it.
Code:
 #include <iostream>
#include <string>
#include <climits>
#include <fstream>

using std::cin;
using std::cout;
using std::endl;
using std::numeric_limits;
using std::streamsize;
using std::ofstream;
using std::string;
using std::getline;
using std::ios;

//Optional function: depends on IDE. Remove in final release. void wait_for_enter();

int main(int argc, char *argv[])
{
   string woodName;
   double price;

   //Get data from user
   cout << "Enter hardwood name: " ;
   getline(cin, woodName);
   
   cout << "Enter price of wood: " ;
   cin >> price;
   
   //Open file for writing. We add the wood types and prices to the file.
   ofstream fout( "HARDWOOD.TXT",  ios::app );
   if ( fout ) //<-- file has been opened or created successfully.
   {
      fout << woodName <<'\t' << price << endl;
      fout.close();
      if( !fout.fail() )
      {
         cout << "Above data written to file HARDWOOD.TXT" << endl;
      }
      else
      {
         cout << "ERROR: something has gone wrong during writing of file."<<endl;
      }
      cout << woodName <<",\t" << price << endl;
   }
   else // <-- file could not be opened or created for some reason.
   {
      cout << "ERROR: could not open or create file." << endl;
      cout << "NOTICE: all processes have been skipped." << endl;
   }
   
   //Remove this for release version.
   cin.ignore(numeric_limits<streamsize>::max(), '\n');
   wait_for_enter();
   return 0;

}

//--------------------------------------------------- void wait_for_enter()
{
  cout << "press <enter> to continue...";
  // Reset failstate, just in case.
  cin.clear();
  string line;
  getline( cin, line);
}
Quote:
I hope I explained this well.
No, but that is not your fault. No one tought you yet.
Because of the simplicity, the model was clear to me: I think I know what you are talking about.
But I don't know what the requirements EXACTLY are for the application TO DO. So I gamble a bit and decide to append new entries to the file.
This is called "use case": what does it do, what is it exactly responsible for.
__________________
Valmont is offline   Reply With Quote