Sign in to follow this  

Troubel saving to text files

This topic is 3585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a program for fun to learn more about C++. I can show the "quotes" on screen once I run the debugger, but I'm having trouble coding how to save new quotes and saving them to the existing text file. Could someone help me? Here is my code:
// Ass2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "conio.h"
#include "iostream"
#include "iomanip"
#include "fstream"
#include "sstream"

#define clrscr()

using namespace std;

const float ZONE1_CHARGE=0.1, ZONE2_CHARGE=0.2, ZONE3_CHARGE=0.3,ZONE4_CHARGE=0.4;// Corresponding delivery zone charges
const int   ZONE1=5, ZONE2=12, ZONE3=20; // Delivery zones representing miles from shop
const int   QUALITY_1=11,QUALITY_2=13,QUALITY_3=14,QUALITY_4=17,QUALITY_5=20; // Price per sq m of carpet (based on quality)
const int   GUARANTEE_Q1=1,GUARANTEE_Q2=2,GUARANTEE_Q3=3,GUARANTEE_Q4=4,GUARANTEE_Q5=5; // Guarantee periods for corresponding quality prices
const int   INITIAL_QUOTE_REF_NUM=1000;
const int   NAME_LEN=20;
const int   QUOTE_COL_WIDTH = 7, NAME_COL_WIDTH = NAME_LEN, COST_COL_WIDTH = 11, DELIVERY_COL_WIDTH = 12, TOTAL_COL_WIDTH = 9, GUARANTEE_COL_WIDTH = 2;
const int   TABLE_WIDTH = 79; // Num of characters in output table row
const char  POUND_SIGN = 156;

void main()
{
  fstream quotesFile, quotesFileCopy;
  string  custRecord;
  float   Length=0, Width=0, Area=0, Cost=0, DeliveryCharge=0;
  int     UnitPrice=0, GuaranteePeriod=0, DelDistance=0, QuoteNum=INITIAL_QUOTE_REF_NUM;
  char    AnotherQuote, DelRequired, c;
  char    Name[NAME_LEN];
  bool    ValidPrice;

	  quotesFile.open("Quotes.txt", ios::in);
	  if(quotesFile.fail())
	  {
		  cout << "Master text file cannot be read. Program terminating." << endl;
		  return;
	  }

	  quotesFileCopy.open("QuotesCopy.txt", ios::out); 
	  if(quotesFileCopy.fail())
	  {
		  cout << "Text file cannot be read. Program terminating." << endl;
		  return;
	  }

	  getline(quotesFile, custRecord);

	  while (!quotesFile.fail() )
	  {
		  int i = 0;
		  while(!custRecord[i] == 0)
		  {
			  c = custRecord[i];
			  cout << c;
			  i++;
		  }

	  cout << endl;

	  quotesFileCopy << custRecord << endl;

	  getline(quotesFile, custRecord); 

	  }

	  quotesFile.close(); 
	  quotesFileCopy.close();

  do
  {
     clrscr();

     cout << "\nPlease enter surname and initials: ";
     cin >> ws;
     cin.getline(Name,NAME_LEN);

     cout << "Please enter length of room (m): ";
     cin >> Length;

     cout << "Please enter width of room (m): ";
     cin >> Width;

     do
     {
       DeliveryCharge=0;
       ValidPrice = true;

       cout << "Please enter the price per square metre from the following price range:\n";
       cout << POUND_SIGN << QUALITY_1 << ", " << POUND_SIGN << QUALITY_2 << ", " << POUND_SIGN
            << QUALITY_3 << ", " << POUND_SIGN << QUALITY_4 << ", " << POUND_SIGN << QUALITY_5 << "\n\n" << POUND_SIGN;
       cin >> UnitPrice;

       cout << "\n";

       //Assign correct guarantee period for unit price entered.
       switch(UnitPrice)
       {
         case QUALITY_1 : GuaranteePeriod=GUARANTEE_Q1;  break;
         case QUALITY_2 : GuaranteePeriod=GUARANTEE_Q2;  break;
         case QUALITY_3 : GuaranteePeriod=GUARANTEE_Q3;  break;
         case QUALITY_4 : GuaranteePeriod=GUARANTEE_Q4;  break;
         case QUALITY_5 : GuaranteePeriod=GUARANTEE_Q5;  break;
         default        :  cout << "\n\nInvalid price.\n\n";
                           ValidPrice = false;
       }

     }while (!ValidPrice);


     Area = Length * Width;
     Cost = Area * UnitPrice;

     cout << "Do you require delivery? (Y/N) ";
     cin >> DelRequired;

     //Force correct input to delivery question.
     while(toupper(DelRequired)!='Y' && toupper(DelRequired)!='N')
     {
       cout << "Invalid character  - enter Y or N:";
       cin >> DelRequired;
     }

     if(toupper(DelRequired)=='Y')
     {
       cout << "\n\nPlease enter your delivery distance in miles: ";
       cin >> DelDistance;

       if(DelDistance >=0 && DelDistance <=ZONE1)
       {
         DeliveryCharge = Cost * ZONE1_CHARGE;
       }
       else
       {
         if(DelDistance <=ZONE2)
         {
           DeliveryCharge = Cost * ZONE2_CHARGE;
         }
         else
         {
           if(DelDistance <=ZONE3)
           {
             DeliveryCharge = Cost * ZONE3_CHARGE;
           }
           else
           {
             DeliveryCharge = Cost * ZONE4_CHARGE;
           }
         } //end else > ZONE2
       } //end else > ZONE1
     } //end delivery required

     cout << endl << endl << endl
          << setfill('_') << setw(TABLE_WIDTH) << "" << "\n\n"
          << setfill(' ')
          << setw(7) << "   Ref  | " << setw(NAME_COL_WIDTH) << "         Name        | " << setw(COST_COL_WIDTH) << "Carpet | " << setw(DELIVERY_COL_WIDTH)
          << "Delivery | " <<  setw(TOTAL_COL_WIDTH) << "  Total   | " << setw(GUARANTEE_COL_WIDTH+7) << "Guarantee |" << endl
          << setfill('_') << setw(TABLE_WIDTH) <<  "" << "\n\n" << setfill(' ');

     cout << fixed << showpoint << setprecision(2);

     cout << setfill(' ')
          << setw(7) << QuoteNum << " | "
          << setw(NAME_COL_WIDTH) << Name << " | "
          << setw(1) << POUND_SIGN << setw(COST_COL_WIDTH-4)  << Cost << " | "
          << setw(1) << POUND_SIGN << setw(DELIVERY_COL_WIDTH-4) << DeliveryCharge << " | "
          << setw(1) << POUND_SIGN << setw(TOTAL_COL_WIDTH-1) << Cost + DeliveryCharge << " | "
          << setw(7) << "Years: " << setw(GUARANTEE_COL_WIDTH)  << GuaranteePeriod << " |"
          << endl;

     cout << "\n\nDo you require another quote? ";
     cin >> AnotherQuote;

  } while(AnotherQuote=='Y'||AnotherQuote=='y');

  cout << "\n" << endl;
  system("pause");
}
[Edited by - lvrpl4life on February 16, 2008 3:28:19 PM]

Share this post


Link to post
Share on other sites
1. Click the 'edit' button in the upper-right, and then place [source][/source] tags around your code.

2. This would be more at home in For Beginners (perhaps a mod will move it there).

3. Just from glancing at your code, it appears that you're using a mix of C and C++ techniques and idioms. Fixing this is one of the first things you'll want to do (and once you've gotten your code formatted so that we can read it more easily, we can help you do this).

Share this post


Link to post
Share on other sites

This topic is 3585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this