OK, so I have completed a homework assignemnt, and the code is correct; however, it is not running as smooth as I wish for it to. I am posting this purely out of curiosity. The program prints out a diamond (consisting of asterisks) where the size is based on a # received from the user. The problem is that after the diamond is printed it prints out a lot of extra lines. Here it is!
Code:
#include<iostream>
using namespace std;
int main(){
int width, //used to receive width from user
star = 0, //used to increment asterisks and is defaulted to 0 as the initial do-while loop must print only 1 star
space; //used to increment the spaces before asterisks
do{
cout << endl << endl << "Welcome to the Asterisk Diamond Generator" << endl << endl;
cout << "Please enter an odd number which will be the width of the diamond: "; //receive width variable
cin>>width;
if(width % 2 == 0) //prevent program from continuing with an even #
cout <<"\nWell, that's almost and odd number, but try again...\n\n\n";
}while(width % 2 == 0);
space = (width/2); //set the initial spacing for the star
cout<< "\n\n\n\n\n"; //clear room for a good drawing
do{ //draw the top half
for(int i = 0; i <= space; i++)
cout<< " ";
for(int j = 0; j <= star; j++)
cout<< "*";
cout<< endl << endl;
star += 2;
space -= 1;
}while(star <= width);
space = 1; //set the initial spacing and star count for the bottom half
star = (width - 3);
do{ //draw the bottom half
for(int i = 0; i <= space; i++)
cout<< " ";
for(int j = 0; j <= star; j++)
cout<< "*";
cout<< endl << endl;
star -= 2;
space += 1;
}while(space <= (width+1));
cout<< "Scroll up to see the diamond.\n\n";
cout<< "Thank you for using the Asterisk Diamond Generator." << endl << endl << endl;
return 0;
}
Any advice on this problem would be grrrrrreat!