Create a shape in C++
Asked Answered
R

4

5

I want to create a shape like this:

ccccccc
cccccc
ccccc
cccc
ccc
cc
c

My code is:

#include <iostream>

using namespace std;

int main(){
    int i, j;
    for(i = 0; i < 7; i++){
        for(j = 7; j > 7; j--){
            cout << 'c';
        }
        cout << endl;
    }
    return 0;
}

But in terminal the output I get is some blank lines.

What am I doing wrong?

enter image description here

Raquel answered 24/4, 2015 at 14:17 Comment(2)
Where are these alleged blank lines? Can you provide us with your current output?Hardenberg
Here is a screenshotRaquel
C
18

for(j = 7; j > 7; j--){ This expression is always false.

You need to write for(j = 7; j > i; j--){

Crashland answered 24/4, 2015 at 14:18 Comment(2)
It's a pattern very, very popular in all C++ software :D I hope someone make a library of it, say std::ChristmasTree<>, templated is better :DIphagenia
@Mouze: In all my coding years, the only time I see this pattern is by assignments in introductory C and C++ programming course. It is seldom used again throughout the C and C++ curriculum.Ephraim
E
4

You want this:

#include <iostream>

using namespace std;

int main(){
    int i, j;
    for(i = 7; i > 0; --i){
        for(j = i; j > 0 ; j--){
            cout << 'c';
        }
        cout << endl;
    }
    return 0;
}

live example

Your original code had a logic error in the inner loop

for(j = 7; j > 7; j--){

here j is 7 but j will never be greater than 7 so it never executes, but even if this was fixed to

for(j = 7; j > 0; j--){

This will just cout 7 'c' 7 times, so what I modified was to change your inner loops starting value so that it then decrements correctly.

for(i = 7; i > 0; --i){
            for(j = i; j > 0 ; j--){
                    ^ now initialised by outer loop

So what would happen is that the inner loop never executed but you executed cout << endl; 7 times hence the blank lines

Embark answered 24/4, 2015 at 14:21 Comment(0)
B
0

The condition of the loop

for(j = 7; j > 7; j--){

is wrong. That is it always is equal to false because initially i is set to 7 and it can not be greater than 7.:)

I think you mean something like

for(j = 7 - i; j > 0; j--){

The program can be written simpler.

#include <iostream>
#include <iomanip>

int main() 
{
    while ( true )
    {
        std::cout << "Enter a non-negative number (0-exit): ";

        size_t n = 0;
        std::cin >> n;

        if ( !n ) break;

        const char c = 'c';

        std::cout << std::setfill( c );

        while ( n ) std::cout << std::setw( n-- ) << c << std::endl;
    }

    return 0;
}

The program output is

Enter a non-negative number (0-exit): 7
ccccccc
cccccc
ccccc
cccc
ccc
cc
c
Enter a non-negative number (0-exit): 0
Buhrstone answered 24/4, 2015 at 14:30 Comment(0)
F
-2
#include <iostream>
using namespace std;

int main()
{
    int i, j;
    for(i = 7; i > 0; --i)
    {
        for(j = i; j > 0 ; j--)
        {
            cout << 'c';
        }
        cout << endl;
    }
    return 0;
}

the way you are giving 7 in 2nd loop is creating problems in output this is the right way to declare.

Frontiersman answered 12/1 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.