Site hosted by Angelfire.com: Build your free website today!

Program 28
C++

“Space is almost infinite.  As a matter of fact we think it is infinite.”

                                                                Former Vice-President Dan Quayle

 
C++

Program 28

K. Dzwonkiewicz

 

This program will use disk files (data read/written to your disk) so make sure your disk has free space.

 

You need to keep track of test scores for a C++ class where you are the teacher’s aide.

 

Have your program ask for test scores for students in the class.  You don’t know how many there are in the class, so use a do…while loop.  Enter –999 to quit the data entry.  Add them as you go so you can get the class average as well.

 

What you need to do is write these scores to a data file on your disk called class.dat.  This must be done inside the loop for the scores and after the loop for the average.

 

Don’t forget to close the data file when you are done.

 

An American poet, Jean Dunnington, has created a compound genre: the snowballing iceogram.  Here, for example, is a dirge for a Scottie drowned at a picnic which includes an ironic digression on the indifference of nature to human sorrow:

 

                O.

                On!

                One

                Done.

                Drone

                Droned.

                Drowned.

 

 
Sample run…                                           

Please enter test score…

75

85

95

65

65

78

45

88

98

90

95

-999 DONE

 
                      

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Make sure you use the proper format for opening the file by having error checking.

 

Examine your disk to see if the data has been stored.  

In an often-quoted story, “Caves of Steel,” Isaac Asimov formulated three laws for robots.  All three are defensive, reflecting fear that robots may turn into Golems:

 

1.        A robot may not injure a human being or through inaction allow a human being to come to harm.

2.        A robot must obey the orders given it by human beings except where such orders would conflict with the First Law.

3.        A robot must protect its own existence as long as such protection does not conflict with the First or Second Law.

 
                                     

 

 

 

 

 

 

Examples

#include<iostream.h>

#include<iomanip.h>

#include<fstream.h>

      ofstream socks;  // since files are outside your program

   ifstream shoes;  // declare them globally

write data

 
 


void main()

{

int x, num;

float dec2;

socks.open("a:\\output.cpp",ios::out);// ios not required... I still use it in memory of R&R

      for(x=0;x<6;++x)

   socks<<x<<' ';//numbers need to be separatedfromeachother

      socks<<12.34567;  // stores 12.3457 unless we format it with socks.setf

   socks.close();

cout.setf(ios::floatfield,ios::fixed);

cout.setf(ios::showpoint);

retrieve data

 
      shoes.open("a:\\output.cpp",ios::in);

   for(x=0;x<6;++x)

   {

   shoes>>num;          // retrieves 6 numbers

   cout<<num<<endl;

   }

    shoes>>dec2;  // retrieves the last

    cout<<dec2;

    shoes.close();

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#include<iostream.h>

#include<iomanip.h>

#include<fstream.h>

      ofstream socks;

   ifstream shoes;

 

void main()

{

      int x,num;

   float dec2;

   socks.open("a:\\output.dat",ios::out);//ios not required...I use it in memory K&R

   if(!socks)

      {

            cout<<"CAN'T OPEN THE FILE"<<endl;

write data

 
      }

      else

      {           for(x=0;x<6;x++)

            socks<<x<<' ';                // numbers need to be separatedfromeachother

         socks<<12.34567;     //stores 12.3457 unless we format it with socks.setf

      socks.close();                      // close the stream!!!!!!

 

 

   shoes.open("a:\\output.dat",ios::in);

      if(!shoes)

            cout<<"CAN'T OPEN THIS FILE TO GET DATA"<<endl;

      else

      {

       for(x=0;x<6;x++)

                  {

            shoes>>num;                   // retrieves 6 numbers

retrieve data

 
                        cout<<num<<endl;

            }

            shoes>>dec2;  // retrieves the last

                  cout<<dec2;

   shoes.close();                         // close the stream!!!!!!!

      } // end of read shoes

            } // end of write socks