-4

Below are my codes:

    char str [80];
    int n;

     n = MAX + ( rand () % 1000 + 1);
     cout << "number: " << n << endl;

     constructArray(str, n);



    void constructArray (char str [], int n)
    {
    for (int i = 0; i < n; i++)
    {
        while (n > 0)
        {
           // get last pair of digits
           str [i] = n%10;

            n/= 10;
        }
      cout << str[i] << endl;
    }
    }

I can't figure out why my compiler doesnt output any values. It works if I didnt implement array.

Any help will be appreciated.

Elyssehui
  • 11
  • 3
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Nov 04 '16 at 05:56
  • Are you running into [this issue](http://stackoverflow.com/questions/19562103/uint8-t-cant-be-printed-with-cout/19562163#19562163) finally? – πάντα ῥεῖ Nov 04 '16 at 05:57
  • This question does not show any research effort; It is unclear or not useful – Nitesh Kumar Singh Nov 04 '16 at 06:08

3 Answers3

0

In the first iteration of your for loop, when while is executed for the first time (i=0), n will be reduced to 0. Thus your for loop will execute only once. This will lead to only str[0] being set. Others already pointed out that char cant store large numbers, but this is differnt problem.

Marcin
  • 215,873
  • 14
  • 235
  • 294
0

There are few problems with you code.

  • You are storing integer in char type array.
  • for loop will only be executed only once because while loop make n = 0.

hence only str[0] will be assigned.

Shravan40
  • 8,922
  • 6
  • 28
  • 48
0

In the while loop,the n is still divided by 10 until n is reduced to 0. Then the for loop will finished. So only output str[0].

Jeff
  • 12,555
  • 5
  • 33
  • 60
leak
  • 1