0

I made figures with * and when i display it once it works fine but if i try to display it again and again it, the botton of the figure is the only thing that shows I am not sure what is wrong with the loops?
maybe someone can see what is wrong
thanks in advance

#pragma once
/*GOYO
*/
class Shape
{
public:
    virtual void Draw ()=0;
	virtual void Clear()=0;
    virtual void Center()=0;
   
};
#include"Shape.h"
#include"Triangle.h"
#include"Rectangle.h"

#include"Square.h"
using namespace std;
int main()
{
	Square squar;
	Rectangle rect;
	//rect.Draw();
	Triangle tri;
	//tri.Draw();
	void menu();
	char shape;
	
	while(true)
	{
	void menu(char&shape);
	{
		cout<<"please select one of the following"<<endl;
		cout<<"E Erase shape:\n"
			<<"S Square:\n"
			<<"R Rectangle:\n"
			<<"T Triangle:\n"<<endl;
		cin>>shape;
shape = static_cast<char>(toupper(shape));
    while(shape != 'E' && shape != 'R' 
          && shape && shape != 'T'&& shape !='S')
    {
        cout<<"please select one of the following"<<endl;
		cout<<"E Erase shape:\n"
			<<"S Square:\n"
			<<"R Rectangle:\n"
			<<"T Triangle:\n"<<endl;
		cin>>shape;
        shape = static_cast<char>(toupper(shape));
     }///end while   
    switch(shape)
    {
           case 'E':
                tri.Clear();
                break;
		   case 'S':
			   squar.Draw();
			   break;
           case 'R':
                rect.Draw();
                break;
           case 'T':
                tri.Draw();
                break;
    }// end switch
	

	}//menu;
	}
	return 0;
	

}
#include<iostream>
#include"Shape.h"
using namespace std;
/*
RECTANGLE 
*/
class Rectangle : public Shape
{
public:
    Rectangle ();
    virtual void Draw ();
	virtual void Clear();
	virtual void Center();
   

private:
	int height;
    int row;
    int col;
};
Rectangle::Rectangle()
{	height = 5;
	row = 0;
	col = 0;
}
    
 void Rectangle::Draw()
{
	
	
   // draw top line
   while (col < height * 2) 
   {
      cout << '*';
      ++col;
   }

   cout << endl;
   ++row;

   // draw middle lines
   while (row < height - 1) 
   {

      col = 0;

      while (col <= height * 2) 
	  {

         ++col;

         if (col == 1)
            cout << '*';
         else {
            if (col == height * 2)
               cout << '*';
            else
               cout << ' ';
         }
      }

      cout << endl;
      ++row;
   }

   // draw bottom line
   col = 0;

   while (col < height * 2) 
   {
      cout << '*';
      ++col;
   }

   // new line after figure
   cout << endl;

   // blank line between figures
   cout << endl;
	
				
}

 void Rectangle::Clear()
{
   system("cls");
}

void Rectangle::Center()
{
	Clear();
	Draw();
	
    
}
/* triangle */
#include"Shape.h"
#include<iostream>
using namespace std;

class Triangle : public Shape
{
public:
    Triangle();
    virtual void Draw();
	virtual void Clear();
	virtual void Center();
	
   
private:
	int col;
    int row;
    int height;
};
 
Triangle::Triangle()
{
	col=0;
	height = 5;
	row =0;
}


 void Triangle::Draw()
{


   // draw rows above base
   while (row < height - 1)
   {
      col = 0;

      while (col < height + row) {

         ++col;

         if (col == height - row)
            cout << '*';
         else {

            if (col == height + row)
               cout << '*';
            else
               cout << ' ';
         }
      }

      cout << endl;
      ++row;
   }

   // draw the base
   col = 0;

   while (col < height * 2 - 1) {
      cout << '*';
      ++col;
   }

   // new line after figure
   cout << endl;

}
    
 void Triangle::Clear() 
{
	system("cls");
}

 void Triangle::Center()
{
	Clear();
	Draw();
   
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Lerner
0

I haven't compiled and run your program, but the first thing that makes me uncomfortable is the way you are using the menu and th switch statement. Stop trying to make the menu display a function since you only have to write it once either way, use a default switch statement to deal with irrelevant input, and give yourself a way out of the loop using a flag/sentinnel boolean variable.

bool more = true;
while(more)
{		
    cout<<"please select one of the following"<<endl;
    cout<<"E Erase shape:\n"
           <<"S Square:\n"
           <<"R Rectangle:\n"
           <<"T Triangle:\n"
           << "X exit program"
           <<endl;
     cin>>shape;
    
     switch(shape)
    {
           case 'E':
                tri.Clear();
                break;
            case 'S':
                squar.Draw();
                break;
           case 'R':
                rect.Draw();
                break;
           case 'T':
                tri.Draw();
                break;
           case 'X':
                 more = false;
                 break;
            default:
                 cout << "irrelevant entry.  try again" << endl;
                 break;
    }// end switch
}//end while[

If you want to display the menu using a function, declare it and define it outside of main().

The next thing that makes me uncomforable is the attempt to clear a shape using a system call to cls. First, don't use system calls unless you know what you are doing. In this case cls doesn't clear data in an object, it clears the screen. You need to develop a different implementation of the Clear() methods.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.