Rabu, 01 April 2026

C++ From Year 1995 File From "E"

 // Filename:  EDIT.CPP

#include <iostream.h>

void main()

{

cout << "Visual C++ is easy!";

}

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

// Filename: ERAS.CPP

// Erases the file specified by the user


#include <stdio.h>

#include <iostream.h>


void main()

{

  char filename[255];


  cout << "What is the filename you want me to erase? ";

  cin >> filename;

  if (remove(filename))

    cout << "*** I could not remove the file ***"; 

  else

    cout << "The file " << filename

         << " is now removed";

}

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

// Filename: EVNTOODD.CPP

// Changes the user's value to the next-highest odd number

#include <iostream.h>

void main()

 {

   int num;


   cout << "Enter an even number: ";

   cin >> num;


   num |= 1;   // Turn on the rightmost bit position

   cout << "The next-highest value is " << num << endl;


   return;

 }

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

// Filename: EXPRESS.CPP

// Computing expressions with C++

#include <iostream.h>


void main()

{

  int ans1, ans2, ans3;


  ans1 = 2 * 3 + 3 * 2;

  cout << "The first answer is " << ans1 << endl;

  ans2 = 20 + 20 / (5 + 5) % 2;

  cout << "The second answer is " << ans2 << endl;

  ans3 = ((2 + 3) + 6 * (3 + (4 - 2))) + 3;

  cout << "The third answer is " << ans3 << endl;

  return;

}


C++ From Year 1995 File From "D"

 // Filename: DATECHK.CPP

// Demonstrates a complete OOP program with class data

// Add simply error-checking to the member function code

// so that reasonable values are assigned to the date.

#include <iostream.h>


class Date {

   int day;

   int month;

   int year;

public:    // Everything that follows is accessible by main()

   void prDate();        // Member functions

   void setDay(int d);

   void setMonth(int m);

   void setYear(int y);

};


void Date::prDate()      // :: Tells C++ the owner class

{

   cout << "Date: " << day << "/" << month << "/"

        << year << "" << endl;

}

void Date::setDay(int d)

{

   if (d <= 31)

     { day = d; }

   else

     { cout << endl << "The day cannot be " << d << "!" << endl; }

}


void Date::setMonth(int m)

{

   if (m <= 12)

     { month = m; }

   else

     { cout << endl << "The month cannot be " << m << "!" << endl; }

}


void Date::setYear(int y)

{

   if (y >= 90)

     { year = y; }

   else

     { cout << endl << "The year cannot be " << y << "!" << endl; }

}


void main()

{

   Date today;         // Define a new active class variable

   // today.day = 20 is NOT possible!!! You must initialize

   // the data can only be initialized through member functions

   today.setDay(20);     // Initialize the day

   today.setMonth(6);    // Initialize the month

   today.setYear(1994);  // Initialize the year

   today.prDate();       // Print the date (You could'nt print the

                         // date's values via the dot operator!)

   return;

}



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

// Filename: DEFPNTS.CPP

// Defines several variables and pointers to those variables

#include <iostream.h>


void main()

{

  int     i1 = 14;    // Define and initialize an integer

  int*    ip1;        // Define a pointer to an integer

  int     i2 = 20;

  int*    ip2 = &i2;  // Define and initialize the pointer

  float   f = 92.345;

  float*  fp = &f;

  double  d;

  double* dp;


  ip1 = &i1;

  

  cout.precision(3);

  cout.setf(ios::showpoint);

  cout.setf(ios::fixed);

  

  cout << "i1 is " << i1 << " and *ip1 is also "

       << *ip1 << endl;

  

  cout << "i2 is " << i2 << " and *ip2 is also "

       << *ip2 << endl;

  

  cout << "f is " << f << " and *fp is also "

       << *fp << endl;

  

  *fp = 1010.10;

  

  cout << "After changing f through fp, " << endl;

  cout << "f is now " << f << " and *fp is also "

       << *fp << endl;                           

       

  dp = &d;

  *dp = 83949443.54333;   // Change dp

  

  cout << "d is now " << d << " and *dp is also "

       << *dp << endl;

  return;

}

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

// Filename: DISSECT.CPP

// Simple program for you to analyze


#include <iostream.h>


void main()

{

  int age, weight;   // These lines define four variables

  char initial;

  double salary;


  age = 13;   // Assigns an integer literal to age

  weight = age * 6;   // Assigns a weight based on a formula

  initial = 'D';      // All character literals are

                      // enclosed in single quotes

  salary = 200.50;    // salary requires a floating-point

                      // value because it was defined to

                      // be a floating-point

  salary = salary * 2.5;   // Changes what was in salary


  // Next line sends the values of the variables to the screen

  cout << age << ", " << weight << ", " << initial

       << ", " << salary;


}

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

