Here is the code:
#include <iostream>
#include <iomanip>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//functions
int numberOFdigit (int n){
int num_of_digit;
if (n==0) num_of_digit=1;
else{
while (n>0){
n/=10;
num_of_digit+=1;
}
}
return num_of_digit;
}
int main(int argc, char** argv) {
int number;
cout<<"Input an integer: ";
cin>>number;
int num_of_digit=numberOFdigit (number);
int num_dig[10];
int backup=number;
int number_backup=number;
for (int i=num_of_digit-1;i>=0;--i){
int digit_=backup%10;
num_dig[i] = digit_;
backup/=10;
}
return 0;
}
I want to get the digits of an array. But don't know why I have a segmentation fault in the online c++ online compiler after I used the for loop to extract the digit.
This is the condition in Programmiz's compiler:
Input an integer: 130
Segmentation fault
Could ask how I solve it? Many thanks.