The document provides an overview of basic input-output in C++, essential for interactive programming. It covers standard input (cin) and output (cout) streams, along with common operations like reading from the keyboard and displaying text on the screen. Detailed explanations of formatted and unfo...
C++ Basic Input/Output
In this topic, we will learn to use the cin object to take input from the user, and
the cout object to display output to the user with the help of examples.
C++ Output
In C++, cout sends formatted output to standard output devices, such as the
screen. We use the cout object along with the << operator for displaying output.
Example 1: String Output
#include <iostream>
using namespace std;
int main() {
// prints the string enclosed in double quotes
cout << "This is C++ Programming";
return 0;
}
Output
This is C++ Programming
How does this program work?
• We first include the iostream header file that allows us to display output.
• The cout object is defined inside the std namespace. To use
the std namespace, we used the using namespace std; statement.
• Every C++ program starts with the main() function. The code execution
begins from the start of the main() function.
, ELITE TUTORING
• cout is an object that prints the string inside quotation marks " ". It is
followed by the << operator.
• return 0; is the "exit status" of the main() function. The program ends with
this statement, however, this statement is not mandatory.
Note: If we don't include the using namespace std; statement, we need to
use std::cout instead of cout .
This is the preferred method as using the std namespace can create potential
problems.
However, we have used the std namespace in order to make the codes more
readable.
#include <iostream>
int main() {
// prints the string enclosed in double quotes
std::cout << "This is C++ Programming";
return 0;
}
Example 2: Numbers and Characters Output
To print the numbers and character variables, we use the same cout object but
without using quotation marks.
#include <iostream>
using namespace std;
int main() {
int num1 = 70;
double num2 = 256.783;
char ch = 'A';
cout << num1 << endl; // print integer
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller peterkuria3. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $5.49. You're not tied to anything after your purchase.