-2

The program is pretty simple. The basic aim of the program is to take a number as input from the user and then store the individual digits of the number in an array. For this my approach is to first convert the input integer number into a string. Then we will iterated through every character of the string and covert the character into a digit and store it in the array. First I take the input of the length of the number. Then I take input of the number. Then I convert that integer number into string and then iterate through every position of the string and store the subsequent digit in the array. code:

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    int m;
    cin>>m;
    string s = to_string(m);
    int arr[n];
    int j=0;
    for(auto d:s){
        arr[j] = d-'0';
        j+=1;
    }
    for(int i=0;i<n;i++) cout<<arr[i];
}

The code works absolutely fine for all numbers which do not begin with '0'. But as soon as I give an input integer like '0135' i get strange array elements like '13532764'. Please help me in finding the problem.

  • 6
    [`int arr[n];`](https://stackoverflow.com/questions/1887097/) is not standard C++, use `std::vector` instead. In any case, you are not checking if `operator>>` is successful before using the `int` value it outputs. And what is the point of reading in `n` before `m`? You have `m` in a `string`, just use the string's `size()` to allocate the `int` array, no `n` is needed. – Remy Lebeau Jul 20 '21 at 20:46
  • 1
    See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). – prapin Jul 20 '21 at 20:48
  • 1
    Ask yourself: In `int arr[n];`, what is the value of `n`? – NathanOliver Jul 20 '21 at 20:49
  • Note: the issue would have been easy to spot stepping through it using a debugger. Even if you don't know how to use one, a few print statements scattered throughout would help. For example: `cout << n << " " << m << " " << s << "\n";` would show that `s` only had 3 chars. – 001 Jul 20 '21 at 21:00
  • 1
    Even putting a space between the digits of `arr[]` would have helped spot the issue, eg: `for(int i = 0; i < n; i++) cout << arr[i] << ' ';` Then you would have seen the output as `1 3 5 32764` – Remy Lebeau Jul 20 '21 at 21:07

3 Answers3

1

When you give something like 0135 as m and 4 as n, arr[n] will contain 4 elements. But your string from std::to_string(135) only contains 3 characters. So, the last element of the arr will not be initialized, it can have any random value, and because of that you may see random digits after your desired number.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
TonySalimi
  • 8,257
  • 4
  • 33
  • 62
0

Leading zeros have no meaning to integers, so when you read in 0135 as an int from user input, it will output an int value of 135. Thus, after converting 135 to a std::string, you end up putting only 3 digits into your 4-digit array, leaving the 4th digit unassigned. Since you are not initializing your array, that 4th digit has a random value, in your case 32764 (0x7FFC). Demo

You don't need n at all. Once you have m in a std::string, you can use the string's size() instead, eg:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){
    int m;
    cin >> m;
    string s = to_string(m);
    vector<int> arr(s.size());
    int j = 0;
    for(auto ch : s){
        arr[j] = ch - '0';
        ++j;
    }
    for(auto d : arr)
        cout << d;
}

Demo

If you want to preserve leading zeros, then don't read in the input as an int, read it in as a std::string instead, and then validate the characters as needed, eg:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){
    string s;
    cin >> s;
    vector<int> arr(s.size());
    int j = 0;
    for(auto ch : s){
        if (ch >= '0' && ch <= '9'){
            arr[j] = d - '0';
            ++j;
        } else {
            ...
        }
    }
    for(int i = 0; i < j; ++i)
        cout << arr[i];
}

Demo

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

The length of the number is determined after this loop

for(auto d:s){
    arr[j] = d-'0';
    j+=1;
}

and equal to the value of the variable j.

So at least in the following loop in its condition you should use the variable j instead of n

for ( int i=0; i < j; i++) cout<<arr[i]; 
               ^^^^^^

But in any case using your approach is error prone because the size of the variable length array arr (that is not a standard C++ feature) can be less than the number of digits in the entered number. Moreover the user can enter a negative number.:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335