-4

So I'm trying to do something like this:

std::cout << "*" << std::endl;
std::cout << "**" << std::endl;
std::cout << "***" << std::endl;
std::cout << "****" << std::endl;
std::cout << "*****" << std::endl;

Instead of doing it 5 times, can I just do a range function to cout rows that add up? So if want it to be 50 "*" signs. So basically starting at 1 * all the way to printing out "*"++ once it hits the 50th row. then I want to do it easier without doing cout so many times.

if you understand my horrible explanation please give your idea.

mediocrevegetable1
  • 4,086
  • 1
  • 11
  • 33
Judahmane
  • 3
  • 1
  • 6
    Have you learned about loops yet? – Nathan Pierson Aug 26 '21 at 04:02
  • also the empty quatation marks is the * sign – Judahmane Aug 26 '21 at 04:02
  • 2
    This should be of use: https://stackoverflow.com/q/388242/2079303 – eerorika Aug 26 '21 at 04:05
  • @NathanPierson but im trying to do it for each row so 1 = * 2 = ** 3 = *** all the way up to 50. but each number makes a new line instead of being on the same line. I have no idea how to do that. Ive learned loops yes but i dont know how to do the end line once it hits a number and gets to that number. – Judahmane Aug 26 '21 at 04:06
  • @Judahmane -- *but each number makes a new line instead of being on the same line.* -- I thought that printing on separate lines is what you wanted, looking at your attempt. – PaulMcKenzie Aug 26 '21 at 04:09
  • Looks like you are using `std::endl` a bit idiomatic here. Maybe that's the source of your confusion. There is no "hidden rule" that you mus write `std::endl` at the end of the line when printing something. In fact `std::endl` is just a "shortcut" for printing `'\n'` and flushing. If you don't need to flush the buffer, don't use `endl`. If you don't want a newline to be printed, don't use `std::endl` or `'\n'`. – Lukas-T Aug 26 '21 at 04:14
  • *"doing it 5 times"* -- which "it"? Maybe you should add some code to show what you have so far? Or use more words to describe your situation; you have gone beyond "concise" and are threatening to venture into "lost the meaning". – JaMiT Aug 26 '21 at 04:21
  • Consider showing the output you get in contrast to the output you want. That would among other things clarify the whitespace you want or do not want... – Yunnosch Aug 26 '21 at 04:49
  • 2
    Please try `std::cout << "*" << "**" << "***" << "****" << "*****" << "******" << "*******" << "********" << "*********" << std::endl;` and explain what you do not like about its result. – Yunnosch Aug 26 '21 at 04:50

4 Answers4

1

It is definitely possible to achieve this without repeating your code but without using loops. Everything that can be done with loops can be done via recursion. You will need to learn of it anyhow, so start early.

An example here can be:

void printLines(int n)
{
  if(n<=0) return;
  printLines(n-1);
  printStars(n);
}

void printStars(int n)
{
  if(n<=0)
  {
    std::cout << std::endl;
    return;
  }
  std::cout << '*';
  printStars(n-1);
}
v010dya
  • 5,296
  • 7
  • 28
  • 48
0

Without using loops or recursion this can't be done.

I had used for loop for your question.

#include<iostream>

using namespace std;


int main()
{

  for (int i = 1; i <= 50; i++)
  {
    for (int j = 1; j <=i ; j++)
    {
      cout<<"*";
    }
    cout<<endl;
  }

  return 0;
}

This will have 2 nested loops, one for printing the stars "*" and another for changing the line to print next stars to next rows.

Here 1st loop will run for 1 to 50 that you asked as no. of rows. And the nested loop will print stars from 1 to the counter of 1st loop. Hence, for 1st time it'll run and print 1star and first loop will change the line and then again 2nd time it'll print 2stars and 1st loop will change the line and this process will continue up to 50 rows.

Uttam
  • 718
  • 6
  • 19
0

Here's one way:

#include <cstdio>

int main() {
  auto& str = "*****";
  auto offset = sizeof(str);

  while (offset--) std::puts(str + offset);
}

The trick we use is to move the pointer by an offset to print a different number of stars.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
0

You can simply use loops. Here i have used maxCount variable to store number of lines you want to print. You can just change its value as per your need!

#include<iostream>

using namespace std;


int main(){
    int maxCount = 50;
    int tmaxCount = maxCount;
    while(tmaxCount--){
        for(int i=0; i<maxCount-tmaxCount; ++i){
            cout<<"*";
        }
        cout<<"\n";
    }
    return 0;
}
virteanchi
  • 169
  • 1
  • 14