// Filename: DOUSRAGE.CPP

// Uses a do-while loop to get an age from the user

#include <iostream.h>

void main()

{

  int age;


  // In the previous listing, two extra lines went here

  do

    { cout << "How old are you? ";   // Get the age

      cin >> age;

      if ((age < 5) || (age > 110))

        { cout << "I'm not sure that I believe you are "

               << age << endl;

          cout << "Try again..." << endl << endl; }

    } while ((age < 5) || (age > 110));   // Quit after good input


  // The program continues if the user entered a reasonable age

  if (age < 16)

    { cout << "Your curfew is at 11:30 pm" << endl;

      cout << "Have fun!" << endl;

    }

  else

    {

      cout << "You're old enough to drive, so be careful ";

      cout << "out there!" << endl;

    }

  return;

}

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


C++ From Year 1995 File From "C"

 // Filename: CFIRST.CPP

// Program displays a message on-screen

#include <iostream.h>

void main()

{

   cout << "I will be a C++ expert!";

}

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

// Filename: CH1.CPP

// Introduces get() and put()


#include <fstream.h>


void main()

{

  char  in_char;   // Holds incoming initial

  char first, last;   // Holds converted first and last initial


  cout << "What is your first name initial? ";

  cin.get(in_char);   // Waits for first initial

  first = in_char;

  cin.get(in_char);   // Ignores newline

  cout << "What is your last name initial? ";

  cin.get(in_char);   // Waits for last initial

  last = in_char;

  cin.get(in_char);   // Ignores newline

  cout << endl << "Here they are: " << endl;

  cout.put(first);

  cout.put(last);

  return;

}

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

// Filename: CHANGBUG.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 << endl << "*** Error opening file ***" << endl;

    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(-2L, 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(-2L, ios::cur);

  fp << 'x';

  fp.close();

}

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

// Filename: CHARFUNS.CPP

// Uses character arrays and character pointers

#include <iostream.h>

#include <string.h>


void main()

{

  char c;


  char * cpt0;   // A stand-alone character pointer

  char * cpt5 = "abcd";           // Points to 5 bytes

  char * cpt12 = "Programming";   // Points to 12 bytes

  char   ara27[27];               // Points to 27 bytes

  char * cpt27 = ara27;           // Points to 27 bytes


  cpt0 = "cpt0 is pointing to this string";

  cout << cpt0;   // No problem

  cpt0 = "A new string for cpt0";   // Still no problem

  cout << endl << cpt0 << endl;

  // You couldn't

  // strcpy(cpt0, "This is a string for cpt0 that is too long")

  // though!


  cout << "Please type your name (up to 12 characters): ";

  cin.get(cpt12, 12);   // Okay because of get() 12-char limit

  cout << "You typed " << cpt12 << endl << endl;


  // cin.get(cpt5, 12) wouldn't work either because

  // all characters after the fifth one would overwrite

  // memory not pointed to by cpt5


  // Fill the 27-character array

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

    { ara27[c - 65] = c;  }   // ASCII A is equivalent to decimal 65

  ara27[26] = '\0';;

  

  strcpy(cpt27, ara27);   // Okay because they point to

                          // the same number of bytes

  // strcpy(cpt12, ara27) would NOT be okay

  cout << "cpt27 contains: " << cpt27;

  return;

}

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

// Filename: CITYNAME.CPP

// Stores and prints a list of city names

#include <iostream.h>


void main()

{

  int ctr;


  char * cities[5] = {"San Diego", "Miami", "New York",

                      "Oklahoma City", "St. Louis"};


  // Print the cities

  // Anywhere a character array can appear, so can the

  // elements from the cities array of pointers

  cout << "Here are the stored cities:" << endl;

  for (ctr = 0; ctr < 5; ctr++)

    { cout << cities[ctr] << endl; }

  cout << endl;


  // Change the cities with literals

  // These assignments store the address of

  // the string literals in the elements

  cities[0] = "Tulsa";

  cities[1] = "Boston";

  cities[2] = "Indianapolis";

  cities[3] = "Las Vegas";

  cities[4] = "Dallas";


  // Print the cities again using pointer notation

  cout << endl << "After changing the pointers:" << endl;

  for (ctr = 0; ctr < 5; ctr++)

    { cout << *(cities + ctr) << endl ; }

  cout << endl;


  return;

}

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

// Filename: CLASSPGM.CPP

// Demonstrates a complete OOP program with class data


#include <iostream.h>


class Date {

   int day;

   int month;

   int year;

public:    // Everything that follows is accessible by main()

   void prDate();        // Member functions

   void setDay(int d);

   void setMonth(int m);

   void setYear(int y);

};


void Date::prDate()      // :: Tells C++ the owner class

{

   cout << "Date: " << day << "/" << month << "/"

        << year << endl;

}


