-1

I'm trying to write a program that converts Roman Number to integer but its compilation output the following error:

Line 65: Char 5: error: conflicting types for 'main'
int main(int argc, char *argv[]) {
    ^
Line 47: Char 5: note: previous definition is here
int main()
    ^
1 error generated.

Here there is some of my code:

class Solution {
public:
    int value(char r){
        if (r == 'I')
            return 1;
        if (r == 'V')
            return 5;
        if (r == 'X')
            return 10;
        if (r == 'L')
            return 50;
        if (r == 'C')
            return 100;
        if (r == 'D')
            return 500;
        if (r == 'M')
            return 1000;
 
        return -1;
    }
    int romanToInt(string& s) {
        int ret = 0;

        for (int i = 0; i < s.length(); i++) {
            int s1 = value(s[i]);

            if (i + 1 < s.length()) {
                int s2 = value(s[i + 1]);

                if (s1 >= s2) {
                    ret = ret + s1;
                }
                else {
                    ret = ret + s2 - s1;
                    i++;
                }
            }
            else {
                ret = ret + s1;
            }
        }
        return ret; 
    }

};

int main()
{
    Solution m;

    string str = "III";
    cout << "Integer form of Roman Numeral is " << m.romanToInt(str) << endl; 
    return 0;
}

The method value() reads the string with the Roman Number letter by letter and recognizing the value of each letter.
I think that the main() function needs some change in order to do this task but I am a little stuck on how to do so.

cafce25
  • 15,907
  • 4
  • 25
  • 31

2 Answers2

1

You have probably defined the function int main() twice. Considering that the error message

Line 65: Char 5: error: conflicting types for 'main'
int main(int argc, char *argv[]) {
    ^
Line 47: Char 5: note: previous definition is here
int main()
    ^
1 error generated.

says that there is an error at line 65, whereas your code is less than 60 lines long, I would assume that there is more code than what was copied here.

Mikael Jagan
  • 9,012
  • 2
  • 17
  • 48
Oreoezi
  • 129
  • 9
0

The main() function definition and your error message

Your error message says that there are two conflicting definition of the main() function. For example if you declare a main() function at the top of the file as following:

int main(int argc, char *argv[]);

but your main() function definition is:

int main()
{
    Solution m;

    string str = "III";
    cout << "Integer form of Roman Numeral is " << m.romanToInt(str) << endl; 
    return 0;
}

The compilation outputs an error analog to yours because your definition is missing arguments int argc, char *argv[].

Correct code

Your code is correct and I have made only the following modifications to it:

  • added the insert of the string str variable by keyboard (by the instruction std::cin) to simplify the test
  • at the top of the file I have inserted a correct declaration of the function main()
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
// I have also added the following #include to your code
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <iostream>
#include <string>
using namespace std;

// correct declaration of main() 
int main();

/*+++++++++++++++++++++++++++++++++++++++
Your code for Class Solution is correct
+++++++++++++++++++++++++++++++++++++++*/
class Solution {
public:
    int value(char r){
        if (r == 'I')
            return 1;
        if (r == 'V')
            return 5;
        if (r == 'X')
            return 10;
        if (r == 'L')
            return 50;
        if (r == 'C')
            return 100;
        if (r == 'D')
            return 500;
        if (r == 'M')
            return 1000;

        return -1;
    }
    int romanToInt(string& s) {
        int ret = 0;

        for (int i = 0; i < s.length(); i++) {
            int s1 = value(s[i]);

            if (i + 1 < s.length()) {
                int s2 = value(s[i + 1]);

                if (s1 >= s2) {
                    ret = ret + s1;
                }
                else {
                    ret = ret + s2 - s1;
                    i++;
                }
            }
            else {
                ret = ret + s1;
            }
        }
        return ret;
    }

};


int main()
{
    Solution m;
    string str;

    cout << "Insert a Roman Number: ";
    cin >> str;
    cout << "Integer form of Roman Numeral is " << m.romanToInt(str) << endl;

    return 0;
}

How to compile and execute the program in a Linux system

To compile the previous program in a Linux system (with g++ compiler installed) save it in a file called solution.cpp, change the folder to go in that which contains the file and compile it:

> cd /path/to/file-source
> g++ solution.cpp -o solution

To execute the program use:

> ./solution

The output of the code execution is:

Insert a Roman Number: III
Integer form of Roman Numeral is 3
frankfalse
  • 1,553
  • 1
  • 4
  • 17