{
        int input;

        cout<<"Please select a number from 0 to 127: ";
        cin>>input;

        if (input<127 && input>0)
            MarkNumber(input);

        else
            cout<<"Please enter a number from 0 and 127!"<<endl;
    }

void MarkNumber(int input)
        {
            int loop=0, n=0;

            while (loop!=input)
            {
                cout<<n;
                n++;                //incrementing number from 0
                loop++;           //incrementing counter for loop
            }
            
            cout<<endl<<endl;
        }

I need to use only numbers from 0-9. SO if the user inputs 13, the output should be 01234567890123 (repeating the 0-9) instead of 012345678910111213.

Any help is appreciated!

Dani AI

Generated

The goal in this thread was to print only the least-significant digit of each integer from 0 up to the chosen value (so for 13 the intended sequence is the digits of 0,1,2,...,13 taken modulo 10). Two issues in the original code are an off-by-one in the loop (it stops before the entered value) and the input check input<127 && input>0 which excludes 0 and 127; the correct inclusive test is input >= 0 && input <= 127.

Both suggestions already posted are on the right track: pointed out the idea of separate counters and showed a chunked output approach. A simpler, clearer solution is a single loop that prints i % 10 for each i; this avoids building special-case blocks and naturally handles the repeating 0–9 pattern.

Example implementation (keeps the count inclusive and safe for small limits):

void MarkNumber(int input) {
    if (input < 0) return;
    for (int i = 0; i <= input; ++i)
        std::cout << char('0' + (i % 10));
    std::cout << "\n\n";
}

Notes and troubleshooting tips:

  • Use input >= 0 && input <= 127 if the 0–127 constraint is required.
  • For very large input values, build a std::string with reserve and push_back first, then cout it once for better performance.
  • The char('0' + (i % 10)) trick is safe while i % 10 stays 0–9; avoid negative i values.
  • Confirm whether the original intention was inclusive of the entered number; adjust the loop condition (<= vs <) accordingly.

Recommended Answers

All 2 Replies

Member Avatar for Member #46692

Have two counts.

The main count will count up to the number entered by the user.

The other count will count up in tens. Once ten is reached you can reset this count back to zero and continue.

Modify your MarkNumber as shown below.

void MarkNumber(int input)
        {
            int loop=input ;
            while (loop >= 10)
            {
                cout<<"0123456789";
                loop -= 10;
            }
   int n = input%10;
   for(int k = 0;k<n;k++)
    cout<<k;

            
            cout<<endl<<endl;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.