0

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.

KiuSandy
  • 17
  • 5
  • 4
    You are lucky, your compiler is here to assist you. You only need to enable the warnings and it will readily tell you that `num_of_digit` in `numberOFdigit()` is used uninitialized. – Yksisarvinen Oct 26 '22 at 14:43
  • 3
    You could also add debugging messages. "Print debugging" is one of the oldest tricks in the book. – Raymond Chen Oct 26 '22 at 14:44
  • @Yksisarvinen thanks a lot! You helped me solve this big issue! – KiuSandy Oct 26 '22 at 14:51
  • 3
    @KiuSandy No problem. But seriously, learn to love your compiler, it's your best friend in coding. Enable as many warnings as you can and think of them as errors, it will save you a lot of headache while coding. Learning [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) is also helpful, but compiler is your first line of defence against typos and other small issues. – Yksisarvinen Oct 26 '22 at 14:54
  • 1
    @RaymondChen There is no reason for that any reasonable IDE should come with an interactive debugger. No need to polute code with debug statements. (spedialy not if you also write unit tests). – Pepijn Kramer Oct 26 '22 at 14:56
  • A bug waiting to happen is this : int num_dig[10] (for large inputs). If you use a std::vector you can grow it to fit your needs after the user input. – Pepijn Kramer Oct 26 '22 at 14:57
  • @PepijnKramer Array size should be fine, provided that `int` is 32-bit or smaller on their platform and `numberOFdigit` has correct implementation. – Yksisarvinen Oct 26 '22 at 15:02
  • 2
    @Yksisarvinen Probably yes, but size of int is not standardized either ;) I've learned (the hard way) to not make too many assumptions in my code. And std::vector is a good thing to learn anyway :) – Pepijn Kramer Oct 26 '22 at 15:04
  • @PepijnKramer OP said they were using an online compiler, not an online IDE. It does not look like Programmiz includes a debugger. – Raymond Chen Oct 26 '22 at 15:16
  • Fair enough, this one does though : https://www.onlinegdb.com – Pepijn Kramer Oct 26 '22 at 15:43
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Oct 26 '22 at 17:03
  • @PepijnKramer thanks for your reminder. Found the arrays execute better after standardization. – KiuSandy Oct 28 '22 at 06:52
  • @RaymondChen thanks for your reminder. I used another IDE to write it first, the IDE can run the code well but the online compiler does not. So I did not realize the debugger was needed. – KiuSandy Oct 28 '22 at 06:55

0 Answers0