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

ACLC College Computer Programming1 C SESSION8

Uploaded by

diazvanharoldm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

ACLC College Computer Programming1 C SESSION8

Uploaded by

diazvanharoldm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to Programming

(C++)

Gliceria S. Tan
Instructor
Session 8 Topics

➢ Using cout
➢ Using cin

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Using cout

To print to the screen we use the iostream


object cout (which is short for “console
output”). To send data to standard output,
you use the operator <<. With the iostream
object cout, the operator << means “send
to.”

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Example:
cout<<“First output line.\n”;
cout<<“Second output line.”;

Output: First output line.


Second output line.
The \n at the end of the first cout is a
special character called a newline
character. It causes the next output to
appear on the next line.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Alternatively, you may also use endl. It works
the same as \n. That is, it also causes
the next output to appear on the next line.
Example:
cout<<“First output line.”<<endl;
cout<<“Second output line.”;

Output:
First output line.
Second output line.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Below are more examples of the cout statement:
int x = 10, y = 3;
float cost = 10.7;
char ch = ‘A’;
cout<<“Please pay $”<<cost<<“.”;
cout<<“\nThe number is “<<x<<“.”;
cout<<“\nThe sum of “<<x<<“ and “<<y<<“ is “<< x + y;
cout<<“\nYou entered the character “<<ch;

The output of the program is:

Please pay $10.700000.


The number is 10.
The sum of 10 and 3 is 13
You entered the character A

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Using cin

The iostream object used for input is cin


(short for “console input”).
The iostream operator used with cin is >>.
Example:
cin>>num1;
cin>>price;
cin>>x>>y; //asks for two values
cin>>a>>b>>c; //asks for three values

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
To illustrate the use of both cout and cin, let’s take as an
example the flowchart we had in the beginning that
displays the sum of two input numbers.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
is equivalent to
cin>>x>>y;

When reading input, it’s always a


good idea to show a message
called a “prompt” before the input
statement to indicate to the user of
the program that some input is
expected. Here’s a good prompt to
pair with the cin statement above:
cout<<“Enter two numbers: “;

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
is equivalent to
sum = x + y;

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
is equivalent to
cout<<sum;

A more user-friendly way is to add a


message to make the output
informative. For example:
cout<<“The sum is “<<sum;
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Here’s the entire program.

//Accepts two numbers and displays their sum


#include <iostream>
using namespace std;
int main() {
int x, y, sum; //declaration of all vars in the program
cout<<”Enter two numbers “;
cin>>x>>y;
sum=x+y;
cout<<”The sum is “ <<sum;
}

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Below is a sample dialogue that might
appear on the computer screen when the
program is run and the values 3 and 6 are
entered.

Enter two numbers: 3 6


The sum is 9

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Here’s a program that accepts a person’s age and
displays a message.

//Accepts a person’s age and displays a message


#include <iostream>
using namespace std;
int main() {
int age;
cout<<”Enter your age: “;
cin>>age;
cout<<”You are “ <<age <<” years old.”;
}

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Below is a sample screen dialogue when
the program is run and the age of 18 is
entered.

Enter your age: 18


You are 18 years old.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
“QUOTE OF THE DAY!”

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR

You might also like