void Date::setDay(int d)

{

   day = d;

}


void Date::setMonth(int m)

{

   month = m;

}


void Date::setYear(int y)

{

   year = y;

}


void main()

{


   Date today;         // Define a new active class variable

   // today.day = 20 is NOT possible!!! You must initialize

   // the data can only be initialized through member functions

   today.setDay(20);     // Initialize the day

   today.setMonth(6);    // Initialize the month

   today.setYear(1994);  // Initialize the year

   today.prDate();       // Print the date (You couldn't print the

                         // date's values via the dot operator!)

   return;

}

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

// Filename: COMAVGE.CPP

// Program to compute the average of three values

#include <iostream.h>

void main()

{

  double g1, g2, g3;   // Variables to hold student grades


  cout << "What grade did the first student get? ";

  cin >> g1;


  cout << "What grade did the second student get? ";

  cin >> g2;


  cout << "What grade did the third student get? ";

  cin >> g3;


  double avg = (g1 + g2 + g3) / 3.0;   // Computes average


  cout << "The student average is " << avg;  // Prints average

  return;

}

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

// File name: CONDEST.CPP

// Shows construction and destruction

// of objects and simple memory management

//

#include <iostream.h>

#include <string.h>


class Name

  {

    public:   

      // Constructor

      Name();       

      // Destructor

      ~Name();  

      void SetName(const char * newName);

      const char * GetName() const;

    private:

      char * name;

  };

void main()

  {

    cout << "-- First line of main() --" << endl;

    Name firstName;

    firstName.SetName("firstName");

    Name * secondName = new Name;

    secondName->SetName("secondName");

    cout << "-- Before block --" << endl;

    // New block

      {

        cout << "-- First line of block --" << endl;

        Name thirdName;

        Name fourthName;

        thirdName.SetName("thirdName");   

        cout << "-------------------" << endl;

        cout << "Contents of objects" << endl;

        cout << firstName.GetName() << endl;

        cout << secondName->GetName() << endl;

        cout << thirdName.GetName() << endl;

        cout << fourthName.GetName() << endl;

        cout << "-------------------" << endl ;

        cout << "-- Last line of block --" << endl;

      }  // Block ends - third & fourth name destroyed

    cout << "-- After block --" << endl;

    delete secondName;

    cout << "-- Last line of main() --" << endl;

  }      // firstName goes;  

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

//

// Name  class function definitions

//


// Constructor        

Name::Name()

  {

    cout << "Constructor called" << endl;

    name = 0;

  }           


// Destructor

Name::~Name()

  {

    cout << "Destructor called ";

    cout << "name is " << GetName() << endl;

    delete name; // Delete on zero pointer is safe

  }          

  

// Member function to store a name                    

//

void Name::SetName(const char* newName)

  {

     // First, remove any name that might already exist

     // Use zero pointer to inidicate no name stored

     // C++ will not destroy storage on a zero pointer

     // "if (name)"

     delete [] name;                                    

     // Create new storage

     name = new char[strlen(newName) + 1]; // add 1 for

                                           // terminator

     strcpy(name,newName);  // Copy data into new name

  }


// Member function to get the stored name

// Coded to always return a safe value

const char * Name::GetName() const

  {

    if (name)

      return name;

    else

      return "No name exists";

  }

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

 // Filename: CONDIT.CPP

 // Finds the minimum and maximum values

 // from the user's entered values

 #include <iostream.h>

 void main()

 {

   int val1, val2, min, max;


   cout << "Enter a value: ";

   cin >> val1;


   cout << "Enter another value: ";

   cin >> val2;


   if (val1 < val2)

      { min = val1;

        max = val2; }

   else

      { max = val1;

        min = val2; }


   cout << endl << "Using if-else, the minimum is " 

        << min << " and the maximum is " << max << endl;


   min = (val1 < val2) ? val1 : val2;

   max = (val2 > val1) ? val2 : val1;


   cout << endl << "Using ?:, the minimum is " 

        << min << " and the maximum is " << max << endl;

   return;

 }

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

// Filename: CONTACT.CPP

// Allow the display and entry of contacts

//

#include <iostream.h>


struct Contact

  {

     char name[50];

     int  age;

     char phoneNo[15];

  };                         

  

// Maximum number of contacts

const int MAXCONTACTS = 10;


// Prototypes

int AddContact(Contact& contact);

void DisplayContact(const Contact& contact);  

void DisplayAllContacts(const Contact contacts[MAXCONTACTS],int count);


void main()  

  {

    Contact contacts[MAXCONTACTS];

    int count = 0;

    while( count < MAXCONTACTS )// while more room

      {

         if (AddContact(contacts[count]))

           count++;

         else

           break;

      }

    DisplayAllContacts(contacts,count);

  }                                         

//

