Need some help!! newbie here
Nov 4, 2016 at 4:31am UTC
Hello, I'm new here and to C++ and for some reason my code output repeats itself and displays infinite asterisks when i am trying to output its shape with asterisks. any help is appreciated thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <iostream>
#include "lab9.h"
using namespace std;
void drawPixel(char c)
{
int width, height, slant, side;
switch (c)
{
case 'w' :
case 'W' :
drawRectFrame(width, height);
break ;
case 'x' :
case 'X' :
drawXFrame(slant);
break ;
case 'l' :
case 'L' :
drawTriFrame(side);
break ;
}
return ;
}
int main()
{
char c;
do
{
cout << "Input a character that will either be w, x, or l for the 3 shapes..." << endl;
cin >> c;
if (c == 'w' )
{
int width, height;
cout << "Width of rectangle: " ;
cin >> width;
cout << "Height of rectangle: " ;
cin >> height;
drawPixel(c);
}
else if (c == 'x' )
{
int slant;
cout << "Enter an odd integer greater than 2 for a perfect X: " ;
cin >> slant;
drawPixel(c);
}
else if (c == 'l' )
{
int side;
cout << "Enter base and height of triangle: " ;
cin >> side;
drawPixel(c);
}
else
cout << "Please input a legal character w, x or l." << endl;
} while ((c != 'w' ) || (c != 'W' ) || (c != 'x' ) || (c != 'X' ) || (c != 'l' ) || (c != 'L' ));
return 0;
Last edited on Nov 4, 2016 at 4:38am UTC
Nov 4, 2016 at 4:35am UTC
Here's the header for the functions, maybe something's wrong there?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
void drawRectFrame(int width, int height)
{
for (int iw=0; iw < width; iw++)
{
cout << '*' ;
}
cout << endl;
int i=0;
while (i < height-2)
{
cout << '*' ;
for (int ih=0; ih < width-2; ih++)
{
cout << ' ' ;
}
cout << '*' << endl;
i++;
}
for (int iw=0; iw < width; iw++) cout << '*' ;
return ;
}
void drawXFrame(int slant)
{
for (int rows=1; rows <= slant; rows++)
{
for (int cols=1; cols <= slant; cols++)
{
if ((rows == cols) || (cols == (slant+1)-rows))
{
cout << '*' ;
}
else
{
cout << ' ' ;
}
}
cout << endl;
}
return ;
}
void drawTriFrame(int side)
{
for (int stairs=1; stairs <= side; stairs++)
{
for (int col=1; col <= stairs; col++)
{
if (col > stairs)
{
cout << ' ' ;
}
else
{
cout << '*' ;
}
}
cout << endl;
}
return ;
}
Nov 4, 2016 at 9:41am UTC
You ask it to draw either a rectangle, cross or triangle ... and then you try and draw a pixel!
drawPixel(c);
Amended your main() function at the three lines where it calls a function to draw a shape.
You still have to fix the prompt for your triangle - not clear what you intended.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
int main()
{
char c;
do
{
cout << "\nInput a character that will either be w, x, or l for the 3 shapes..." << endl;
cin >> c;
if (c == 'w' )
{
int width, height;
cout << "Width of rectangle: " ;
cin >> width;
cout << "Height of rectangle: " ;
cin >> height;
drawRectFrame( width, height); // CHANGED
}
else if (c == 'x' )
{
int slant;
cout << "Enter an odd integer greater than 2 for a perfect X: " ;
cin >> slant;
drawXFrame(slant); // CHANGED
}
else if (c == 'l' )
{
int side;
cout << "Enter base ( FIX THIS - and height - FIX THIS ) of triangle: " ;
cin >> side;
drawTriFrame(side); // CHANGED
}
else
cout << "Please input a legal character w, x or l." << endl;
} while ((c != 'w' ) || (c != 'W' ) || (c != 'x' ) || (c != 'X' ) || (c != 'l' ) || (c != 'L' ));
return 0;
}
Topic archived. No new replies allowed.