Lecture - Console in Console Out AY1819
Lecture - Console in Console Out AY1819
INPUT / OUTPUT
Programming
Objectives
Recall Parts of C++ Program
Use Output statement using cout <<
Use Output statement using cerr
int main()
{
//declare variable
int no1 = 10, no2 = 30;
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
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
#include <iostream>
#include <string>
using namespace std;
4.7 CLASS STRING IN INPUT /
OUTPUT STATEMENT
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;
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: