How to get numbers in ascending order?

Hello!

I have to write this program that gives a certain amount of cubes that the user requests. I've got the program all set up so that it allows the user to put in how many cubes they want, and it shows the cubes all proper and whatnot; however, the numbers themselves are displayed in descending order rather than ascending order (i.e. if I say I want 3 cubes displayed, it'll print out as
"3 cubed is 9
2 cubed is 4
1 cubed is 1")

We are not very far into our textbook, and I've tried to scour all my notes and slides and stuff, but I've yet to really find anything that'll help. I don't want anyone to just say "put this in" (apologies if it sounds like that!). I would like for someone to explain where I need to look for my answer, as it is entirely possible that I've been looking in the completely wrong area.

Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
using namespace std;

int main ( )
{
    int x, cube;
    
    cout<<"How many cubes do you want displayed? ";
    cin>>x;

    while (x > 0)
    {
        cube = x * x * x;
        cout<<x<<" cubed is "<<cube<<endl;
        x = x - 1;

    }
    cout<<endl;

    return 0;
}
closed account (iw7Gy60M)
How about in your loop, instead of starting at x and counting down to 1, you start at 1 and count up to x? Try using a 'for' loop instead of a 'while' loop.
There is more explanation about various loops (although many examples are counting down):
http://www.cplusplus.com/doc/tutorial/control/


You do have a variable (x) that is changing within each iteration, and a constant limit (0) in the condition that does not change.

If you had a second variable (y), then one of the two could hold the limit given by the user and the other would change within iterations.
I took a look at the link, threw in a "y", and changed

while ( x > 0 )

to

for (int y = 1; x > 0; y++)

but it still doesn't work. I also tried putting y = 0, x = 1, x++, etc. but they still didn't do what I wanted to do. Is there anything else I could try?
What doesn't work?
Your first program seems to be working.
closed account (E0p9LyTq)
Using a for loop to output in ascending order:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main ()
{
    std::cout<<"How many cubes do you want displayed? ";
    int x;
    std::cin >> x;
    std::cout << "\n";

    for (int loop = 1; loop <= x; loop++)
    {
        int cube = loop * loop * loop;
        std::cout << loop << " cubed is " << cube << "\n";
    }
}

How many cubes do you want displayed? 5

1 cubed is 1
2 cubed is 8
3 cubed is 27
4 cubed is 64
5 cubed is 125
I placed the comments with an explanation on the for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
    int x{ 0 };
    int cube{ 0 };

    cout << "How many cubes do you want displayed? : ";
    cin >> x; // Set the limit for the for loop.

    // Use the for loop with a counter to begin 1.
    // You can begin with zero if you want to, as well.
    // The second parameter of the loop will loop
    // from 1 up to equal to the value you set up
    // as the limit of the for loop.
    // The third parameter will increment the count
    // until it reaches the limit of the loop.
    for (int y = 1; y <= x; y++)
    {
	   cube = y * y * y;
	   cout << y << " cubed is " << cube << endl;
    }

    cout << endl;

    return 0;
}


FurryGuy beat me to it :-) I hope you get the concept, though.
Last edited on
Learn more about the for-loop :
http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

Topic archived. No new replies allowed.