// DisplayContact displays a single name

//

void DisplayContact(const Contact& contact)  

  {

    cout << endl;

    cout << "Name    : " << contact.name << endl;

    cout << "Age     : " << contact.age << endl;

    cout << "phone no: " << contact.phoneNo << endl;

    cout << endl;

  }

//

// DisplayAllContacts calls DisplayContact to show

// all entered names

//

void DisplayAllContacts(const Contact contacts[MAXCONTACTS],int count)

  {

    for (int i = 0; i < count; i++)

      {

        DisplayContact(contacts[i]);

      }    

  } 

//

// Add contact asks for one contact

//

int AddContact(Contact& contact)

  {

    char answer;

    cout << "Do you want to add a contact [Y]es/[N]o: ";

    cin >> answer;

    cin.ignore(); // skip rubbish

    

    if (answer == 'y' || answer == 'Y')

      {          

        cout << "Name: ";

        cin.getline(contact.name,30);

        cout << "Phone no: ";

        cin.getline(contact.phoneNo,15);

        cout << "Age ";

        cin >> contact.age;

        cin.ignore();

        return 1;   // Added name ok

      }

    else

      return 0;     // Did not add name

   }

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

// Filename: CONTACTO.CPP

// Allow the display and entry of contacts

// using OOP techniques

//

#include <iostream.h>


class Contact

  {

    public:          

     char name[50];

     int  age;

     char phoneNo[15];

      

     // Member function declarations (prototypes)

     void AddContact();

     void DisplayContact() const;

  };                         

  

// Maximum number of contacts

const int MAXCONTACTS = 10;


// Global Prototypes

void DisplayAllContacts(const Contact contacts[MAXCONTACTS],int count);


void main()  

  {

    Contact contacts[MAXCONTACTS];

    char answer;

    int count = 0;

    while( count < MAXCONTACTS )// while more room

      {

         // Ask the user if they want to continue

         cout << "Do you want to add a contact [Y]es/[N]o: ";

         cin >> answer;

         cin.ignore(80,'\n'); // skip rubbish

    

         if (answer == 'y' || answer == 'Y')

           {

             // Add the information

             contacts[count].AddContact();

             count++;

           }

         else

           break;

      }

    

    DisplayAllContacts(contacts,count);

    

  }                                         

//

// DisplayContact displays a single name

//

void Contact::DisplayContact() const 

  {

    cout << endl;

    cout << "Name    : " << name << endl;

    cout << "Age     : " << age << endl;

    cout << "phone no: " << phoneNo << endl;

    cout << endl;

  }

//

// DisplayAllContacts calls DisplayContact to show

// all entered names

//

void DisplayAllContacts(const Contact contacts[MAXCONTACTS],int count)

  {

    for (int i = 0; i < count; i++)

      {

        contacts[i].DisplayContact();

      }    

  } 

//

// Add contact asks for one contact

//

void Contact::AddContact()

  {

    cout << "Name: ";

    cin.getline(name,30);

    cout << "Phone no: ";

    cin.getline(phoneNo,15);

    cout << "Age ";

    cin >> age;

    cin.ignore();

  }

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

// Filename: CONTINU2.CPP

// Use continue when data is missing

// and break when the user finishes early

#include <iostream.h>

void main()

{

  int count;       // Loop control variable

  float dSales;    // Will hold each day's sales

  float wSales=0;  // Weekly total


  // Set up the loop for a possible of five iterations

  for (count=0; count<5; count++)

    {

      cout << "Enter the sales for day #" << (count+1)

           << " (-99 for none): ";

      cin >> dSales;

      if (dSales < -98.9)  // User entered -99

        { continue; }

      if (dSales < 0.0)    // User entered -1

        { break; }


      // The following statement executes ONLY if a

      // valid daily sales total were just entered

      wSales += dSales;

    }


  // Print the week's total

  cout.precision(2);

  cout.setf(ios::showpoint);

  cout.setf(ios::fixed);

  cout << "The weekly total is $" << wSales ;

  return;

}

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

// Filename: CONTINUE.CPP

// Uses continue when data is missing

#include <iostream.h>


void main()

{

  int count;          // Loop control variable

  float dSales;       // Will hold each day's sales

  float wSales = 0;   // Weekly total


  // Set up the loop for a possible five iterations

  for (count = 0; count < 5; count++)

    {

      cout << "Enter the sales for day #" << (count + 1)

           << " (-99 for none): ";

      cin >> dSales;

      if (dSales < 0.0)

        { continue; }


      // The following statement executes ONLY if a

      // valid daily sales total was just entered

      wSales += dSales;

    }


  // Print the week's total

  cout.precision(2);

  cout.setf(ios::showpoint);

  cout.setf(ios::fixed);

  cout << endl << "The weekly total is $" 

       << wSales << endl;

  return;

}

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

