Hi all, I need to draw 3 shapes and put them into an output file. I'm having problems. Suppose I enter 7 into the console then the shapes need to be like this.
I'm having problems with the looping structure, so if anyone could give their input and maybe throw a little bit of code my way, not asking you to write the program, I just need help and maybe a different view will lead me the right way, i have searched but it didnt help :(
You can use for loops that increment a variable until it is equal to another variable.
For example:
1 2 3 4
for (a=0;a<8;a++)
{
//code you want here
}
That's what the for loop would look like, not for any of these shapes but it's a starting point. It's also going to be as much as I'll post until you show your own code.
/*
*
*
*/
/*
*
*
*/
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ofstream DataOut; //output file object
int n, i;
int j, k;
DataOut.open("Output.txt"); //open output
cout<< "enter a number 1-20";
cin>> n;
if ( n <= 0 || n > 21)
{
cout<<" Invalid number. Please type a number between 1-20. ";
}
do
{
}
while (n < 0 || n > 20);
do{
for (i = 1; i <= n; i++)
{
cout << endl;
for (j = 1; j <= i; j++)
{
DataOut << "@\t";
}
DataOut << endl;
}
} while (i <= n);
return 0;
}
this code outputs
@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
This is the last one that I need to do. I know how I can do the middle shape in my examples, the square I'm a little confused on? I am a noob, thanks again for any help.
also if I type in 0 or anything over 20, it doesn't cout please re enter a number 1-20
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ofstream DataOut; //output file object
int Num; //number of characters you want to be displayed in given shape
constchar PERCENT = '%'; // const char %
constchar AT = '@'; //const char @
constchar AND = '&'; //const char &
DataOut.open("Output.txt"); //open output
if(DataOut)
{
cout<< "The output file was created successfully." <<endl<<endl;
}
while (!( Num > 0 && Num < 21))
{
cout<< "Please enter a number 1-20.";
cin>> Num;
if (Num>20 || Num<1) //check to see if number is less 1-20 and prompt user to re enter proper number
{
cout<< "Invalid number."<<endl;
}
}
//square
for (int i = 0; i < Num; i++)
{
for (int j = 0; j < Num; j++)
{
DataOut << PERCENT ;
}
DataOut << endl; }
//upside down triangle
for (int i = 0; i < Num; i++)
{
for (int j = Num; j > i; j--)
DataOut << AT;
DataOut << endl;
}
DataOut <<endl;
//regular triangle
for (int i = 0; i < Num; i++)
{
for (int j = 0; j <= i; j++)
DataOut << AND;
DataOut << endl;
}
return 0;
}