Q:WRITE A PROGRAM TO CALCULATE POWER OF A NUMBER
SLU:
SLU:
SLU:
/******************************************************************/
/* AAMIR
EJAZ */
/* F_43013 */
/******************************************************************/
#include<iostream>
using
namespace std;
void main()
{
int x,y;
char ch;
ab:
cout<<"enter
base : ";
cin>>x;
cout<<"enter
power : ";
cin>>y;
int n=1;
if(y=='0'){
cout<<"answer is
:1"<<endl;
}
else
{
while(y>0){
n=n*x;
y--;
}
cout<<"answer
is"<<n<<endl;
cout<<"do
you want to continue enter enter t for yes,n for no : ";
cin>>ch;
if((ch=='t')||(ch=='T'))
{
goto ab;
}
}}
Q:PROGRAM OF FILING:SLU:
#include
<iostream>
#include
<fstream>
void main ()
{
char
str[100];
ofstream
myfile ("example.txt");
if (!myfile)
{
cout<<“File could not open”
}
else
{
myfile << "This is a
line.\n";
myfile << "This is
another line.\n";
myfile.close();
}
while(myfile)
{
myfile.getine(str,80);
cout<<str;
}
}
Q:CODE OF FILING IN C+++:
SLU:
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
struct records
{
int roll_no;
int marks;
};
void main(void)
{
clrscr();
records rec;
char op;
int temp;
ofstream out_file("Example.dat",ios::app);
if(!out_file)
{
cout<<"File cannot open";
exit(1);
}
//////////////////////Writing to the file//////////////////
cout<<"Writing records to files";
cout<<"Enter records";
do
{
clrscr();
cout<<"Enter roll no";
cin>>rec.roll_no;
cout<<"Enter marks";
cin>>rec.marks;
out_file.write((char *)&rec,sizeof(rec));
cout<<"More records? ";
cin>>op;
}while(op=='y');
out_file.close();
///////////////////Reading all records from file///////////////////
ifstream in_file("Example.dat");
clrscr();
long int num=1;
if(!in_file)
{
cout<<"Cannot open file";
exit(1);
}
while(in_file.read((char *)&rec,sizeof(rec)))
{
cout<<"\nRecord Number:"<<num;
cout<<"\nRoll No: "<<rec.roll_no;
cout<<"\nMarks: "<<rec.marks;
num++;
}
in_file.close();
getche();
//////////////Finding a record from the file /////////////////
clrscr();
ifstream find("Example.dat");
if(!find)
{
cout<<"Cannot open file";
exit(1);
}
cout<<"\nEnter the roll number you want to find";
cin>>temp;
num=1;
int found=0;
while(find.read((char *)&rec,sizeof(rec)))
{
if(rec.roll_no==temp)
{
find.seekg((num)*sizeof(rec));
cout<<"YOUR RECORD IS";
cout<<"\nRoll No: "<<rec.roll_no;
cout<<"\nMarks: "<<rec.marks;
found=1;
}
num++;
}
if(found==0)
cout<<"The record could not be found";
getche();
find.close();
//////////////Updating a record in the file /////////////////
clrscr();
fstream update("Example.dat",ios::in|ios::out);
if(!update)
{
cout<<"Cannot open file";
exit(1);
}
cout<<"\nEnter the roll number whose marks you want to update";
cin>>temp;
num=0;
found=0;
long int pos;
while(update.read((char *)&rec,sizeof(rec)))
{
if(rec.roll_no==temp)
{
cout<<"YOUR RECORD IS";
cout<<"\nRoll No: "<<rec.roll_no;
cout<<"\nMarks: "<<rec.marks;
cout<<"\nEnter new marks";
cin>>rec.marks;
pos=update.tellp();
update.seekp(pos-sizeof(rec));
update.write((char*)&rec,sizeof(rec));
update.close();
found=1;
}
num++;
}
if(found==0)
cout<<"The record could not be found";
getche();
}
0 comments:
Post a Comment