// Filename: COUNTRY.CPP

// Allocates ten country names on the heap.

#include <iostream.h>

#include <string.h>


void allMem(char * countries[10]);

void initMem(char * countries[10]);

void prMem(char * countries[10]);

void freeMem(char * countries[10]);


void main()

{

  char * countries[10];  // Pointers to each country name

  allMem(countries);     // Allocate the memory

  initMem(countries);    // Store the country names

  prMem(countries);      // Print the country names

  freeMem(countries);    // Deallocate the memory


  return;

}

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

void allMem(char * countries[10])

{

  int ctr;

  for (ctr=0; ctr<10; ctr++)

  { countries[ctr] = new char [15]; }

  return;

}

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

void initMem(char * countries[10])

{

  strcpy(countries[0], "Italy");

  strcpy(countries[1], "Canada");

  strcpy(countries[2], "New Zealand");

  strcpy(countries[3], "France");

  strcpy(countries[4], "Chile");

  strcpy(countries[5], "Japan");

  strcpy(countries[6], "Brazil");

  strcpy(countries[7], "Mexico");

  strcpy(countries[8], "Greece");

  strcpy(countries[9], "Egypt");

  return;

}

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

void prMem(char * countries[10])

{

  int ctr;

  cout << "Here are the countries stored on the heap:" << endl;

  for (ctr=9; ctr>=0; ctr--)

  { cout << countries[ctr] << endl; }

  return;

}

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

void freeMem(char * countries[10])

{

  int ctr;

  for (ctr=0; ctr<10; ctr++)

  { delete countries[ctr]; }

  return;

}

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

/* Filename: CPPADV.CPP */

// Shows some advantages and shortcuts of C++

#include <iostream.h>

#include <string.h>


struct People {

  char * name;     // Will allocate each name

  int weight;

};


void getData(People dieter[4]);

void calcData(float & weight);

void prData(People dieter[4]);

void dealData(People dieter[4]);


main()

{


  cout << "** Calculating a diet's weight loss **" << endl << endl << endl;

  cout << "I'll ask for four people's names and";

  cout << " weights and" << endl << "then calculate their expected";

  cout << " weight loss after two weeks." << endl << endl;


  // Define the four structure variables in the middle

  // C++ programmers often use uppercase letters for structures

  People dieter[4];


  // Pass the variables to a function

  // to allocate each name and fill the variables

  getData(dieter);


  // Pass the weights, by reference, to a calculate function

  for (int ctr=0; ctr<4; ctr++) // Don't redefine control variable

    {  // Pass just the weight of each array variable

       // Due to a structure-passing conversion, save

       // the structure's weight in a stand-alone variable

       // before passing it. You cannot pass the middle of

       // a structure by reference.

       float passWeight = dieter[ctr].weight;

       calcData(passWeight);

       calcData(passWeight);

       dieter[ctr].weight = passWeight;  }

  // Pass the variables to a print function

  prData(dieter);


  // Pass the variables to deallocate names

  dealData(dieter);


  return 0;

}

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

void getData(People dieter[4])

{

  char buffer[80];   // For temporary input

  for (int ctr=0; ctr<4; ctr++)

    { cout << "What is dieter #" << (ctr+1) << "'s name? ";

      cin >> buffer;

      // Make name pointer in each variable point to heap

      dieter[ctr].name = new char[strlen(buffer)+1];

      strcpy(dieter[ctr].name, buffer);

      cout << "What is dieter #" << (ctr+1) << "'s weight? ";

      cin >> dieter[ctr].weight;

    }

  return;

}

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

void calcData(float & weight)

{

  weight *= .85;

  return;

}

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

void prData(People dieter[4])

{

  cout << endl << "Here are the predicted results after 3 weeks:";

  for (int ctr=0; ctr<4; ctr++)

  {

    cout << "" << endl << dieter[ctr].name << " will weigh "

         << dieter[ctr].weight;

  }

  return;

}

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

void dealData(People dieter[4])

{

  for (int ctr=0; ctr<4; ctr++)

    { delete dieter[ctr].name; }

  return;

}

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

// Filename: CRREAD.CPP

// Creates a file then reads it


#include <fstream.h>



void main()

{

  fstream myFile;   // Inputs file pointer

  char inChar;


  myFile.open("c:\\data.txt", ios::out | ios::in);

  if (!myFile)

  {

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

    return;

  }

  myFile << "Line 1 in DATA.TXT" << endl;

  myFile << "Line 2 in DATA.TXT" << endl;

  myFile << "Line 3 in DATA.TXT" << endl;

  myFile << "Line 4 in DATA.TXT" << endl;


  myFile.seekg(0L, ios::beg);

  cout << "The file:" << endl;

  while (myFile.get(inChar))

    { cout << inChar; }   // Outputs characters to the screen



  myFile.close();

  return;

}


