-1

I am getting segmentation fault to this code to convert roman numerals to numbers, i am getting segmentation error when i try the program in https://www.programiz.com/cpp-programming/online-compiler/ and i found that the inside of the loop A is never executed in my case

#include<iostream>
#include<conio.h>
#include<cstring>
using namespace std;

char str[50];
int a[50],m=50,val=0;

int intit(){
    m=strlen(str);

    for(int i=0;i<m;i++){//loop A
        cout<<i;
        if(str[i]=='M'){a[i]=1000;}
        if(str[i]=='D'){a[i]=500;}
        if(str[i]=='C'){a[i]=100;}
        if(str[i]=='L'){a[i]=50;}
        if(str[i]=='X'){a[i]=10;}
        if(str[i]=='V'){a[i]=5;}
        if(str[i]=='I'){a[i]=1;}
    }
    return 0;
}

int main(){
    char str[50];
    int a[50],m,val=0;
    cin>>str;
    cout<<"exit val" +char(intit());
    for(int i=m-1;i>=0;i--){ //loop B
        cout<<"inside evaluation loop"<<a[i];
        if(a[i+1]>a[i]){val = val - a[i];}
        else{val = val + a[i];}
    }
    cout<<"\n\n\nans:"<<val;
    getch();
}
  • 1
    `char str[50];` is defined twice. The one that your `intit` function sees is the one defined globally. The one your `main` function sees is the one defined in main. Same with your other global variables. – paddy Aug 03 '22 at 03:40
  • 2
    You don't seem to realize that your global variables are different variables than the local variables of the same name in main(). – Avi Berger Aug 03 '22 at 03:41
  • You have defined variables `str`, `a`, `m`, and `val` as global, and defined another set of them (unrelated to the globals, since they are in different scope) in `main()`. Since `intit()` accesses the globals, nothing it does affects the variables in `main()`. The simplest solution to that is to remove the declarations from `main()` - since that then causes `main()` to do things to the globals. I haven't checked your code further, so there may be other problems as well, but getting the functions to work with the same data will be a start. – Peter Aug 03 '22 at 03:48
  • Im so sorry, i had the function inside main() previously and totally forgot about that another declaration in main(). Thank you so much. – kirupashankar raman Aug 03 '22 at 03:49
  • Also, s is not initialized to zero, so you might run into garbage values. – Joseph Larson Aug 03 '22 at 03:50
  • ITS WORKING!, i removed the variable declaration inside the main(), its working for my inputs, have to check with ohter possible inputs. thank you so much – kirupashankar raman Aug 03 '22 at 03:52
  • You might want to look into supplying the data to `intit()` as arguments, rather than relying on globals. Globals will work in simple cases like this one, but they also come with a lot of constraints or "gotcha"s in practice - particularly if you start writing less simple code - so it is usually better to avoid globals where possible. – Peter Aug 03 '22 at 03:57

1 Answers1

0

You use global and local variable with the same name. remove one of them and the program work correctly. However it is better to send your parameter by reference than define global parameter:

int intit(char (& str)[50], int (&a)[50],int &m ,int &val ) {
    m = strlen(str);

    for (int i = 0;i < m;i++) {//loop A
        cout << i;
        if (str[i] == 'M') { a[i] = 1000; }
        if (str[i] == 'D') { a[i] = 500; }
        if (str[i] == 'C') { a[i] = 100; }
        if (str[i] == 'L') { a[i] = 50; }
        if (str[i] == 'X') { a[i] = 10; }
        if (str[i] == 'V') { a[i] = 5; }
        if (str[i] == 'I') { a[i] = 1; }
    }
    return 0;
}

int main() {
    
    char str[50];
    int a[50], m = 50, val =0;
    cin >> str;
    cout << "exit val" + char(intit(str,a,m,val));
    for (int i = m - 1;i >= 0;i--) { //loop B
        cout << "inside evaluation loop" << a[i];
        if (a[i + 1] > a[i]) { val = val - a[i]; }
        else { val = val + a[i]; }
    }
    cout << "\n\n\nans:" << val;
    
}
  • I made some changes but i still need your help, i moved the "evaluation loop" and "getting the string from the user" to the intit(). The program works fine when i leave the variable initialization as global, but when i change it to local im getting wrong errors – kirupashankar raman Aug 10 '22 at 06:33
  • @kirupashankarraman Try to initialize arrays in the function like this `char str[50] = {""}; int a[50] = {0}` . – Mohammadreza Hojatoleslami Aug 10 '22 at 11:38