0% found this document useful (0 votes)
33 views

Lecture - Console in Console Out AY1819

The document discusses input and output in C++ programming. It covers using cout to output text and values to the console, cin to accept input, and formatting output for numbers. It also introduces the string data type and using cin and getline() for string input/output. The main parts of a C++ program are also briefly outlined.

Uploaded by

Hydie Cruz
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Lecture - Console in Console Out AY1819

The document discusses input and output in C++ programming. It covers using cout to output text and values to the console, cin to accept input, and formatting output for numbers. It also introduces the string data type and using cin and getline() for string input/output. The main parts of a C++ program are also briefly outlined.

Uploaded by

Hydie Cruz
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

CONSOLE

INPUT / OUTPUT

Programming
Objectives
 Recall Parts of C++ Program
 Use Output statement using cout <<
 Use Output statement using cerr

 Use Input statement using cin >>


PARTS OF C++ PROGRAM
<Comment/s>
<Header files>
<Constant definition>
<Function Definition>
<Main program >
<variable declaration>
<statements>
PARTS OF C++ PROGRAM
 1. Comment – as introductory comment
which written the programmer’s name,
date program is written and purpose of
the program
 2. Header files – list and identifies the
different Libraries used within the
program
 If any of the header is present the code
below is required using namespace std;
PARTS OF C++ PROGRAM
 3. Constant definition can be declared
after the header file with sample
syntax below:
 #define PI 3.14.16

 #define TAX 0.12

4. Function Definitions – use to declare


other subprogram connected within
the main.
PARTS OF C++ PROGRAM
5. main program – use to declare the
beginning of the program.
Using the word int main() followed by
open curly bracket ({ ) declare the
beginning and ended with close curly
bracket ( } ) to specify the end scope
of the main program.
Display output using cout
Any character or word/s can be
displayed on screen using the cout
object code.
Followed by the word cout(means
console out) with the two less than
sign (<<) no space in between.
The string must included with the double
quote ( “ )
Display output using cout

cout<<”Hello World!!. \n”;


cout<<”Welcome to Programming”;
Output:
Hello World!!
Welcome to Programming
Mathematical Operator
Operations Uses
+ - Addition
- - Subtraction
* - Multiplication
/ - Division
% - Modulus (remainder)
Display output using cout
with Mathematical Operator
int no1 = 10, no2 = 30;
cout<<”Sum of two numbers no1 + no2 = “
<<(no1 + no2);
Output:

Sum of two numbers no1 + no2 = 40


//created by filename: programCoutSurname.cpp
#include <iostream>
using namespace std;

int main()
{
//declare variable
int no1 = 10, no2 = 30;

cout<<“ no1 = “ <<no1<<‘\n’;


cout<<“ no2 = “ <<no2 <<‘\n’;
cout<<”Sum of two numbers no1 + no2 = “ <<(no1 + no2);
return 0;
}
cout<<“ no1 = “ <<no1<<‘\n’;
cout<<“ no2 = “ <<no2 <<‘\n’;
cout<<”Sum of two numbers no1 + no2 = “ <<(no1 + no2)<<endl;
cout<<”Product of two numbers no1 + no2 = “ <<(no1 * no2) <<endl;
cout<<”Difference of two numbers no1 + no2 = “ <<(no1 - no2) <<endl;
cout<<”Quotient of two numbers no1 + no2 = “ <<(no1 / no2) <<endl;

return 0;
}
//created by filename: programCinSurname.cpp
#include <iostream>
using namespace std;

int main()
{
//declare variable
int no1 = 0, no2 = 0;
//input using cin
cout<<“Enter no1  “;
cin>>no1;
cout<<‘\n’;

cout<<“Enter no2  “;
cin>>no2;
cout<<‘\n’;
Statement using cin
cin is used to accept any input coming
from the keyboard.
The input can be any character, words
or numeric value that can be either
used for computations or displayed
purposes .

cin>>no1
Statement using cin
 cout<<” Enter number of students \n “
<<” followed by the number of male \n“
 cin>>totStudent >> noMale;
#include <iostream>
using namespace std;

int main(){

//variable declaration
float noStud=0, noGirl=0, noBoy=0;
//Input
cout<< "\n Cruise - Cruz Technical School
Enrollment System ";
cout << "\n Enter number of enrolled Girls -> ";
cin >> noGirl;
cout << " \n Enter number of enrolled Boys -> ";
cin>> noBoy;

//Process
noStud = noGirl + noBoy;

//Output
cout <<" \n Total School Population = "
<<noStud;
return 0;
}
Output:
FORMATTING FOR NUMBERS WITH A
DECIMAL POINT

ata type double, when it comes to output


the numeric value maybe what we never
expected.
cout<< “ The net Sales is equal to P “
<< netSales <<endl;
variable netSales has the value of P 873.5,
the output can be
The net Sales is equal to P 873.500000
FORMATTING FOR NUMBERS WITH A
DECIMAL POINT
To ensure that the screen produce output what we want , for
example displayed two (2) decimal places, use the following
lines of code below.

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

cout<< “ The net Sales is equal to P “<< netSales <<endl;


Output  The net Sales is equal to P 873.50
4.7 CLASS STRING IN INPUT /
OUTPUT STATEMENT

Class string is defined within the library called


<string> and the definition are placed in the
std namespace

#include <iostream>
#include <string>
using namespace std;
4.7 CLASS STRING IN INPUT /
OUTPUT STATEMENT

String is a non-numeric variables that can


store values longer than one single character
enclosed with the double quote (“ ).
 Declare string variables

string message1 = “Hello World”;


or can also be declared as
string message2 (“Hola Amiga”);
//Filename stringSampleCruz.cpp
#include <iostream>
#include <string>
using namespace std;

int main()
{
string sentence;
string word1= " Welcome to Logic Formulation ";
string word2 = " An Introduction to Programming \n ";
string word3 = " A very important subject in IT Course";
string word4 = "\n I hope I passed this subject. ";
sentence = word1 + word2 + word3 + word4;

cout <<"\n An example using string "<<endl;


cout <<sentence;

return 0;
}
Output:
cin and strings
 cin ( >> extraction operator) to get
string input like example above after
encountering a space the reading of
the string will stop
 getline() function is the
recommended syntax to get more
than one word with spaces or whole
sentence.
//getLineStringSample.cpp
#include <iostream>
#include <string>
using namespace std;

int main()
{

//initialization
string stringSam1, stringSam2;
cout<<"\n What is your name ? ";
getline(cin, stringSam1);
cout <<stringSam1
<<" Welcome to C++ Programming. "
<<"\n";

return 0;
}
Output:

You might also like