#include
<iostream>
#include
<string>
using
namespace
std;
//Object: Using loops to create pictures
//Pre:
Positive integer //Post: Produces fence according to number of
posts entered
void
simplefence(int
num)
{
while
(num>1) //condition
{
cout<<"|---"; //body,
in this case, a fence segment
num--;
//update
}
cout<<"|";
//Creates
last post
}
int
main ()
{
int
num; //defining
variable
do
//do-while
loop allows user to create fences as long there is "enough material"
{ cout<<
"Number
of posts in fence: ";
//prompt
cin>> num;
//input
if
(num>1)
{ simplefence(num);
cout<<endl;
//number
of calls equals height of fence
simplefence(num);
cout<<endl<<endl;
}
else
{
cout<< "Not
enough material to make a fence.";
}
}while
(num>1);
return
0;
}
#include
<iostream>
#include
<string>
using
namespace
std;
//Using loops to produce self-select sized fences
//Pre:
Two positive integers entered //Post: Generates fence according to user's
dimensions
void
bars (int
b)
{
while
(b>0) //Inner
loop (to make bars) is a separate function
{ cout<<"-";
} b--;
//update
}
void
fence (int
p, int
b)
{
while
(p>1) //Outer
loop
{ cout<<"|";
bars (b);
//Inner
loop
p--; //update
}
cout<<"|";
//Creates
the last post
}
int
printFence(int
p, int
b)
{
cout<<"Number
of posts and bars for the fence[x, y]? ";
cin>>p>>b;
if
(p>1 && b>0) //Minimum
requirements: two posts & one bar
{
fence (p,b); cout<<endl; fence (p,b); //#
of calls equals
height of fence
}
else
{ cout<< "Not
enough material for a fence.";
}
}
int
main ()
{
int
p,b;
printFence(p,b);
//Call
can be repeated as needed
return
0;
}