Rabu, 01 April 2026

C++ From Year 1995 File From "F"

 // Filename: FIXCHNBG.CPP

// Stores the alphabet in a file, reads two letters from it,

// and changes those letters to x


#include <fstream.h>


void main()

{

  fstream fp;

  char ch;   // Holds A through Z


  // Opens in update mode so that you can

  // read file after writing to it

  fp.open("alph.txt", ios::in | ios::out);

  if (!fp)

  {

    cout << "*** Error opening file ***";

    return;

  }

  for (ch = 'A'; ch <= 'Z'; ch++)

    { fp << ch; }   // Writes letters

  fp.seekg(8L, ios::beg);  // Skips eight letters, points to I

  fp >> ch;

  // Changes the I to an x

  fp.seekg(-1L, ios::cur);

  fp << 'x';

  cout << "The first character is " << ch << endl;

  fp.seekg(16L, ios::beg);   // Skips 16 letters, points to Q

  fp >> ch;

  cout << "The second character is " << ch << endl;

                             // Changes the Q to an x

  fp.seekg(-1L, ios::cur);

  fp << 'x';

  fp.close();

  return;

}

===========================
// Filename: FLOTARPT.CPP
// Defines a floating-point array and then
// accesses elements from the array using
// pointer notation
#include <iostream.h>

void main()
{
  float ara[6] = {11.1, 22.2, 33.3, 44.4, 55.5, 66.6};
  int ctr;   // for-loop counter

  // First, print the array using subscripts
  cout << "Here is the array using subscripts:" << endl;
  cout.precision(1);
  cout.setf(ios::showpoint);
  cout.setf(ios::fixed);
  for (ctr = 0; ctr < 6; ctr++)
    { cout << ara[ctr] << ' '; }

  // Print the array using simple pointer notation
  cout << endl << endl 
       << "Here is the array using pointers:" << endl;
  for (ctr = 0; ctr < 6; ctr++)
    { cout << *(ara + ctr) << ' '; }

  // You can even combine pointer and array notation!
  cout << endl << endl 
       << "Here is the array using a combination:" << endl;
  cout << (ara + 0)[0] << ' ';  // ara[0]
  cout << (ara + 1)[0] << ' ';  // ara[1]
  cout << (ara + 0)[2] << ' ';  // ara[2]
  cout << (ara + 0)[3] << ' ';  // ara[3]
  cout << (ara + 3)[1] << ' ';  // ara[4]
  cout << (ara + 2)[3] << ' ';  // ara[5]
  return;
}
======================
// Filename: FORAVG.CPP
// A for loop asks the user for five values.
// As the user enters each value, a compound
// assignment statement adds the values.
// After all five values have been entered and
// the loop ends, the program prints an average.
#include <iostream.h>
void main()
 {
   float value, avg;
   float total = 0;

   cout << endl << "** An Average Program **"
        << endl << endl;

   // Loop five times
   for (int count = 0; count < 5; count++)
     { 
       cout << "What is the next value? ";
       cin >> value;
       total += value;   // Add to the total variable
     }

   // Now, compute and print the average
   avg = total / 5.0F;
   cout.precision(1);
   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout << endl << "The average of your values is " 
        << avg << endl;
   return;
 }
=======================
// Filename: FORAVG2.CPP
// A for loop asks the user for values as determined by the
// #define value. As the user enters each value, a compound
// assignment statement adds the values. After the loop ends
// and all three values are entered, the program prints an average.
#include <iostream.h>

void main()
{
  const int NUM = 5;  // A constant variable that can't change
  int count;          // The for loop's control variable
  float value, avg;
  float total = 0;

  cout << "** An Average Program **" << endl << endl;

  // Loop num times
  for (count=0; count<NUM; count++)
    { cout << "What is the next value? ";
      cin >> value;
      total += value;    // Add to the total variable
    }

  // Now, compute and print the average
  avg = total / (float)NUM;
  cout << endl << "The average of your values is " << avg << endl;
  return;
}
===================
// Filename: FORBREAK.CPP
// Prints a dog's name 10 times
// (or less if the user dictates)
#include <iostream.h>
void main()
{
  char ans;         // Will hold a Y or N answer
  int count;        // The loop control variable
  char dName[25];   // Will hold the dog's name

  cout << "What is your dog's name? ";
  cin >> dName;

  cout << "I'll now print the name ten times (maybe)..." << endl;

  for (count = 0; count < 10; count++)
    { cout << dName << endl;
      cout << "Do you want to see the name again (Y/N)? ";
      cin >> ans;
      if ((ans == 'N') || (ans == 'n'))
        { 
          break;  // Terminate early
        }  
    }   // Iterate again if not broken out of the loop

  cout << "That's a nice dog!";
  return;
}

Tidak ada komentar: