Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,163 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,702 people online right now. Registration is fast and FREE... Join Now!




Decrement Operator

 
Reply to this topicStart new topic

Decrement Operator, This program won't work with the decrement operator

smartin
post 7 Oct, 2008 - 05:35 PM
Post #1


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 18


My Contributions


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?
CODE



#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;
}
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 7 Oct, 2008 - 05:44 PM
Post #2


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


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.
User is online!Profile CardPM

Go to the top of the page

demiserealized
post 7 Oct, 2008 - 05:47 PM
Post #3


New D.I.C Head

*
Joined: 11 Sep, 2008
Posts: 6

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.
User is offlineProfile CardPM

Go to the top of the page

smartin
post 8 Oct, 2008 - 01:36 PM
Post #4


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 18


My Contributions


QUOTE(GWatt @ 7 Oct, 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.


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

CODE
{
        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--)
        {

User is offlineProfile CardPM

Go to the top of the page

smartin
post 8 Oct, 2008 - 01:46 PM
Post #5


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 18


My Contributions


QUOTE(demiserealized @ 7 Oct, 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.

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?
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 8 Oct, 2008 - 01:51 PM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 8 Oct, 2008 - 02:45 PM
Post #7


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


QUOTE(smartin @ 8 Oct, 2008 - 05:36 PM) *

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

CODE
{
        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.
User is online!Profile CardPM

Go to the top of the page

gabehabe
post 8 Oct, 2008 - 02:48 PM
Post #8


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,402



Thanked 94 times

Dream Kudos: 2625

Expert In: Dingleberries

My Contributions


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

Use syntax highlighting! tongue.gif

[code=cpp]Paste your code here[/code]
User is offlineProfile CardPM

Go to the top of the page

smartin
post 8 Oct, 2008 - 03:51 PM
Post #9


New D.I.C Head

*
Joined: 30 Aug, 2008
Posts: 18


My Contributions


QUOTE(GWatt @ 8 Oct, 2008 - 03:45 PM) *

QUOTE(smartin @ 8 Oct, 2008 - 05:36 PM) *

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

CODE
{
        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.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 02:25PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month