際際滷

際際滷Share a Scribd company logo
Step 1(List down all header file)


        #include "stdafx.h"
        #include<iostream>
        #include<fstream>
        #include<iomanip>
        #include<string>
        #include<cstdlib>
        using namespace std;
Step 2(Identify all possible
     functions)
void viewRecords();//function to view records
void insertRecords();//function to insert
 records
void outputline(int, const string, const string); //
 prototype of method to output
Step 3(Specify your main content)
  int main()
  {
  int ch;


  cout<<"File Processing :: Input - Output"<<endl;
  cout<<"[1] - View Recordsn";
  cout<<"[2] - Insert Records";
  givechoice:
  cout<<"nchoice: ";
  cin>>ch;
  switch(ch)
  {
  case 1: cout<<"nView Recordsn";
  viewRecords();
  break;
  case 2: cout<<"nInsert Recordsn";
  insertRecords();
  break;
  default: cout<<"Invalid"; goto givechoice;
  }
  }
Srep 4 (Outline of the output)
//this will iterate until there's data to read
void outputline(int studentid, const string
  name, const string course)
{
cout<<left<<setw(15)<<studentid<<left<<
  setw(17)<<name<<left<<setw(10)<<cou
  rse<<endl;
}
Step 5(Output Process)
   /*-------------------insert records------------------------------------*/


   void insertRecords()
   {
   //open the file for input
   ofstream outFile;
   outFile.open("Student.txt", ios::app);
   //check if the file exists
   if (!outFile)
   {
   cerr<< "File could not be opened.";
   exit (1);
   }


   //prompt the user to enter data
   cout<<"Enter the 10-digit student number, name and program code."<<endl;
   cout<<"Enter end of file to end input."<<endl<<"?";


   int studentid;
   string name, course;
   //user will enter data
   while(cin>>studentid>>name>>course)
   {
   outFile << studentid <<" "<< name <<" "<< course<<endl;
   cout<<"?";
   }
   }
Step 6(View Record you input)
   //function to view records
   void viewRecords()
   {
   ifstream inFile;
   inFile.open("Student.txt"); //open file for output
   int studentid;
   string name, course;
   //print header
   cout<<left<<setw(15)<<"Student ID"<<setw(17)<<"Student
      Name"<<setw(10)<<"Prog Code"<<endl;

   //output the content of the file by calling the function
   while(inFile>>studentid>>name>>course)
   outputline(studentid, name, course);
   }

More Related Content

Sample file processing

  • 1. Step 1(List down all header file) #include "stdafx.h" #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<cstdlib> using namespace std;
  • 2. Step 2(Identify all possible functions) void viewRecords();//function to view records void insertRecords();//function to insert records void outputline(int, const string, const string); // prototype of method to output
  • 3. Step 3(Specify your main content) int main() { int ch; cout<<"File Processing :: Input - Output"<<endl; cout<<"[1] - View Recordsn"; cout<<"[2] - Insert Records"; givechoice: cout<<"nchoice: "; cin>>ch; switch(ch) { case 1: cout<<"nView Recordsn"; viewRecords(); break; case 2: cout<<"nInsert Recordsn"; insertRecords(); break; default: cout<<"Invalid"; goto givechoice; } }
  • 4. Srep 4 (Outline of the output) //this will iterate until there's data to read void outputline(int studentid, const string name, const string course) { cout<<left<<setw(15)<<studentid<<left<< setw(17)<<name<<left<<setw(10)<<cou rse<<endl; }
  • 5. Step 5(Output Process) /*-------------------insert records------------------------------------*/ void insertRecords() { //open the file for input ofstream outFile; outFile.open("Student.txt", ios::app); //check if the file exists if (!outFile) { cerr<< "File could not be opened."; exit (1); } //prompt the user to enter data cout<<"Enter the 10-digit student number, name and program code."<<endl; cout<<"Enter end of file to end input."<<endl<<"?"; int studentid; string name, course; //user will enter data while(cin>>studentid>>name>>course) { outFile << studentid <<" "<< name <<" "<< course<<endl; cout<<"?"; } }
  • 6. Step 6(View Record you input) //function to view records void viewRecords() { ifstream inFile; inFile.open("Student.txt"); //open file for output int studentid; string name, course; //print header cout<<left<<setw(15)<<"Student ID"<<setw(17)<<"Student Name"<<setw(10)<<"Prog Code"<<endl; //output the content of the file by calling the function while(inFile>>studentid>>name>>course) outputline(studentid, name, course); }