Making a C++ Diamond with a given width

I have written a C++ program which should make a diamond with an odd width inputted by the user. However, the problem is that it only makes the diamond increasing, not decreasing... Can anyone help?
Here is the code so far:

#include <iostream>
#include <cstdlib>

void intro ();
//Precondition: User has opened the program
//Postcondition: User knows what the program does

void input (int& width);
//Precondition: User is ready to input values
//Postcondition: User has correctly inputted the value of the width of the diamond as width

bool isNegative (int width);
//Precondition: width has been assigned a value
//Postcondition: will return true if width is nagetive, will return false if width is postive

void error_check (int width);
//Precondition: width has been assigned a value
//Postcondition: program will repeat if width is negative or/and even. program will continue to the next function
// if width is valid

void calculate_output (int width);
//Precondition: width has been assigned a value, width has passed the error_check
//Postcondition: Diamond outputted to the screen

void repeat (char response);
//Precondition: user is ready to say if he/she wants to repeat program
//Postcondition: user has inputted whether or not they want to repeat program

int main()
{
int width;
char response;

//Calls all of the functions
intro ();
input (width);
error_check (width);
calculate_output (width);
repeat (response);

system ("pause");
return 0;
}

//Tells user what program will do
void intro ()
{
using namespace std;

cout << "This program will output a diamond if you input an odd integer width\n" << endl;
}

//Get the input from the user
void input (int& width)
{
using namespace std;

cout << "Please input the odd integer width of the diamond: ";
cin >> width;
cout << "The width that you inputted is " << width;
cout << endl;
cout << endl;
}

//Test for whether width is negative or not
bool isNegative (int width)
{
return (width < 0);
}

//Makes sure the width is valid
void error_check (int width)
{
using namespace std;
double number;
char response;

if (isNegative (width))
{
cout << "You cannot have a negative integer width...\n" << endl;
}


number = width%2;

if (number == 0)
{
cout << "Your width has to be odd\n" << endl;
}

//Repeats program if width is not valid
if ((number == 0) || (isNegative (width)))
{
repeat (response);
}

}

void calculate_output (int width)
{
using namespace std;

for(int i = 0; i < width; i++)
{
int aster;
int row;

if(i < width)
row = i;
else
row = width - i - 1;

aster = 1 + (2*row);

int space = (width-aster)/2;

for(int a = 0; a < width; a++)
{
if(a < space)
cout << " ";

else if (a >= space && aster > 0)
{
cout << "*";
aster--;
}

else
cout << " ";
}
cout << endl;
}
}

//Repeats program if user wants to
void repeat (char response)
{
using namespace std;

cout << endl;
cout << "Would you like to repeat this program?\n";
cin >> response;

if ((response == 'Y') || (response == 'y'))
{
system ("cls");
main ();
}

else
{
cout << "Thank you for using this program!\n";
}

system ("pause");
}
try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void calculate_output (int width) { 
  using namespace std; 
  int space;
  int row;
  for(int i=1; i<2*width; i+=2){
    if(i<=width) row = i;
    if(i>width) row = 2*width-i;
    space= (width-row)/2;
    for(int j=0; j<space; j++){
      cout << " ";
    }
    for(int j=0; j<row; j++){
      cout << "*";
    }
    cout << endl;
  }
}


Topic archived. No new replies allowed.