C++ From Year 1995 File From "B"

 // Filename: BREXIT.CPP

// Demonstrates both break and return

#include <iostream.h>


void main()

{

  int count=0;

  char ans;


  // The following loop prints "Hi" five times only

  // as long as the user keeps requesting the word.

  // Whether the user terminates the loop early or

  // not, the program continues executing.

  while (count < 5)

    { 

      cout << "Hi" << endl;

      cout << "Do you want to see it again (Y/N)? ";

      cin >> ans;

      if ( ans == 'N' || ans == 'n') 

        { 

          break; 

        }

      count++;

    }


  cout << endl;


  // The following loop prints "Bye" five times only

  // as long as the user keeps requesting the word.

  // The difference here from the previous section

  // is that the entire program, not just the loop,

  // terminates if the user requests termination.


  count = 0;

  while (count < 5)

    { 

      cout << "Bye" << endl;

      cout << "Do you want to see it again (Y/N)? ";

      cin >> ans;

      if (ans == 'N' || ans == 'n')

        { 

          return; // Exit current function 

        }

     count++;

    }


  // The following cout executes ONLY if the user let

  // the previous loop execute to its natural termination

  cout << "That's all, folks!" << endl;


  return;   // Return to QuickWin

}


C++ From Year 1995 File From "A"

 // Filename: 1STLONG.CPP

// Longer C++ program that demonstrates comments,

// variables, constants, and simple input/output

#include <iostream.h>

void main()

  {

    int i, j;    // These three lines declare four variables

    char c;

    float x;


    i = 4;       // i is assigned an integer literal

    j = i + 7;   // j is assigned the result of a computation

    c = 'A';     // Enclose all character literals

                 // in single quotation marks


    x = 9.087;   // x is a floating-point value


    x = x * 12.3F;// Overwrites what was in x

                  // with something else

                  // (The F stops a conversion warning forcing

                  // the literal to be floating point)


    // Sends the values of the four variables to the screen

    cout << i << ", " << j << ", " << c << ", " << x << endl;


    return;   // Not required, but helpful

  }

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

// Filename: ABS.CPP

// Computes the difference between two ages

#include <iostream.h>

#include <math.h>

void main()

{

  float age1, age2, diff;

  cout << endl << "What is the first child's age? ";

  cin >> age1;

  cout << "What is the second child's age? ";

  cin >> age2;


  // Calculates the positive difference

  diff = age1 - age2;

  diff = fabs(diff);   // Determines the absolute value


  cout << endl << "They are " << diff << " years apart.";

  return;

}

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

// Filename: AD1.CPP

#include <iostream.h>

#include <ctype.h>


void main()

{

  if (isalpha('2'))

    { cout << "Yes"; }

  else

    { cout << "No"; }

  return;

}

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

// Filename: AD2.CPP

#include <iostream.h>

#include <ctype.h>


void main()

{

  char ch = 'a';

  if (islower(ch))

    { cout << "Yes"; }

  else

    { cout << "No"; }

  return;

}

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

// Filename: AD3.CPP

#include <iostream.h>

#include <ctype.h>


void main()

{

  char ch = 'a';

  if (isupper(ch))

    { cout << "Yes"; }

  else

    { cout << "No"; }

  return;

}

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

// Filename: AD4.CPP

#include <iostream.h>

#include <ctype.h>


void main()

{

  char ch = 'a';

  if (isdigit(ch))

    { cout << "Yes"; }

  else

    { cout << "No"; }

  return;

}

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

// Filename: ALCHANGE.CPP

// Stores the alphabet in a file, changes three letters from it,

// and then prints the file


#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("c:\\letters.txt", ios::in | ios::out);

  if (!fp)

  {

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

    return;

  }

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

    { fp << ch; }   // Writes letters

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

  fp >> ch;

  // Changes the H to an *

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

  fp << '*';

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

  fp >> ch;

  // Changes the P to an *

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

  fp << '*';

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

  fp >> ch;

  // Changes the T to an *

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

  fp << '*';

  fp.seekg(0L, ios::beg);

  while (fp.get(ch))

    { cout << ch; }   // Outputs characters to the screen


  fp.close();

}

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

// Filename: ALCHECK.CPP

// Asks the user for the number of checks written

// last month, then allocates that many floating-point

// values. The user then enters each check into the

// allocated array and the program prints the total

// after all checks are entered.

#include <iostream.h>


int   HowMany();

void  GetChecks(int noOfChecks, float * theChecks);

float GetTotal(int noOfChecks, const float * theChecks);


void main()

