[go: up one dir, main page]

0% found this document useful (0 votes)
33 views7 pages

Numbers and Texts C++

The document explains the use of literals in C++, including integers, floating-point numbers, and strings. It details how to print these values using the cout statement, including examples of printing single and multiple numbers and strings. Additionally, it addresses common mistakes such as forgetting quotation marks and semicolons in print statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views7 pages

Numbers and Texts C++

The document explains the use of literals in C++, including integers, floating-point numbers, and strings. It details how to print these values using the cout statement, including examples of printing single and multiple numbers and strings. Additionally, it addresses common mistakes such as forgetting quotation marks and semicolons in print statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Numbers and Texts

In programming, we can directly use some fixed values in our program.


These fixed values are called literals.

Let's take a look at some commonly used literals in C++.

1. Integers (int)

Integers are numbers without decimal parts. For example: 5, -11, 0, 12,
etc. For now, let's refer to integers as simply int .

2. Floating-point Numbers (double)

Floating-point numbers contain decimal parts. For


example: 2.5, 6.76, 0.0, -9.45, etc. For now, let's refer to floating-point
numbers as double .

3. Strings (string)

In C++, texts wrapped inside double quotation marks are called strings .

"This is a string."
Print Numbers and Strings
Now that we have a basic idea of how C++ classifies numbers and texts,
it's time to learn how to print these things!

You might remember this from our Hello World! program.


cout << "Hello World!";
In C++, we use the cout << output; statement to print output on the screen.

We will learn more about this print statement in detail later. For now, let's
use this print statement to print numbers and strings.

Let's start with printing numbers.

Print Strings
We've already printed a string before in our "Hello World!" program.
This is another program that prints a string.

1#include <iostream>
2using namespace std;
3
4int main() {
5
6 cout << "This is a string.";
7
8 return 0;
9}
Run Code >>

Output

This is a string.

Note: The outer quotation marks are not displayed when we print
strings.

Print String
Create a program to print the sentence C++ is interesting. on the screen.

Example

Expected Output

C++ is interesting.

#include <iostream>
using namespace std;

int main() {

cout << "C++ is interesting.";

return 0;
}

Print Multiple Strings


Create a program to print "Eminem" , " and " , "Dr. Dre" on the screen in a
single statement.

Example

Expected Output

Eminem and Dr. Dre

#include <iostream>
using namespace std;

int main() {

cout << "Eminem" << " and " << "Dr. Dre";

return 0;
}

Print Numbers
Here's how we can print numbers.

1#include <iostream>
2using namespace std;
3
4int main() {
5
6 cout << 67;
7 return 0;
8}
Run Code >>

Output

67

Here, cout prints the content after << , which is 67.

Remember that we haven't included quotes around 67. This is because


using quotes makes it a string instead of a number (i.e., "67" is a string).

Print Multiple Numbers


Just like strings, we can print multiple numbers by separating them with
the << operator.

1#include <iostream>
2using namespace std;
3
4int main() {
5
6 cout << 67 << 59;
7 return 0;
8}
Run Code >>

Output

6759

Here, we have printed two numbers 67 and 59.

In the output, we can see that there is no space between 67 and 59. Let's
change that.

1#include <iostream>
2using namespace std;
3
4int main() {
5
6 cout << 67 << " " << 59;
7 return 0;
8}
Run Code >>

Output

67 59

As you can see, we have printed the numbers (67 and 59) and a string ( "
" ) together inside a single print statement.

Again, the outer quotation marks are not printed when we print strings.

Print Multiple Numbers


Create a program to print 3 numbers in a single statement.

Print the numbers 5.55, 6.66, and 7.77 using a


single cout statement.
 Separate each number using a comma followed by an empty space.
Hint: Use the string ", " to print a comma followed by an empty space.

Example
Expected Output

5.55, 6.66, 7.77


#include <iostream>
using namespace std;

int main() {

cout << 5.55 << ", " << 6.66 << ", " << 7.77;
return 0;
}

Multiple Print Statements


We can also use multiple print statements in a program.

1#include <iostream>
2using namespace std;
3
4int main() {
5
6 cout << "Hey";
7 cout << "How are you?";
8
9 return 0;
10}
Run Code >>

Output

HeyHow are you?

Here, we have used two print statements to print texts: Hey and How are
you? . However, notice that these two texts are printed together in the
same line.

This is not our desired output because we want to print them in different
lines. To do so, we can use endl with the cout statement. For example,

1#include <iostream>
2using namespace std;
3
4int main() {
5
6 cout << "Hey" << endl;
7 cout << "How are you?";
8
9 return 0;
10}
Run Code >>

Output

Hey

How are you?

Notice that we have used endl with the first print statement.
Here, endl adds a new line after printing the value, so the text How are
you? is printed in the new line.

It works just like pressing the Enter key.

Common Mistakes While Printing


1. Error Because of Quotation Marks

If you do not wrap text inside quotation marks, it's not a string. For
example,

1#include <iostream>
2using namespace std;
3
4int main() {
5
6 cout << This is a string.;
7
8 return 0;
9}
Run Code >>

If you run the program, you will get an error. It's because we haven't
wrapped the text This is a string. inside quotation marks.

2. Forgetting Semicolon After cout Statement

If we forget the semicolon, our code will not run.

1#include <iostream>
2using namespace std;
3
4int main() {
5
6 cout << "This is a string."
7
8 return 0;
9}
Run Code >>

Output

ERROR! 6 | cout << "This is a string."


| ^
| ;
7|
8| return 0;
| ~~~~~~

Here, the error message states that we must insert a semicolon ; before
the return 0; statement.

In other words, we must put a semicolon ; at the end of the cout << "This is a
string." statement in line 6 of our program.

You might also like