-1

I am supposed to write a program that prompts the use for number of lines, then outputs ASCII art in a "V" shape. For input 4, output is ("-" represents spaces):

*-----*
-*---*  
--*-*  
---*     

My code is:

// prompt for number of stars
int stars;
std::cin >> stars;

// indent
int indent = 0;
int space = 1;

// find space 
if (stars = 1)
{
    space = 0;
}
else if (stars == 2)
{
    space = 1;
}
else if (stars >= 3)
{
    int addspace = stars - 2;
    space = space + (2 * addspace);
}
// print spaces to double check calculation
std::cout << "space: " << space << '\n';

// print first star
if (stars == 1)
{
    std::cout << "*";
}
// print lines
for (int lines = 1; lines == stars; ++lines)
{
    // print indent
    std::cout << "indent: " << indent << '\n'
              << "spaces: " << space << '\n';
    if (lines > 1)
    {
        for (int ind_loop = 1; ind_loop < indent; ++ind_loop)
        {
            std::cout << " ";
        }
    }
    std::cout << "*";
    indent += 1;

    // print spaces
    std::cout << "spaces: " << space << '\n';
    for (int sp_loop = 0; sp_loop < space; ++sp_loop)
    {
        std::cout << " ";
    }
    space -= 2;
    std::cout << "*";

    // next line
    std::cout << '\n';
}
std::cout << '\n';

Every time it gives me just:

*   

and int spaces always comes out to equal 0.

Does anyone know why this might be, and what I need to do to correct it?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Smoak
  • 25
  • 6

2 Answers2

3

There are some issues with your code:

  1. if (stars = 1) is always true, and it changes the value of stars to 1. You need to change it to if (stars == 1) instead.

  2. for(int lines = 1; lines == stars; ++lines) is wrong, as it will not execute unless stars is exactly 1. You need to change it to for(int lines = 1; lines <= stars; ++lines) instead.

  3. for(int ind_loop = 1; ind_loop < indent; ++ind_loop) needs to be changed to for(int ind_loop = 0; ind_loop < indent; ++ind_loop) instead, to act correctly on the second line.

  4. Remove all couts other than * and space from your for loop, they will break your V form.

  5. for the last std::cout << "*"; in your loop, you need to add a condition to examine if it's the last line or not (last line should print * one time).

So, the final code would be something like this:

int main()
{
    int stars;
    std::cin >> stars;

    // indent
    int indent = 0;
    int space = 1;

    // find space 
    if(stars == 1)
    {
        space = 0;
    }
    else if(stars == 2)
    {
        space = 1;
    }
    else if(stars >= 3)
    {
        int addspace = stars - 2;
        space = space + (2 * addspace);
    }
    // print spaces to double check calculation
    std::cout << "space: " << space << '\n';

    // print first star
    if(stars == 1)
    {
        std::cout << "*";
    }
    // print lines
    for(int lines = 1; lines <= stars; ++lines)
    {
        // print indent
        //std::cout << "indent: " << indent << '\n'
        //  << "spaces: " << space << '\n';
        if(lines > 1)
        {
            for(int ind_loop = 0; ind_loop < indent; ++ind_loop)
            {
                std::cout << " ";
            }
        }
        std::cout << "*";
        indent += 1;

        // print spaces
        //std::cout << "spaces: " << space << '\n';
        for(int sp_loop = 0; sp_loop < space; ++sp_loop)
        {
            std::cout << " ";
        }
        space -= 2;
        if(lines != stars)
            std::cout << "*";

        // next line
        std::cout << '\n';
    }
    system("pause");
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
HMD
  • 2,202
  • 6
  • 24
  • 37
0

You can do the same in a much compact manner, try to understand the following code which does the same thing than you without the need of the if, hope it could help your first steps in programing

#include <iostream>
using namespace std;
int main ()
{
  int lines,pad,cnt,i,j;
  while(true)
  {
    cin >> lines;
    for(i=lines,pad=0,cnt=(lines-2)*2+1;i>1;i--,pad++,cnt-=2)
    {
      for(j=0;j<pad;j++)cout<<" ";
      cout<<"*";
      for(j=0;j<cnt;j++)cout<<" ";
      cout<<"*"<<endl;
    }
    for(j=0;j<pad;j++)cout<<" ";
    cout<<"*"<<endl;
  }
}

You could get rid of the variable i, how?

David Daverio
  • 323
  • 1
  • 11