3

I know I am probably just doing something dumb wrong, but I need to take a number from the user, create an infinite loop (by making my while statement true) of multiples of 2. I got the math to multiply the number from the user times itself, but I can't get it to loop. This is the last part of my homework for the week and my brain is fried, so I can't figure out where I went wrong!

Any help would be amazing! Here is what I have:

#include <iostream>

using namespace std;

int main (int argc, const char * argv[])
{
    int d;
    int e;
    cin >> d;
    while (true)
    {
        e = d * d;
    }
        cout << e << ", ";
}
4
  • 1
    Do you mean to say "I need to take a number from the user, create an infinite loop displaying multiples of 2" ? Because you definitely got the loop, it's just not showing you anything. Commented Jul 23, 2011 at 5:54
  • 1
    +1 for saying that this is homeowrk. :) Commented Jul 23, 2011 at 5:54
  • 1
    Benjamin - Yes :-) That is exactly what I was trying to say! Commented Jul 23, 2011 at 5:56
  • 1
    Mehrdad - Thank you! I figure complete honesty is the best policy when asking for help! :-) Commented Jul 23, 2011 at 5:57

3 Answers 3

4

There is unreachable code at: cout << e << ", "; Perhaps this was meant to go in the while loop?

You are assigning e the value of d*d over and over. Because d*d does not change, the value of e never changes. Perhaps you should initialize e to the number you want outside of the loop, and then set e = e * 2 inside of the loop, then print e. This will print multiples of your number by successive powers of 2, which is what I think you want.

Sign up to request clarification or add additional context in comments.

7 Comments

Maybe I'm just not skilled enough in C++, but won't e get initialized in the first iteration of the loop?
Oops. I misread that. I thought it read: 'd = e*d'. I'll remove that part. Thanks.
FWIW, it probably should end up reading d = e * d (or something similar.) The code (as is) will loop forever, repeating the same multiplication over and over.
so if it is inside the while loop, then it can't be read from outside of it? I thought I initialized it with e = d * d,but it appears I didn't!
You did initialize it. I made a mistake that I removed.
|
4

As written, your code will loop forever and, as a result, it will never get to that cout statement. Maybe you want to put the cout statement inside of the loop body so that the variable gets printed?

3 Comments

ah yes! I am supposed to make it run forever, just print out the results! Thank you!
I moved the cout inside the loop and it just shows the same number over and over. I need it to read out like "2, 4, 8, 16, 32" etc. I am so close!
@audiedoggie As Chris Lutz mentioned below, you'll need to initialize e outside of the loop and then change your statement to e=e*d. (Or e*=d to be cool)
0

One of the the possible solutions:

int main (int argc, const char * argv[])
{
    int d;
    int e = 2;
    cin >> d;
    while (d>0)
    {
       cout << e << ", ";

       e = e * 2;
       d--;
    }
}

1 Comment

Try not to spoonfeed answers to homework problems. Beyond that, it seems that you misunderstood the question. Your program prints successive powers of two. The original poster wanted to print successive multiples of 2 of a particular number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.