I didn't want to ask for help on this seemingly easy program, but...

Started by
10 comments, last by -dcx- 21 years, 6 months ago
...For some reason this one is giving me a little bit of trouble. I was reading through one of the tutorial books and for one of the exercises it asks to create a program that takes two positive integer, one representing height and one width. And depending on the given numbers, eg. width=8 height=4, the program should output a box with asterix like this: ******** *...........* *...........* ******** (not including the periods) The book says the main program should call a function that takes two const unsigned arguments called width and height and then draws the rectangle. I've made similar program like this before where there is a name in the middle of the box, but I don't know what to do with this one. I've never even used 'const unsigned' before, how does that work? Here's the code for the other program I made: #include <iostream> #include <string> int main() { std::cout << "Enter Name: "; std::string name; std::cin >> name; //the message const std::string greeting = "Hello, " + name + "!"; //2nd/4th lines const std::string spaces(greeting.size(), ' '); const std::string second = "* " + spaces + " *"; //first and last lines const std::string first(second.size(), '*'); //print out std::cout << std::endl; std::cout << first << std::endl; std::cout << second << std::endl; std::cout << "* " << greeting << " *" << std::endl; std::cout << second << std::endl; std::cout << first << std::endl; return 0; } Is there anything I can change in my greeting program but without the name, sorry I'm completely stuck and can someone tell me what const unsigned does, I've never used it before. [edited by - -dcx- on October 6, 2002 4:34:27 PM]
Advertisement
Here is one way to do it, and it could be done with std::string to cut out all the repetitive couts.



        void drawRect(const unsigned int w, const unsigned int h) {	unsigned int w1 = 0;	unsigned int h1 = 0;		while(h1++ < h) {		while(w1++ < w) cout << '*';		cout << '\n';		w1 = 0;	}}        


and onto const and unsigned.

first const, which is short for constant.

a const variable is a variable that CANNOT be changed, for instance this is illegal.

const int myVar = 12321; // So far so good.myVar += 12; // ERROR 



as for unsigned its pretty much self explanatory, it means the number doesn't have a sign, so it is always a positive number.

also having an unsigned data type changes its "Range", example...

a normal short variable has a range of -32,768 to 32,767, which means it can hold the value of any integer between those to values.

An unsigned short has a range of 0 to 65,535.



[edited by - Xanth on October 6, 2002 5:02:45 PM]

[edited by - Xanth on October 6, 2002 5:08:00 PM]
"I thought Genius lived in bottles..." - Patrick Star
That code works, but it''s not quite what I''m trying to do. When the box gets output it''s suppose come out as a frame with no stars in between meaning that there''s one row on the top and one on the bottom, and then there''s another row on the left and one on the right.

How would I do that?
char* stars="******\n";char* space="*    *\n";cout << stars << space << space << space << space << stars; 


    #include <windows.h>                         // Needed Header...COORD myCoord;                               // COordinates, contains X and YHANDLE output;                               // handle...myCoord.X = 0;                               // Set valuesmyCoord.Y = 0;output = GetStdHandle( STD_OUTPUT_HANDLE );  // Create an OUTPUT handle...for( int line=0; line<4; line++ )            // For Loop Start{myCoord.X = line;SetConsoleCursorPosition( output , myCoord );if( line == 0 || line == 3 ){cout << "******\n" ;}else{cout << "*    *\n" ;}}                                            // For Loop End      



use: [ c o d e ] [ / c o d e ] and [ / s o u r c e ] <br><br>[edit] why is my text green?!<br><br><font size=1><a href="mailto:rick_rackplayer@hotmail.com">.lick</a></font><br><br><br><SPAN CLASS=editedby>[edited by - pipo declown &#111;n October 7, 2002 10:44:48 AM]</SPAN>
I think Xenoth gave enough of a definition of const unsigned, so I won''t go into that. But as to the ''answer'' to the problem I believe you should try to work it out yourself, but I will give you a hint.

First use a double for loop

for(...)
{
for(...)
{
.
.
}
}

The middle for loop will output the width
The outside loop will output the hieght, meaning
after each iteration of one outside loop you will have to have an cout << endl; statement to move to the next line.

How do you make spaces? Hint: use function setw() look it up in the msdn if your confused about how to use it but basically you use cout << "*" << setw(int n) << "*"; Which will set the allignment of the second start n spaces away.

Hope this helps, try working through the code yourself a little more with these new tips. If someone gives you the answer you won''t feel satisfied, if you accomplish it yourself it is a great feeling and you learn more.
That's very helpful thanks, I'll give it a shot right now.

I overlooked the post with the coding because I did want to try it for myself, I just needed a few pointers, but thanks anyways.


[edited by - -dcx- on October 7, 2002 2:38:22 PM]
Eh...I still can''t get it to work. Do you have any other tips that would help me out?
I''ll have ato make this short because I''m between classes, but

First this is a simpler version

have a loop that does the first row
for(int x=0;x
and print out the stars

then have a loop that does the middle rows which is a double loop
out side loop for cout << endl;
and inside loop for cout << "*" << setw(width) << "*";

then have the last loop do the same thing the first loop does



I havn''t given you the exact code yet but I''m getting pretty close to giving it to you with this version I just wrote. If you still can''t get it to work please post your function here and I''ll tell you what you did wrong.
Finally got it to work, and thanks for the help, I really appreciate it.

I usually can handle making programs that involve multi dimensional arrays, structures, classes, files, and other things you learn after one year in CS, but for some reason these kinds still give me problems.

Ph33r, nice. I'd forgotten about the setw() function... I did it the hard way...


    void drawFrame(const unsigned int w, const unsigned int h) {  string rect;  unsigned int w1 = 0;  unsigned int h1 = 0;	  while(h1++ < h) {    if(h1 > 1 && h1 < h) rect += '*';    while(w1++ < w) {      if(h1 > 1 && h1 < h)  {        if(w1 > w-2) break;        rect += ' ';      } else rect += '*';    }		    if (h1 > 1 && h1 < h) rect += '*';    rect += '\n';    w1 = 0;  }	  cout << rect;  return;}  


=\ Gotta keep familiarizing myself with the STL.

[edited by - Xanth on October 8, 2002 9:45:50 PM]
"I thought Genius lived in bottles..." - Patrick Star

This topic is closed to new replies.

Advertisement