{

  int noOfChecks;

  float total;

  float * theChecks;  // Will point to allocated memory


  cout << "** Monthly Checkbook Program **" << endl << endl;

  

  noOfChecks = HowMany();          // Ask the user how many checks

  

  // Allocate the memory, 1 float per check

  theChecks = new float [noOfChecks];

  GetChecks(noOfChecks, theChecks);          // Get the values

  total = GetTotal(noOfChecks, theChecks);   // Add them up

  cout.precision(2);

  cout.setf(ios::showpoint);

  cout.setf(ios::fixed);

  cout << endl << endl << "Your total was $" << total

       << " for the month." << endl;

  delete [] theChecks;

  return;

}

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

int HowMany()

{

  int ans;  // To hold the cin value

  cout << "How many checks did you write last month? ";

  cin >> ans;

  return (ans);

}

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

void GetChecks(int noOfChecks, float * theChecks)

{

  int ctr;

  // No need or vehicle for passing allocated memory. The

  // memory does not go away between functions or blocks.

  cout << endl << "You now must enter the checks, one at a time."

       << endl << endl;

  for (ctr=0; ctr< noOfChecks; ctr++)

  {

    cout << "How much was check " << (ctr+1) << " for? ";

    cin >> theChecks[ctr];  // Store value on the heap

  }

  return;

}

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

float GetTotal(int noOfChecks, const float * theChecks)

{

  // Add up the check totals

  int ctr;

  float total = 0.0;

  for (ctr=0; ctr<noOfChecks; ctr++)

  {

    total += theChecks[ctr];

  }

  return total;

}

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

// Filename: ALPH.CPP

// Stores the alphabet in a file, then reads

// two letters from it


#include <fstream.h>



void main()

