endl

Jan 28, 2015 at 4:45pm
So I'm trying to complete this for an assignment. I need to print a pyramid of stars using 2 ways: the first with 4 cout statements; and the second with only 1 cout statement. However, on my second version, I can't manage to get the stars on different lines. Where do I put the endl so that it comes up with the same result as version 1?


This is my first program ever in C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  //-----------------------------------------------------
//Assignment 1
//Question 1
//File name: Assignment1Q1.cpp
//Written by: Shawn Rousseau (ID: 7518455)
//For COMP 218 Section EC/Winter 2015
//Concordia University, Montreal, QC
//-----------------------------------------------------

//This program's purpose is to print a pre-given message.

#include <iostream>
#include <string>

using namespace std;

const string line1 = "   *";
const string line2 = "  ***";
const string line3 = " *****"; 
const string line4 = "*******";


int main()
{

	cout << "This is version 1 with four cout statements." << endl;
	cout << line1 << endl;
	cout << line2 << endl;
	cout << line3 << endl;
	cout << line4 << endl;

	cout << "This is version 2 with only one cout statement." << endl;
	cout << line1 << endl << line2 << endl << line3 << endl << line4<< endl;

	return 0;
}
Jan 28, 2015 at 4:58pm
Jan 28, 2015 at 5:03pm
thank you!!
But I would love to understand what is different from mine.
I noticed the editor places red marks after each line# in my version, but not in yours. The red marks are placed even before compiling.

I fail to see what is different...
Jan 28, 2015 at 6:07pm
did you copy it from a book? i just deleted your red dots.
Jan 29, 2015 at 4:27am
yes, I'm so stupid I copy/pasted this from the textbook to "save time"...

Thanks buddy!
Jan 29, 2015 at 6:02pm
You can use the new line character '\n' instead of endl. \n doesn't flush the output buffer like endl does so you can use that and then have an endl at the end of the statement.
Topic archived. No new replies allowed.