Click here to Skip to main content
15,441,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
int rectangle , square, number;


main ()
{

	printf ("Please Enter choice of Shape: ");
	printf ("\n1. Rectangle");
	printf ("\n2. Square");
	printf ("\n3. Triangle");
	printf ("\n4. Circle");
	printf ("\n5. Parallelogram");
	printf ("\n6. Ellipse \n");
	scanf ("%d", &number);
	
	
	if (number == rectangle)
	{
		int length , width , parimeter , area;

		printf("Good day!\n");
		printf("Input length: ");
		scanf ("%d" , &length);
		printf("Input width: ");
		scanf ("%d" , &width);
	
	
		area = (length * width);
		parimeter = 2 * (length + width);
	
		printf ("Area of a Rectangle = %d \n" , area);
		printf ("Parameter of a Rectangle = %d \n" , parimeter);
	}
	else if (number == square)
	{
		int length , width , parimeter , area;
		printf("Good day!\n");
		printf("Input length: ");
		scanf ("%d" , &length);
		printf("Input width: ");
		scanf ("%d" , &width);
	
	
		area = (length * width);
		parimeter = (4 * width);
	
		printf ("Area of a square = %d \n" , area);
		printf ("Parameter of a square = %d \n" , parimeter);
	}
}

What I have tried:

We can't use switch or cases in this activity, only if and else statements. But Im stuck in this problem
Posted
Updated 10-Oct-21 1:07am
v3
Comments
OriginalGriff 7-Oct-21 3:26am     CRLF
This is not a good question - we cannot work out from that little what you are trying to do. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. Use the "Improve question" widget to edit your question and provide better information.
merano99 10-Oct-21 4:20am    
You marked this as C ++, but you are writing pure C code. If C ++ is allowed, the solution from KarstenK would be recommended, otherwise that from CPalini.

C++
if (number == rectangle)

You cannot do that, since rectangle has not been initialised. You should compare number to the actual value you used in the question:
C++
if (number == 1)

Or better still use a switch block, with the 6 case tests.
 
Share this answer
 
Something like
C
#include <stdio.h>

enum
{
  eRectangle = 1,
  eSquare,
  eTriangle,
  eCircle,
  eParallel,
  eEllipse
};

void solve_rectangle();
void solve_square();


int main ()
{
  int choice;
  printf ("Please Enter choice of Shape: ");
  printf ("\n1. Rectangle");
  printf ("\n2. Square");
  printf ("\n3. Triangle");
  printf ("\n4. Circle");
  printf ("\n5. Parallelogram");
  printf ("\n6. Ellipse \n");
  scanf ("%d", &choice);

  switch ( choice )
  {
  case eRectangle:
    solve_rectangle();
    break;
  case eSquare:
    solve_square();
    break;
  //...
  default:
    break;
  }
  return 0;
}

void solve_rectangle()
{
  int length, width;
  int area, perimeter;
  printf("Input length: ");
  scanf ("%d" , &length);
  printf("Input width: ");
  scanf ("%d" , &width);
  area = (length * width);
  perimeter = 2 * (length + width);
  printf ("Area of the Rectangle = %d \n" , area);
  printf ("Perimeter of the Rectangle = %d \n" , perimeter);
}

void solve_square()
{
  int length;
  int area, perimeter;
  printf("Input length: ");
  scanf ("%d" , &length);
  area = length * length;
  perimeter = 4 * length;
  printf ("Area of the Square = %d \n" , area);
  printf ("Perimeter of the Square = %d \n" , perimeter);
}

?
 
Share this answer
 
I mostly recommend the previous answer of CPallini, but you can do better by using classes, interfaces and inheritance. Here is some interface tutorial.
C++
class Shape {
  int area;
  int perimeter;
 
  void print();
  virtual void compute() = 0;
  virtual void askData() = 0;
};

class Rectangle: Shape {
  // override functions
  void compute() {
  // your task
  }

}
If it is a homework your teacher will like it.
 
Share this answer
 
Comments
merano99 10-Oct-21 4:22am    
If C++ ist allowed, combine it with the solution from CPallini and you have a nice Solution.
Extended version based on KarstenK with protected member variables.

C++
const enum SharpForm 
  { SH_NONE, SH_RECTANGLE, SH_SQUARE, SH_TRIANGLE, SH_CIRCLE, 
    SH_PARALLEL, SH_ELLIPSE, SH_SHARPCOUNT};

int ShowMenue() {
    int sel = 0;
    cout << "\nPlease Enter choice of Shape: \n";
    cout << "1. Rectangle\n";
    cout << "2. Square\n";
    cout << "3. Triangle\n";
    cout << "4. Circle\n";
    cout << "5. Parallelogram\n";
    cout << "6. Ellipse\n";
    cout << "99. Exit\n";
    cin >> sel;
    return sel;
};

class Shape {
public:
	Shape():area(0), perimeter(0) {}
	void SetArea(int x) { area = x; }
	void SetPerimeter(int x) { perimeter = x; }
	int GetArea() { return area; }
	int GetPerimeter() { return perimeter; }
	virtual void AskData() = 0;
	virtual void PrintResult() = 0;
private:
	virtual void compute() = 0;
	int area;       // 
	int perimeter;  // 
};

class Rectangle : public Shape {
public:
	Rectangle():length(0), width(0) {};
	// override functions
	void AskData() override;
	void PrintResult() override;
private:
	void compute() override;
	int length, width;
};

void Rectangle::AskData() {
	cout << "Input length: ";
	cin >> length;
	cout << "Input width: ";
	cin >> width;
	compute();
};

void Rectangle::compute() {...}
void Rectangle::PrintResult() {...}

// class Square:Shape { ... }

int main()
{
    int choice = 0;
    do {
        choice = ShowMenue();

        // case not allowed, use if else
        if (choice == SH_RECTANGLE) {
            Rectangle r;
            r.AskData();
            r.PrintResult();
            }
        // else if(coice == SH_SQUARE,
        // else if(coice == SH_TRIANGLE,
        // else if(coice == SH_CIRCLE,
        // else if(coice == SH_PARALLEL,
        // else if(coice == SH_ELLIPSE};
    } while (choice != 99);

    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900