Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 11th, 2005, 6:44 PM   #1
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
I'm on a role with these questions here...

Make this, only using while statements:


user inputs a number: (for example, 5)

* * * * *
* *
* *
* *
* * * * *
and makes a square of asterisks, with 5 asterisks on each side. Here's my code so far, and i'm stuck...



// Exercise 4.25 Asterisk square problem.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int number;
cout << "Enter a number to make a square with that many number of asterisks on each side: ";
cin >> number;

int count = 0, column = 0, wideness = 0, rows = 0;

cout << endl << endl << endl;

while (count < number)
{
cout << "* ";
count++;
}
cout << endl;

while (rows < number - 2)
{
cout << "*";

while (column < number-2)
{
cout << " ";
column++;
}
cout << " ";
cout << "*";
cout << endl;
rows++;
}

while (wideness < number)
{
cout << endl <<"* ";
wideness++;
}
return 0;
}
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 11th, 2005, 6:46 PM   #2
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
and it's not making me the box I want....


o o o o o
o o
o o
o o
o o o o o
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 11th, 2005, 6:47 PM   #3
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
ok, the box has the number of asterisks on each side, boardering the square, that the user inputs... it won't let me do it, it keeps pushing the outside border of asterisks on the left side of the square, even in my program...
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 11th, 2005, 7:04 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Edit your posts, and put both the squares and the C++ code in [code] tags.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 11th, 2005, 7:20 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Here's how I did it in Python:
def square (size):
	print "*" * size
	print ("*" + " " * (size - 2) + "*\n") * (size - 2),
	print "*" * size
Thank god for string multiplication. To do this in C++, replace every instance of the multiplication operator with a loop.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 11th, 2005, 7:31 PM   #6
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Cool

i didn't see anything in the project specs about the square being empty. was that left out, or did you just assume that? obviously, it would be much easier if the square were filled like:

*****
*****
*****
*****
*****
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Apr 11th, 2005, 7:45 PM   #7
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
supposed to be empty
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 11th, 2005, 8:08 PM   #8
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Wink

ahhh. i'm always looking for the easy way out.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Apr 11th, 2005, 8:18 PM   #9
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
me too, too bad teachers don't.
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 11th, 2005, 8:32 PM   #10
ice52
Newbie
 
Join Date: Apr 2005
Posts: 9
Rep Power: 0 ice52 is on a distinguished road
you need to re-initialise your variables before you use them again... particullarly in the nested loop, heres some working code:

#include <iostream>
using namespace std;

int main()
{
	int number;
	cout << "Enter a number to make a square with that many number of asterisks on each side: ";
	cin >> number;
	
	int count = 0, column = 0, wideness = 0, rows = 0;
	
	cout << endl << endl << endl;
	
	while (count < number){
		cout << "* ";
		count++;
	}
	cout << endl;
	
	while (rows < number - 2){
		cout << "* ";
		column=0;
		while (column < number - 2)
		{
		cout << "  ";
		column++;
	}
	
		cout << "* ";
		cout << endl;
		rows++;
	}
	
	count=0;
	
	while (count < number){
		cout << "* ";
		count++;
	}
	return 0;
}

not sure what was going on with this:
Quote:
while (wideness < number)
{
cout << endl <<"* ";
wideness++;
}
you put an endl in there i assume that was a mistake...
__________________
www.ice52.co.uk

Last edited by ice52; Apr 11th, 2005 at 8:38 PM.
ice52 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC