-2

I have a C++ code that asks for a number to work with. I want to use the loop for with the length of this number, such as what we are used to do in JavaScript using .length property.

For example:

the input is: 2563

int input;
for (i = 0; i < input.length; i++)

It is supposed to repeat 4 times.

How could I do it?

user3386109
  • 34,287
  • 7
  • 49
  • 68
KimiH
  • 39
  • 6
  • 3
    Does this answer your question? [C++ - how to find the length of an integer](https://stackoverflow.com/questions/22648978/c-how-to-find-the-length-of-an-integer) – alex01011 May 22 '21 at 23:58
  • 1
    Does this answer your question? [How to count amount of digits in a given number in c++](https://stackoverflow.com/questions/4624490/how-to-count-amount-of-digits-in-a-given-number-in-c) Also https://stackoverflow.com/q/24562607/62576 Or many more you can find with a search for `[c++] count digits in number` here. – Ken White May 22 '21 at 23:59
  • 1
    Do you always receive this input as text? (such as from the command line or from a file?) – JohnFilleau May 23 '21 at 00:02
  • @JohnFilleau yes, it is from console. The user is supposed to type a number which is read by `scanf`. – KimiH May 23 '21 at 00:12
  • 1
    My answer uses `cin`, not `scanf`. Do you have to use the old C-style functions for user input? – JohnFilleau May 23 '21 at 00:15
  • @JohnFilleau Not actually. I can try this one. Thanks! – KimiH May 23 '21 at 00:21
  • @KenWhite I tried the `floor(log10(num))+1` in the loop: `for(int i = 0; i < (floor(log10(num))+1); i++)` but it didn't work. – KimiH May 23 '21 at 00:22
  • @Lisandra when something doesn't work, consider explaining what didn't work about it. – JohnFilleau May 23 '21 at 00:54

1 Answers1

0

If you always receive the number via some text input (command line or a text file) then there's no reason to do modulo arithmetic. If your loop is even tightly coupled to the number of symbols received, it makes even less sense to operate on the integer. Instead, receive the input as a string.

#include <iostream>
#include <string>

int main() {
    std::string input;
    std::cout << "Enter a positive integer: ";
    std::getline(std::cin, input);

    for (int i = 0; i < input.length(); i++) {
        std::cout << "Digit number " << i << " is " << input[i] << std::endl;
    }
}

NOTE input[i] is a character, and any arithmetic operations on that character will not work the same way you expect it to on the integer's individual digits. If you want the number to work with numerically (instead of just symbolically) you can convert to an integer using the std::stoi family of functions ("string to integer").

#include <iostream>
#include <string>

int main() {
    std::string input;
    std::cout << "Enter a positive integer: ";
    std::getline(std::cin, input);

    for (int i = 0; i < input.length(); i++) {
        std::cout << "Digit number " << i << " is " << input[i] << std::endl;
    }

    int value = std::stoi(input);
    std::cout << "The number times 2 is " << value * 2 << std::endl;
}

NOTE The std::stoi family of functions provide error checking functionality. My above example omits that for simplicity, and because this sounds like a school assignment where input validation is the user's problem.

JohnFilleau
  • 4,045
  • 1
  • 15
  • 22