Numbers and Texts C++
Numbers and Texts 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 .
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!
We will learn more about this print statement in detail later. For now, let's
use this print statement to print numbers and strings.
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() {
return 0;
}
Example
Expected Output
#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
1#include <iostream>
2using namespace std;
3
4int main() {
5
6 cout << 67 << 59;
7 return 0;
8}
Run Code >>
Output
6759
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.
Example
Expected Output
int main() {
cout << 5.55 << ", " << 6.66 << ", " << 7.77;
return 0;
}
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
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
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.
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.
1#include <iostream>
2using namespace std;
3
4int main() {
5
6 cout << "This is a string."
7
8 return 0;
9}
Run Code >>
Output
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.