Spaces between variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Spaces between variables

How do I create spaces when I want to show 2(or more ) variables? cout<<variable1<<variable2; variable1variable2 x variable1 variable2 ✓

3rd Apr 2024, 9:59 AM
Giannis Kallergis
Giannis Kallergis - avatar
2 Answers
+ 1
To create spaces between variables when using cout, you can simply include the spaces within the quotes of the text you want to display. Here are a couple of ways to achieve this: Method 1: Using Spaces Inside Quotes cout << variable1 << " " << variable2; This will output variable1 followed by a space, then variable2. Method 2: Using Manipulators You can also use setw from the <iomanip> library for more control over the width of the space: #include <iostream> #include <iomanip> // Your code... cout << variable1 << setw(5) << variable2; In this example, setw(5) sets the width of the space between variable1 and variable2 to be 5 characters. You can adjust this number as needed. Method 3: Manually Specifying Space You can also simply add a space in the cout statement: cout << variable1 << " " << variable2; This will add a single space between variable1 and variable2.
3rd Apr 2024, 4:40 PM
Alberto Iannucci
+ 3
cout << x << " " << y; a string with a blank space inside.
3rd Apr 2024, 10:24 AM
Tibor Santa
Tibor Santa - avatar