-1

I am a beginner with C++.

I'm trying to create a loop with std::size_t, but I'm not sure if I'm using it correctly. The variable (i) should start at 1 and iterate which i <= 10000. Each time it iterates, i should be multiplied by 10.

Therefore, the values should be 1, 10, 100, 1000, 10000

However, I am getting 10, 110, 1110, 11110

int main()
{
    for (std::size_t i = 1; i <= 10000; i += 1)
    {
        i *= 10;
        std::cout << i << " \n";
    }
}
einpoklum
  • 118,144
  • 57
  • 340
  • 684
Michelle
  • 17
  • 4
  • 8
    The code does `i += 1` and `i *= 10`. – Pete Becker Mar 30 '21 at 18:49
  • In addition to multiplying by 10 after each iteration of the loop you add one, so the values of `i` before each iteration are 1, 11, 111, 1111, .... Btw. the use of `size_t` does not change anything here compared to using another integral type that's sufficiently large. In this case replacing `std::size_t` with `int` or `unsigned long long` would result in the exact same output. – fabian Mar 30 '21 at 18:49
  • 1
    Try taking pen and paper, and imagine yourself being a computer and just write down values of `i` and `i *= 10` on each iteration. Do not need to do 10000 iterations, you will clearly see a point after 3. – SergeyA Mar 30 '21 at 18:50
  • 3
    Consider using a debugger. You will immediately see what is wrong – Philip Stuyck Mar 30 '21 at 19:24
  • I agree if you had a debugger like the one in Visual Studio or even gdb you would have seen the bug in less time than it took to post a question. That is provided you know what the keys are to get the debugger to execute 1 line at a time instead of just running your program to completion. Also you would have to look at the variables at each step. – drescherjm Mar 30 '21 at 20:23

3 Answers3

4

Your loop is multiplying i by 10 before displaying i, so you never see 1 printed out. And then you are also incrementing i by 1 after multiplying it by 10, which you should not be doing at all in this situation.

Lets walk through the logic:

  • on the 1st iteration, i starts at 1, and is multiplied by 10, so 10 is printed, then i is incremented by 1.

  • on the 2nd iteration, i is 11, which is multiplied by 10, so 110 is printed, then i is incremented by 1.

  • on the 3rd iteration, i is 111, which is multiplied by 10, so 1110 is printed, then i is incremented by 1.

And so on.

To fix this, you need to get rid of i += 1 altogether, and move i *= 10 outside of the loop body and into the loop counter, eg:

int main()
{
    for(std::size_t i = 1; i <= 10000; i *= 10)
    {
        std::cout << i << " \n";
    }
}

Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

the values should be 1, 10, 100, 1000, 10000

Then you want something like this:

for (std::size_t i = 1; i <= 10000; i *= 10)
    std::cout << i << " \n";
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • 2
    While the answer is correct, it feels incomplete to me. Adding an explanation for the problem with the original code would help complete this answer. – François Andrieux Mar 30 '21 at 18:52
  • There's already two perfectly fine explanations of the problem in the comments to the question. I merely solved the problem. – Blindy Mar 30 '21 at 18:53
  • 1
    @Blindy Regarding your first comment, the nature of this site means that answers implicitly prompt for criticism. – François Andrieux Mar 30 '21 at 18:53
  • Is it even necessary to write `std::size_t` instead of `size_t`? – Jan Christoph Terasa Mar 30 '21 at 18:55
  • 4
    Comments can vanish at any time. A good answer should be reasonably complete in this case means adding the explanation even though it's present in the comments... – fabian Mar 30 '21 at 18:57
  • @JanChristophTerasa [Difference between size_t and std::size_t](https://stackoverflow.com/questions/5813700/difference-between-size-t-and-stdsize-t) – François Andrieux Mar 30 '21 at 18:57
  • This problem is so trivial that the answer is pretty much self explanatory. – Jan Christoph Terasa Mar 30 '21 at 18:57
  • @Blindy I *am* trying to do them a favor but providing criticism. Because that is how answers are improved. Responding defensively is the disservice. – François Andrieux Mar 30 '21 at 19:00
  • @JanChristophTerasa [Is it wrong to use an upvote to balance out a downvote?](https://meta.stackoverflow.com/questions/311406/is-it-wrong-to-use-an-upvote-to-balance-out-a-downvote) – François Andrieux Mar 30 '21 at 19:05
  • And you seem to be offensive. I still have not, at any point, asked if you believe my answer is flawed or not, and why that would be so. – Blindy Mar 30 '21 at 19:17
  • 2
    @Blindy It seems like you expect users to withhold their comments about the post unless a post author explicitly requests them. That isn't how this site works. It is irrelevant whether or not a post author has asked for feedback, comments or opinions. The whole site is structured around peer review. You can disagree with feedback, but you cannot simply dismiss it for being unprompted. – François Andrieux Mar 30 '21 at 19:24
0

While the other answers correctly explain what your problem is, you could have figured this out without asking here on SO, if you would go through its execution step by step, using a debugger.

Here are two Stackoverflow questions about debugging on Linux and on Windows:

(these are about debugging C programs, but the same tools and environments can be used for debuging C++ programs as well.)

If you were to debug the program, stepping through individual commands, you would notice the sequence of changes which @RemyLebeau describes - yourself.

Also, and for future reference - most StackOverflow users expect question askers to perform their "due diligence", making reasonable efforts to figure out their problems before asking us for a solution. Now that you know about debuggers - please use one before asking "Why does my program not do what I expected".

einpoklum
  • 118,144
  • 57
  • 340
  • 684