C Programming

These notes are in addition to the text book used in the class of BAA2013 Computer Programming in C++.

Wednesday, May 10, 2006

Lesson 5 input and output


Example of using special characters


// specchar.cpp
// example of new line and special characters
#include
int main()
{
j=25;
cout<< j<< ‘\n’; //single quotes because it is a single character
cout<< “String\n”; //double quotes because it is part of a string
cout << “The numbers on the following line are separated by tabs.\n”;
cout << “1 \t 2 \t 3 \t 4 \n”;
// the following lines use endl
cout << “in C++, you output backslashes (\\)” << endl;
cout << “you can also print single quotes (\’) and “ << endl;
cout << “double quotes (\”).” << endl;
return 0;
}

Example of formatted output

// coutsetf.cpp
#include
int main()
{
float x = 24.0;
cout << x << endl; //displays 24
cout.setf(ios::showpoint);
cout << x << endl; // displays 24.0000
cout.setf(ios::showpos);
cout << x << endl; // displays +24.00
cout.setf(ios::scientific);
cout << x << endl; // displays +2.400000e+001
return 0;
}

0 Comments:

Post a Comment

<< Home