CS201 Assignment No. 4 solution
Monday, June 27, 2011 Posted In CS and IT Edit ThisSemester: Spring 2011
CS201: Introduction to Programming
Total Marks: 20
Due Date:29/06/2011
Instructions Please read the following instructions carefully before submitting assignment: It should be clear that your assignment will not get any credit if: § The assignment is submitted after due date. § The submitted assignment does not open or file is corrupt. § All types of plagiarism are strictly prohibited. Note: You have to upload only .cpp file. Assignment in any other format (extension) will not be accepted. If you will submit code in .doc (Word document) you will get zero marks. Objective The objective of this assignment is to provide hands on experience of using § Streams in C/C++ Guidelines: § Code should be properly aligned and well commented. § Follow c/c++ rules while writing variables names, function names etc § Use only dev-C++ for this assignment. |
Assignment:
Problem Statement: Printing Electricity Bill Detailed Description: Write a C++ program in which you have to: 1. Create a text file “Electricity_Bill”. 2. Write following data in it: |
--------------------------------------------------------------------------- 3. Open file and read data from it and display it on the screen. Sample Output |
:::::::::::::::::::::::::::::::::::::::::::::::::::::
Solution:
#include<iostream.h>
#include<fstream.h>
main()
{
ifstream inFile;
char inputFileName[]="Electricity_Bill.txt";
const int MAX_CHAR_TO_READ = 100;
char completeLineText[MAX_CHAR_TO_READ];
inFile.open(inputFileName);
if(!inFile)
{
cout <<"Can't open input file named " << inputFileName << endl;
exit(1);
}
while(!inFile.eof())
{
inFile.getline(completeLineText,MAX_CHAR_TO_READ);
cout << completeLineText << endl;
}
inFile.close();
system("pause");
}
:::::::::::::::::::::::::::::::::::::::::::::
Electricity Consumer Bill
---------------------------------------------------------------------------
Reference No Tariff Load Old A/C No
123456789123456 2 1 123456789123456
---------------------------------------------------------------------------
Name and Address
XYZ Lahore Pakistan
---------------------------------------------------------------------------
Reading MF Total Unit Consumed Total Cost of electricity
55671 1 328 9999
---------------------------------------------------------------------------
Month Units Bill Current Bill 10732
Jan-11 312 5000 Arrears 0
Feb-11 312 5000 Tariff Subsidy NA
Mar-11 312 5000 Payable within Duedate 10732
---------------------------------------------------------------------------