Decrement Operator

This program won't work with the decrement operator

Page 1 of 1

8 Replies - 1090 Views - Last Post: 08 October 2008 - 04:51 PM Rate Topic: -----

#1 smartin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-August 08

Decrement Operator

Post icon  Posted 07 October 2008 - 06:35 PM

Our assignment: Making a box

enter number of Columns you wish for your box

enter number of Rows you wish for your box

enter the character to use in building box

example char = & Columns = 6 Rows = 4

for (row = ___; row =< down; down--)
{
for (column =____; column =< across; across--)
{
cout <<_____
}
\n
}
add print statements to show how row, down, column, across are changing.

Main function
get number function
error function
calculation function
print function

[color=#FF0000]

I don't have any errors. I am stuck because it won't work with the decrement operator. It works fine with the increment operator, but when I change it, it fills the screen with the symbol. Is there something missing?


#include <iostream>
using namespace std;

void DrawBox (char, int, int);

int down;
int across;




int main()
{
	
	//get number function 

	char symbol;

	
	cout << "Enter number of Columns you wish for your box.\n";
	cin >> across;
	
	cout << "Enter number of Rows you wish for your box.\n";
	cin >> down;

	cout << "Enter the character to use in building box.\n";
	cin >> symbol;

	DrawBox(symbol, down, across);
	
	system ("pause");
	return 0;
	}
	
	//calculation function
	void DrawBox (char symbol, int down, int across)

	{
		int row;
		int col;

		for (row = 0; row <= down; row--)
	{
		for (col = 0; col <= across; col--)
		{
		cout << symbol;
	}
	cout <<  endl;
	}
return;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Decrement Operator

#2 GWatt  Icon User is online

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: Decrement Operator

Posted 07 October 2008 - 06:44 PM

in your for loops you have the condition that they should continue as long as row and col are less than or equal to down and across, but you decrease the value of row and col with each iteration.
if you want to use the decrement operator, do this
for (row = down; row >= 0; row--)
and the same thing with col.
Was This Post Helpful? 0
  • +
  • -

#3 demiserealized  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-September 08

Re: Decrement Operator

Posted 07 October 2008 - 06:47 PM

Well your setting row and column equal to zero and presumably the integers down and across are greater than 0. Hence your loop condition row<=down;--row
Is just an infinite loop. It is already less than or equal to down and your decrement is just making it more so.
Was This Post Helpful? 0
  • +
  • -

#4 smartin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-August 08

Re: Decrement Operator

Posted 08 October 2008 - 02:36 PM

View PostGWatt, on 7 Oct, 2008 - 06:44 PM, said:

in your for loops you have the condition that they should continue as long as row and col are less than or equal to down and across, but you decrease the value of row and col with each iteration.
if you want to use the decrement operator, do this
for (row = down; row >= 0; row--)
and the same thing with col.


Thank you for responding.
I made the following changes to my function, but now it increments rather than decrement. I don't understand.

{
		int row;
		int col;

		//for (row = 0; row <= down; row--)
		for (row = down; row >= 0; row--)
	{
		//for (col = 0; col <= across; col--)
		for (col = across; col >= 0; col--)
		{


Was This Post Helpful? 0
  • +
  • -

#5 smartin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-August 08

Re: Decrement Operator

Posted 08 October 2008 - 02:46 PM

View Postdemiserealized, on 7 Oct, 2008 - 06:47 PM, said:

Well your setting row and column equal to zero and presumably the integers down and across are greater than 0. Hence your loop condition row<=down;--row
Is just an infinite loop. It is already less than or equal to down and your decrement is just making it more so.

I don't quite follow what you are saying. Above, you can see the assignment. I have changed things around several different ways and can't get the program to do what is required. I tried setting row and col = 1, that didn't work. I tried to use the increment operator. It worked like it should, but when I use the decrement operator, it fills the screen with the symbol. I also tried the suggestions from Gwatt. I'm still stuck. Can you please elaborate on your comments? If it decrements to 0, seems like it wouldn't output anything. Why does it fill the screen with the symbol?
Was This Post Helpful? 0
  • +
  • -

#6 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Decrement Operator

Posted 08 October 2008 - 02:51 PM

He's saying that you're decrementing it, but your checking if it's less than or equal to.

Basically, your counter will decrement, and you'll check if it's still lower than a value. Because it decremented, it still will be, and presto! You're in an infinite loop.
Was This Post Helpful? 0
  • +
  • -

#7 GWatt  Icon User is online

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: Decrement Operator

Posted 08 October 2008 - 03:45 PM

View Postsmartin, on 8 Oct, 2008 - 05:36 PM, said:

Thank you for responding.
I made the following changes to my function, but now it increments rather than decrement. I don't understand.

{
		int row;
		int col;

		//for (row = 0; row <= down; row--)
		for (row = down; row >= 0; row--)
	{
		//for (col = 0; col <= across; col--)
		for (col = across; col >= 0; col--)
		{



that code will decrement. there is nothing wrong with it. Either you're not posting the code you're using, or you're misdiagnosing the problem.
Was This Post Helpful? 0
  • +
  • -

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Decrement Operator

Posted 08 October 2008 - 03:48 PM

Oops~ my bad. Read the commented loop instead of the actual one.

Use syntax highlighting! :P

[code=cpp]Paste your code here[/code]
Was This Post Helpful? 0
  • +
  • -

#9 smartin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 30-August 08

Re: Decrement Operator

Posted 08 October 2008 - 04:51 PM

View PostGWatt, on 8 Oct, 2008 - 03:45 PM, said:

View Postsmartin, on 8 Oct, 2008 - 05:36 PM, said:

Thank you for responding.
I made the following changes to my function, but now it increments rather than decrement. I don't understand.

{
		int row;
		int col;

		//for (row = 0; row <= down; row--)
		for (row = down; row >= 0; row--)
	{
		//for (col = 0; col <= across; col--)
		for (col = across; col >= 0; col--)
		{



that code will decrement. there is nothing wrong with it. Either you're not posting the code you're using, or you're misdiagnosing the problem.


I pasted it right out of my program. It is incrementing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1