{

  char ch;   // Holds A through Z


  // Opens in update mode so that you can

  // read file after writing to it

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

  if (!fp)

  {

    cout << endl << "*** 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;

  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;

}

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

// Filename: AP1.CPP

// Adds three names to a disk file


#include <fstream.h>



void main()

{

  ofstream  fp;

  fp.open("C:\\NAMES.DAT", ios::app);   // Adds to file

  fp << "Johnny Smith" << endl;

  fp << "Laura Hull" << endl;

  fp << "Mark Brown" << endl;

  fp.close();   // Releases the file

}

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

// Filename: ARAEQUAL.CPP

#include <iostream.h>

void main()

{

  char dogName[20];   //  Reserves space for the dog's name

  dogName = "Ross";   // INVALID!

  cout << dogName;   // The program will never get here

  return;

}

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

// Filename: AREAPASS.CPP

// Contains a function that computes a circle's area

#include <iostream.h>


double computeArea(double radius);


void main()

{

  double radius;

  double area;


  cout << "What is the circle's radius? ";

  cin >> radius;


  area = computeArea(radius);

  cout << "The area is " << area << endl;


  return;

}

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

double computeArea(double radius)

{

  const float PI = 3.14159;

  return (PI * (radius * radius));

}

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

// Filename: ARRHEAP.CPP

// Allocates an array of heap pointers

#include <iostream.h>


void  AllMemory(float * cities[5]);

void  GetCity(float * cities[5]);

float CalcCity(float * cities[5]);

void  FreeAll(float * cities[5]);


void main()

{

  float * cities[5];     // Five city's worth of data

  float avg=0.0;

  AllMemory(cities);

  cout.precision(2);     // Ensure that dollar

  cout.setf(ios::fixed); // amounts display

  cout.setf(ios::showpoint);


  GetCity(cities);

  avg = CalcCity(cities);// Total each city

  avg /= 15.0F;          // Calculate average from total

  cout << endl << "The average is $" << avg << endl;

  

  FreeAll(cities);       // Why not a simple delete[]?


}

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

void AllMemory(float * cities[5])

{

  // Allocate each array's three values

  int ctr;

  for (ctr=0; ctr<5; ctr++)

    { cities[ctr] = new float [3]; }

}

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

void GetCity(float * cities[5])

{

  // This function gets the total number

  // of values for each city. Each city has 3

  // salespeople covering the territories.

  int ctr1, ctr2;

  // Use a nested for-loop to get each city's values

  for (ctr1=0; ctr1<5; ctr1++)

    {  cout << "City #" << (ctr1+1) << ":" << endl;

       for (ctr2=0; ctr2<3; ctr2++)

        { 

          cout << "What is value #" << (ctr2+1) << "? ";

          cin >> cities[ctr1][ctr2];

        }

    }

}

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

float CalcCity(float * cities[5])

{

  // Add up the total sales in each city

  int ctr1, ctr2;

  float totalCity=0.0, total=0.0;

  cout << endl;

  for (ctr1=0; ctr1<5; ctr1++)

 {

     for (ctr2=0; ctr2<3; ctr2++)

      { 

        float* &tempCity = cities[ctr1];

        totalCity+= tempCity[ctr2];

      }

     cout << "City #" << (ctr1+1) << " total is $"

          << totalCity << endl;

     total += totalCity; // Add to grand total

     totalCity = 0.0;    // Zero for next city

  }

  return total;

}

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

void FreeAll(float * cities[5])

{

  // Free each array's three values

  int ctr;

  for (ctr=0; ctr<5; ctr++)

    { delete cities[ctr]; }

  return;

}

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

// Filename: ARRTEMP.CPP

// Fills an array with seven temperature readings, converts

// those readings to Celsius, and prints the results

#include <iostream.h>


void FillTemps(float temps[7]);

void CalcCelsius(float temps[7]);

void PrintCelsius(float temps[7]);


void main()

{


  float temps[7];   // The user will initialize the array


  FillTemps(temps);     // Get the seven values from the user

  CalcCelsius(temps);   // Convert the values to Celsius

  PrintCelsius(temps);  // Print the Celsius readings


  return;

}

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

void FillTemps(float temps[7])

{

  int ctr;

  cout << "** Temperature Conversion **" << endl;

  cout << "----------------------------" << endl << endl;

  for (ctr = 0; ctr < 7; ctr++)

    { 

      cout << "What is the Fahrenheit reading for day #"

           << (ctr+1) << "? ";

      cin >> temps[ctr];

    }

  return;

}

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

void CalcCelsius(float temps[7])

{

  // Change each temperature to Celsius

  int ctr;

  for (ctr = 0; ctr < 7; ctr++)

    {

      // The Fs after the numbers are floats not Fahrenheits

      temps[ctr] = (temps[ctr] - 32.0F) * (5.0F / 9.0F);

    }

  return;

}

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

void PrintCelsius(float temps[7])

{

  // Print the Celsius readings

  int ctr;

  cout.precision(1);

  cout.setf(ios::showpoint);

  cout.setf(ios::fixed);

  cout << endl << "Here are the equivalent Celsius readings:"

       << endl << endl;

  for (ctr = 0; ctr < 7; ctr++)

    { 

      cout << "Day #" << (ctr + 1) << ": "

           << temps[ctr] << endl;

    }

  cout << endl << "Stay warm!" << endl;

  return;

}

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

// Filename: ARRTEMP2.CPP

// Fills an array with temperature readings, converts

// those readings to Celsius, and prints the results.


#include <iostream.h>


void FillTemps(double temps[]);

void CalcCelsius(double temps[]);

void PrintCelsius(double temps[]);


const int DAYS  = 21;


void main()

{


  double temps[DAYS];   // The user will initialize the array


  FillTemps(temps);     // Get the values from the user

  CalcCelsius(temps);   // Convert the values to Celsius

  PrintCelsius(temps);  // Print the Celsius readings


  return;

}

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

void FillTemps(double temps[DAYS])

{

  int ctr;

  cout << "** Temperature Conversion **" << endl;

  cout << "----------------------------" << endl << endl;

  for (ctr = 0; ctr < DAYS; ctr++)

    { cout << "What is the Fahrenheit reading for day #"

           << (ctr+1) << "? ";

      cin >> temps[ctr];

    }

  return;

}

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

void CalcCelsius(double temps[DAYS])

{

  // Change each temperature to Celsius

  int ctr;

  for (ctr = 0; ctr < DAYS; ctr++)

    { temps[ctr] = (temps[ctr] - 32.0) * (5.0 / 9.0); }

  return;

}

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

void PrintCelsius(double temps[DAYS])

{

  // Print the Celsius readings

  int ctr;

  cout.precision(1);

  cout.setf(ios::fixed);

  cout.setf(ios::showpoint);

  cout << endl << "Here are the equivalent Celsius readings:"

       << endl << endl;

  for (ctr = 0; ctr < DAYS; ctr++)

    { cout << "Day #" << (ctr+1) << ": "

           << temps[ctr] << endl; }

  cout << endl << "Stay warm!" << endl;

  return;

}

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

// AVETEMP.CPP

// Calculate the average of 10 days readings

// above zero


#include <iostream.h>


void main()

  {                             

 

    int count = 0;       // Count of readings above zero

    int sum = 0;         // Running total of above zero readings

    int temperature = 0; 

    float average = 0.0; 

    

    for (int day = 0; day < 10; day++) // for 10 days

      {

        cout << "Please input reading for day " 

             << day + 1  << ": " ;

        cin  >> temperature;

        if (temperature <= 0)

          continue;          // Skip count & sum code if too low

        sum += temperature;  // sum temperature readings

        count++;             // count

      }

    if (count > 0)  // check for zero divide

      {

        average = (float)sum / count;

        cout << "The average of " << count

             << " readings above zero was " << average

             << endl;

      }                 

    else

      {

        cout << "There were no above zero readings to average." << endl;

      }

  }

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