Question:
REctangle code C++ 2005 express?
jo d
2007-10-15 17:40:46 UTC
Im having a hard time, can some one help me with this program??? This is my assignment, im lost
Write a program that displays a rectangle of given the length (an integer size less
than or equal to 21) and the width (an integer less than or equal to 79). Your
program should accept as input from the keyboard the character used to form the
rectangle, and the two values for the length and width of the rectangle.

Out of range input values should result in an informative error message.

Your program should process only one input case, legal or not. Termination occurs
after either the error message is printed or the diamond is drawn.

It is necessary that the program be written using loops.

Sample runs:

Rectangle drawing program
Enter the character, and length and width (eg. x 7):A 7 12
AAAAAAAAAAAA
A A
A A
A A
A A
A A
AAAAAAAAAAAA

Rectangle drawing program
Enter the character, and length and width (eg. x 7):R 3 5
RRRRR
R R
RRRRR

Rectangle drawing program
Enter the character, and length and width (eg. x 7):A 27 32
You have entered an illegal value for the length. It must be between 1 and 21.

You should use a named constant (const) to define and refer to the box size limits.
This will make it easy to modify the program at a later date if the size restriction is
changed. Write a class called Rectangle. Have the appropriate data members for
that class. Also have a constructor and a function to output the rectangle.
Three answers:
Adopted
2007-10-15 18:22:07 UTC
So you need to have 2 functions, one for the header and footer block, and the other for the middle pieces. Then using math figure out how many times to do it.



psuedo code:



gets(userinput);



for ( int i = 0; i < userinput[2]; i++ )

{

int length = userinput[4];



if (userinput[2] == i || i == 0 )

drawFullCharacterRowOf ( userinput[0], length );

else

drawPartialCharacterRowOf( userinput[0] );

}





void drawFullCharacterRowOf ( char val, int length)

{

for ( int i = 0; i < length; i++ )

cout << val << endl;

}





This is extremely pseudo code, but you see the point I would imagine.
Koushik Biswas
2007-10-15 18:25:37 UTC
// TestCPPConsole.cpp : Defines the entry point for the console application.

//



#include "stdafx.h"

#define MAX_LENGTH 21

#define MAX_WIDTH 79



class Rectangle

{

private:

char m_cUseThisChar;

int m_iHeight;

int m_iWidth;



public:

Rectangle( char cUseThisChar, int iHeight, int iWidth )

{

m_cUseThisChar = cUseThisChar;

m_iHeight = iHeight;

m_iWidth = iWidth;

};



void DrawRectangle( char * szRectangle )

{

char * szTemp = ( char * ) malloc( ( m_iWidth + 2 ) * sizeof( char ) );

try

{

szRectangle[ 0 ] = 0;



memset( szTemp, 0, m_iWidth + 2 );

memset( szTemp, m_cUseThisChar, m_iWidth );

szTemp[ m_iWidth ] = '\n';



for( int iCnt = 1; iCnt <= m_iHeight; iCnt++ )

strcat( szRectangle, szTemp );

}

catch(...)

{

strcpy( szRectangle, "Error" );

}

if( szTemp != NULL )

{

free( szTemp );

szTemp = NULL;

}

};

};



int _tmain(int argc, _TCHAR* argv[])

{

char cUseThisChar;

int iHeight, iWidth;

printf( "Enter the character, and length and width (eg. x %d %d) ):\n", MAX_LENGTH, MAX_WIDTH );

scanf( "%c %d %d", &cUseThisChar, &iHeight, &iWidth );



if( iHeight > MAX_LENGTH )

{

printf( "You have entered an illegal value for the length. It must be between 1 and %d.", MAX_LENGTH );

getch();

return 1;

}

if( iWidth > MAX_WIDTH )

{

printf( "You have entered an illegal value for the width. It must be between 1 and %d.", MAX_WIDTH );

getch();

return 1;

}



Rectangle rect( cUseThisChar, iHeight, iWidth );

char * szBuffer = ( char * ) malloc( ( ( ( iWidth + 2 ) * iHeight ) + 10 ) * sizeof( char ) );

rect.DrawRectangle( szBuffer );

printf( szBuffer );

free( szBuffer );

szBuffer = NULL;

getch();

return 0;

}

==========================================

Disclaimers:

1. Content of stdafx.h file is:



//-------------------------------------------------------------

#pragma once

#define WIN32_LEAN_AND_MEAN

#include

#include

#include

#include

#include

#include

//-------------------------------------------------------------



2. Error handling must be improved

3. Due to deprecation, functions like getch(), strcpy() etc will get warnings. Use other library methods if you are picky.
?
2016-12-18 13:36:58 UTC
you will get Microsoft considered C++ loose from Microsoft yet now no longer considered Studio. The compilers are in Microsoft SDK and DDK (perhaps now no longer all types). I even have been given Microsoft Platform SDK for homestead homestead windows Server 2003 SP1 for the sixty 4 bit and 32 bit compilers and sixty 4 bit assembler and DDK for 32 bit compiler and assembler. without considered Studio, the compilers/assemblers could be run by using command line ideas.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...