Rabu, 01 April 2026

C++ From Year 1995 File From "O"

 // Filename: OPEN1ST.CPP

// Opens a file for output. If the file exists, the

// existing version of the file will be overwritten.

#include <fstream.h>


// Define the file pointers globally

ofstream poemFile;


void main()

{

  poemFile.open("C:\\poems.txt", ios::out);

  if (!poemFile)

    { cout << "An error occurred while opening the file!" << endl;

      return;

    }

}

  // Code gets here if the file opened properly

  //

  // Body of program to write the file's contents would follow

==========================

// File : OVERLOAD.CPP

// Demonstration of overloading and defaults.

// in a program that prints loan repayments

#include <iostream.h>


const int TRUE = 1;

const int FALSE = 0;


void Print(int i, int newline = FALSE);

void Print(float f, int newline = FALSE, int width = 5);

void Print(char str[],int newline = FALSE);


void main()

  {

    float amount = 0;

    float rate = 0;

    float repayment = 0;


    Print("Enter amount to repay: ");

    cin >> amount;

    Print("Enter interest rate per annum: ");

    cin >> rate;


    Print("Amount of loan: ");

    Print(amount,TRUE);

    Print("Interest rate: ");

    Print(rate, TRUE);

    Print("Repayment table", TRUE);

    Print("---------------", TRUE);

    Print("Months\tRepayment\tMonthly Amt",TRUE);


    // Print a table for 3 to 48 months in 3 monthly intervals

    for (int months = 3; months <= 48; months+=3)

      {

        // Interest will be rate * months / 12

        if (rate >= 0)

          {

            repayment = amount

                        + amount * rate * months / 12 / 100;

            Print(months);

            Print("\t");

            Print(repayment,FALSE,8);

            Print("\t");

        // C++ will also recognize the type of an expression

            Print(repayment / months,TRUE,8);

          }


      }

    return;


  }

//************************************************************

void Print(int i, int newline)

  {

    cout << i;

    if (newline)

      cout << endl;

  }

//************************************************************

void Print(float f, int newline,int width)

  {

    cout.precision(2);

    cout.setf(ios::showpoint);

    cout.setf(ios::fixed);

    cout.width(width);

    cout << f;

    if (newline)

      cout << endl;

  }

//************************************************************

void Print(char str[], int newline)

  {

    cout << str;

    if (newline)

      cout << endl;

  }

==========================

// Filename: OVERRIDE.CPP

// Overrides the default method of passing

// a nonarray variable. This program passes

// a nonarray variable by address and,

// therefore, requires some extra notation.

#include <iostream.h>


void ChangeIt(int *i, char c);


void main()

{

  int i;

  char c;


  // Assign two values in main()

  i = 10;

  c = 'X';


  cout << "Before leaving main(), i and c are "

       << i << " and " << c << endl;


  ChangeIt( &i, c);   // Pass the integer by address


  cout << "Back in main(), i and c are "

       << i << " and " << c << endl;

  return;

}

//*********************************************************

void ChangeIt(int *i, char c)

{

  *i = 26;

  c = 'p';   // main()'s version of c won't change

             // but we can still use c locally


  return;   // Return to main()

}


Tidak ada komentar: