Tuesday, July 28, 2009

C++ Program to Print a Hollow Box?

Ok, I need to write code to that will ask:





A. "Do you want to start(Y/N)?


B. "How many rows/columns(5-21)?


C. "Out of range. Renenter:"





I know how to program the If and the While statements to tell whether the pogram runs and if it will run if you enter enough row/scolumns... i just can't figure out how to right the code to print the box. So far I have:





int triangles;


int number;


int count = 0;





int main(){








cout %26lt;%26lt; "Drawing hollow boxes program.\n" %26lt;%26lt; endl;


cout %26lt;%26lt; "How many rows/columns(5-21)? ";


cin %26gt;%26gt; number;





if(number %26gt; 5 %26amp;%26amp; number %26lt; 22)





...


...


cout %26lt;%26lt; "*";


}


cout %26lt;%26lt; endl;





}











return 0;


}








I just can't figure out the function to draw the box... please help or give me a hint atleast... thanx

C++ Program to Print a Hollow Box?
int main(){


int triangles;


int number;


Char Run="";


int count = 0;//These are local variables they need to be within the function that uses them (in this case main)





cout%26lt;%26lt;"Run program Y/N?"\n%26lt;%26lt;endl;


cin%26gt;%26gt;Run;


If(Run.Tostring()=="Y" || Run.ToString()=="y")


{


cout %26lt;%26lt; "Drawing hollow boxes program.\n" %26lt;%26lt; endl;


do{


cout %26lt;%26lt; "How many rows/columns(5-21)? ";


cin %26gt;%26gt; number;//If this is always going to be a square then 1 input is ok. but otherwise youll need to input 2 differnt numbers one for row, one for col.


}while(number%26lt;5 || number%26gt;21)





int i=0;


//print top


do{


cout%26lt;%26lt;"*"%26lt;%26lt;;


i++;


}


While(i%26lt;=number)


cout%26lt;%26lt;\n%26lt;%26lt;;


int row, col=0;





//print mid section


for(row=0;row%26lt;number-2;row++)


{


cout%26lt;%26lt;"*"%26lt;%26lt;;


for(col=0;col%26lt;number-2;col++)


{


cout%26lt;%26lt;" "%26lt;%26lt;;


}


cout%26lt;%26lt;"*"%26lt;%26lt;;


}





//print bottom


i=0;


do{


cout%26lt;%26lt;"*"%26lt;%26lt;;


i++;


}


While(i%26lt;=number)


cout%26lt;%26lt;\n%26lt;%26lt;;


Else


{


Return 0;


}


return 0;


}





or better yet replace the print top and print bottom sections with:





int i=3;


String *Top = New String();


Top="****";





//print top


do{


Top=String::Concat(Top.ToString() %26amp; "*")


i++;


}


While(i%26lt;number)


cout%26lt;%26lt;Top\n%26lt;%26lt;;











This way you only need one loop instead of 2. And youre only calling the cout function twice instead of 10-42 times.

flowers on line

No comments:

Post a Comment