Showing posts from November, 2016

C++:Working With Files -1

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.
        test.docOpening file using constructorThis method is useful when we use only one file in the st…

C++: Program for calculator using functions

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;
case '-':
res = sub(x, y);
cout << "Substraction is:" << res;
case '*':
res = mul(x, y);
cout << "Multiplication is:" << res;
case '/':
res = div(x, y);
cout << "Division is :" << res;
cout << "Enter correct choice";
return 0;
} float sum(float a, float b)

C++: I/O Operations -2

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<<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<<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

C++: Breadth First Search program using Adjacency Matrix

Check if the String is Palindrome (C++ & Java Program)

C++: Depth First Search program using Adjacency Matrix (Graph Algorithm)