Posts

Showing posts from November, 2016

C++:Working With Files -1

Image
A program typically involves either or both of the following kind of data communication:-1. Data transfer between console unit and program2. Data transfer between program and disk file.Input Stream- supplies data to program or reads data from file.Output Stream- recieves data from program or writes data to file.Input operation involves the creation of an input stream and linking it with the program and input file.
________________________________________
Classes for FIle Stream OperationsI/O system of c++ contains a set of classes that defines the file handling methods.These include ifstream, ofstreamand fstream. These classes are derived from fstreambase class and corresponding iostreamclass.
_________________________________________
Opening and Closing of FileThe filename is a string of character conatining two parts - primary name and optional period with extension.
eg.  Input.data
        test.docOpening file using constructorThis method is useful when we use only one file in the st…

C++: Program for calculator using functions

Image
#include<iostream>
using namespace std; float sum(float, float);
float sub(float, float);
float mul(float, float);
float div(float, float); int main()
{
float x, y, res;
char choice;
cout << "Calculator\n\n";
cout << "Enter two numbers\n";
cin >> x >> y;
cout <<
"Operations \n '+' for addition \n '-' for substraction \n '*' for multiplication \n '/' for division \n";
cin >> choice;
switch (choice)
{
case '+':
res = sum(x, y);
cout << "Sum is :" << res;
break;
case '-':
res = sub(x, y);
cout << "Substraction is:" << res;
break;
case '*':
res = mul(x, y);
cout << "Multiplication is:" << res;
break;
case '/':
res = div(x, y);
cout << "Division is :" << res;
break;
default:
cout << "Enter correct choice";
break;
}
return 0;
} float sum(float a, float b)
{

C++: I/O Operations -2

Image
Formatted Console I/O OperationsTo access manipulators the file iomanipshould be included in program.cout.width(w);to define width of a field necessary for output of item.
w is field width(no. of columns)cout.precision(d)to specify no. of digits to be displayed after the decimal point.By default, floating no. are printed with six digits after the decimal point.  cout.precision(3);
  cout<<3.15159<<"\n";
  cout<<2.50032<<"\n";3.142(rounded to nearest cent)
2.5 (no trailing zeros)Unlike width(), precision() retains the setting in effect until it is reset.cout.fill(ch);  to fill unused position by any desired character.By default, the unused positions of the field are filled with the white spaces.  cout.fill("*");
  cout.width(10);
  cout<<5250<<"\n";******5250fill() also stays in effect till we change it. cout.setf(arg1,arg2); 
arg1 is one of the formatting flags specifies the format action required for outp…

Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily

What is "namespace" and why do we use it?