Hey guys, im writing a program that takes a user's input and prints out a hexagon based on it.
i've nearly finished it but im a little stuck on two things the first is getting it to print horizontal lines as its supposed to do one row of horizontal line then one row of spaces.
the second is error checking i know in c# there is try parse to check if an input matches the data type but i don't know what the equivalent is in c++.
#include <iostream>
#include <string>
usingnamespace std;
//This class is to store all the functions needed to create the hexagons
class Hexagon
{
private:
//size stores the sizeInput value from the main class
//type stores the typeInput value from the main class
//fill stores the content to fill the hexagon
int size;
int type;
string fill;
public:
//int getSize() {return size;}
//int getType() {return type;}
// basic constructor and deconstructor to allow instances to be created
Hexagon(int sizeInput = 0, int typeInput = 0) : size(sizeInput), type(typeInput)
{
}
~Hexagon()
{
}
//function to create empty and filled hexagons
void BlankAndFill(int size, int type)
{
//checks which type of hexagon user wants and sets fill accordingly
if(type == 1)
{
fill = " ";
}
elseif(type == 2)
{
fill = "+";
}
//prints out what type of hexagon is being made
cout << "Type " << type << endl;
//stores row number for use as a counter
int rows = 0;
//prints out first half of the hexagon
for(; rows < size+1;rows++)
{
//for first row prints out top of hexagon
if(rows==0)
{
//this for loop prints out required spaces
for(int spaces = 1+size; spaces > rows;spaces--)
{
cout << " ";
}
//this for loop prints out top of hexagon
for(int tops = 0; tops < size;tops++)
{
cout << "_";
}
cout << endl;
}
else
{
for(int spaces = 1+size;spaces > rows;spaces--)
{
cout << " ";
}
cout << "/";
//this for loop prints out the content of the hexagon
for(int content=0; content < size + ((rows - 1) * 2);content++)
{
cout << fill;
}
cout << "\\";
cout << endl;
}
}
//this for loop prints out the second half of the hexagon
for(; rows >= 2; rows--)
{
if(rows == 2)
{
for(int spaces = 2+size;spaces > rows;spaces--)
{
cout << " ";
}
cout << "\\";
//this for loop prints out the bottom line of the hexagon
for(int bottoms = 0;bottoms < size;bottoms++)
{
cout << "_";
}
cout << "/";
cout << endl;
}
else
{
for(int spaces = 2+size;spaces > rows;spaces--)
{
cout << " ";
}
cout << "\\";
for(int contents=0; contents < size + ((rows - 2) *2);contents++)
{
cout << fill;
}
cout << "/";
cout << endl;
}
}
//empties the rows variable to allow multiple hexagons to be printed
rows =0;
}
//function to create hexagons filled with horizontal or vertical lines
void XandY(int sizeInput, int type)
{
//spacer stores spaces to be used to properly space hexagons and add gaps inside them
//counter stores number of iterations that have currently passed so inside spaces get placed properly
string spacer = " ";
int counter =0;
if(type == 3)
{
fill = "-";
}
elseif(type == 4)
{
fill = "|";
}
cout << "Type " << type << endl;
int rows = 0;
for(; rows < size+1;rows++)
{
if(rows==0)
{
for(int spaces = 1+size; spaces > rows;spaces--)
{
cout << spacer;
}
for(int tops = 0; tops < size;tops++)
{
cout << "_";
}
cout << endl;
}
else
{
for(int spaces = 1+size;spaces > rows;spaces--)
{
cout << spacer;
}
cout << "/";
for(int stars=0; stars < size + ((rows - 1) * 2);stars++)
{
//checks if current iteration is even and if type is 4
//if so prints a spacer instead of the filler
if(counter % 2 == 0 && type == 4)
{
cout << spacer;
counter++;
}
else
{
cout << fill;
counter++;
}
}
cout << "\\";
cout << endl;
}
}
//empties counter value so iteration count is reset
counter = 0;
for(; rows >= 2; rows--)
{
if(rows == 2)
{
for(int spaces = 2+size;spaces > rows;spaces--)
{
cout << spacer;
}
cout << "\\";
for(int bottoms = 0;bottoms < size;bottoms++)
{
cout << "_";
}
cout << "/";
cout << endl;
}
else
{
for(int spaces = 2+size;spaces > rows;spaces--)
{
cout << spacer;
}
cout << "\\";
for(int stars=0; stars < size + ((rows - 2) *2);stars++)
{
if(counter % 2 == 0 && type == 4)
{
cout << spacer;
counter++;
}
else
{
cout << fill;
counter++;
}
}
cout << "/";
cout << endl;
}
}
rows =0;
counter = 0;
}
//takes sizeInput and typeInput and sets them to local variables
void setSize(int size) {size = size; return;}
void setType(int typeInput) {type = typeInput; return;}
};
int main()
{
//sizeInput and typeInput stores size/type of hexagon from user
//option stores whether use wants to continue making hexagons
int sizeInput, typeInput,optionInput = 1;
//this while loop keeps the program running until user chooses to exit
while(optionInput == 1)
{
//asks user for input then stores it in sizeInput variable
cout << "Please enter size of hexagon \n";
cin >> sizeInput;
cout << endl;
//checks if size inputted is less then 1, if it is then asks for another number
if(sizeInput < 1)
{
cout << "Please enter only positive integers \n";
cout << endl;
cout << "Please enter size of hexagon \n";
cin >> sizeInput;
cout << endl;
}
//allows user to choose type of hexagon they want and stores it in typeInput variable
cout << "Please choose type of hexagon \n";
cout << "1: Type 1 Empty\n";
cout << "2: Type 2 Filled\n";
cout << "3: Type 3 Horizontal\n";
cout << "4: Type 4 Vertical\n";
cin >> typeInput;
//creates an instance of hexagon and passes size and type into hexagon class
Hexagon h1(sizeInput, typeInput);
//checks typeInput variable the user inputted and runs the appropriate function
if(typeInput == 1 )
{
h1.BlankAndFill(sizeInput, typeInput);
}
if(typeInput == 2)
{
h1.BlankAndFill(sizeInput, typeInput);
}
if(typeInput == 3)
{
h1.XandY(sizeInput, typeInput);
}
if(typeInput == 4)
{
h1.XandY(sizeInput, typeInput);
}
//checks if user wants to draw another hexagon and stores choice in optionInput variable
cout << "Would you like to draw a hexagon? \n";
cout << "1: Yes \n";
cout << "2: No \n";
cin >> optionInput;
}
}
needed to ask different questions about same program and thought it'd be better to use my old topic instead of making a new one.
thought i'd also explain my attempt was to add a second counter in the rows loop then an if statement that prints out blank spaces if the row is even and lines if not.
but it instead just prints /----\ over and over again.