Error: expected primary-expression before 'int'

I am writing a program to convert numbers to Roman numeral.

My code is still in the first stages and im having some problems with my member function call.
I need a clue to set me on the right path for this error.
I get an error when I try to call for Value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "ConvertToRome.h"

using namespace std;



int main()
{
    ConvertToRome Value;
    cout << "Input a number between 1-20 for Numeral conversion(0 to end inquiry)" << endl;
    cin>> Value.convertNum(int i);
    return 0;
}

my (.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef CONVERTTOROME_H
#define CONVERTTOROME_H
#include <string>

class ConvertToRome
{
    public:
        ConvertToRome(){INeum = '0';}
        ConvertToRome(char myNeum){INeum = myNeum;}


        virtual ~ConvertToRome();
    protected:
    private:
        int INum;
        char INeum;
        std::string convertNum(int i);
};

#endif // CONVERTTOROME_H 

my (.cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "ConvertToRome.h"
#include <string>

ConvertToRome::ConvertToRome()
{

}

ConvertToRome::~ConvertToRome()
{
    //dtor
}

std::string ConvertToRome::convertNum(int i)
{
char romeNeum[21];
int i;
romeNeum[0] == "0";
romeNeum[1] == "I"; romeNeum[2] == "II"; romeNeum[3] == "III"; romeNeum[4] == "IV"; romeNeum[5] == "V";
romeNeum[6] == "VI"; romeNeum[7] == "VII"; romeNeum[8] == "VIII"; romeNeum[9] == "IX"; romeNeum[10] == "X";
romeNeum[11] == "XI"; romeNeum[12] == "XII"; romeNeum[13] == "XIII"; romeNeum[14] == "XIV"; romeNeum[15] == "XV";
romeNeum[16] == "XVI"; romeNeum[17] == "XVII"; romeNeum[18] == "XVIII"; romeNeum[19] == "XIX"; romeNeum[20] == "XX";
if(i = 0){ return false;}
return romeNeum[i];

 }
cin>> Value.convertNum(int i);
is wrong.

It should be something along the lines of
12
13
14
int number;
cin >> number;
cout << Value.convertNum(number); // This will print the result 
Hi,

There are some good things already in your code, you have split the declaration & implementation into header and .cpp files which is great. And you have header guards, which is also good news :+) Plus you used code tags, which I am insanely happy about!

Some other points:

I know you are just starting out, a lot of the following might seem too advanced - I will mention it anyway, plenty for you to think about for your future coding.

Don't have using namespace std; , it brings in the entire STL into the global namespace, defeats the purpose of name spaces. Instead put std:: before each STL variable, class, function, contaner, algorithm etc. Like you did with std::string

Choose meaningful variable names: Value, INum, INeum aren't as good as they could be. Don't be afraid to make names longer if it helps with the meaning - for example I might have named convertNum as ConvertDecimalToRoman. That might seem a bit extreme for this small program (might have got away with DecimalToRoman say), but it is a good habit to get into - especially for larger programs. Ideally you want the code to be self documenting - that is the name of the variable or function tells one what is happening. You can use comments to explain expected valid ranges of variables, pre or post conditions, or anything thing else which could be useful, including web addresses for supporting documentation or methodology.

For example you could restrict your input to positive numbers (check that they are) and make the type unsigned int. At the moment your code does a simple look up for the numbers 1 to 20, so you should restrict the input to this, or at least cater for invalid input. What happens if the user inputs 50? You should do this validation before creating the object in main() - do it with a function.

There is a bit of a convention for naming member variables with a preceding m_. It is handy to be to differentiate between member variables and those that are parameters of functions (function arguments).

With your header file, you have some functions defined inline, there is no need to redefine them in the .cpp file.

There is no need for the virtual destructor at this stage. An ordinary destructor will do.

The convertNum function is private so you can't call it directly form an object (Value), so this function should be public

You have convertNum returning a std::string, but on line 23 it returns a bool (true / false)

On line 17, no need to re-declare variable i
On line 24 you return a char, not a string.

char romeNeum[21]; is an array of char, but on lines 18 to 22 you assign them strings. To assign a char use single quotes, but there can only be 1 not any more. So romeNeum should be an array of std::string.

You can assign array values directly with braces (in a .cpp file, or in a .h file with C++11) like this:

const std::string RomanNumeral[] = {"0", "I","II", "III", "IV"} //etc

On line 23 in the if statement you have assignment not comparison. comparison is ==

If a class function doesn't modify any member variables, it can be marked const as well:

std:: string ConvertDecimalToRoman(unsigned int DecimalValue) const;

With const correctness, if it's not going to or shouldn't change make or mark it const

With constructors, you can use an initialiser list to assign values before the body of the function is executed:

1
2
3
4
5
6
ConvertToRome() : // colon introduces initialiser list
       m_DecimalValue(0); {}

ConvertToRome(const unsigned int Decimal) : // colon introduces initialiser list
       m_DecimalValue(Decimal); {} // if more than 1 separate with comma's do in the same order as
                                                     // listed in the class declaration 


One other huge big golden rule is to always initialise your variables with something.

Now with the overall approach of your code. You do a simple look up of the values 1 to 20, is that what was specified in the assignment? There are ways of looking at the units, tens, hundreds, thousands values of your decimal number, convert them to roman and build a string for the entire number.

There you go lots to think about :+D

Hope all goes well, any questions just ask !

Thank you for your help!
If I still need to clean my code up let me know.

(main)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "ConvertToRome.h"


int main()
{
    ConvertToRome Value;
    int Decimal;
    while(Decimal!=0){
    std::cout << "\nInput a number between 1-20 for Numeral conversion(0 to end inquiry)" << std::endl;
    std::cin >> Decimal;
        while(Decimal>20)
            {
                std::cout << "That is not a valid number for this conversion. Try another number." << std::endl;
                std::cin>>Decimal;
            }

    std::cout << Value.ConvertDecimaltoRomanNumeral(Decimal);}
    return 0;
}


(.h)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef CONVERTTOROME_H
#define CONVERTTOROME_H
#include <string>

class ConvertToRome
{
    public:
        ConvertToRome();
        //ConvertToRome(std::string myNeum){NumeralConvert = myNeum;}
        std::string ConvertDecimaltoRomanNumeral(int i);

        virtual ~ConvertToRome();
    protected:
    private:
        int InputNumber;
        //std::string NumeralConvert;

};

#endif // CONVERTTOROME_H 


(.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "ConvertToRome.h"
#include <string>

ConvertToRome::ConvertToRome()
{

}

ConvertToRome::~ConvertToRome()
{
    //dtor
}

std::string ConvertToRome::ConvertDecimaltoRomanNumeral(int InputNumber)
{
const std::string romeNeum[]{
 "END", "Roman: I", "Roman: II", "Roman: III", "Roman: IV", "Roman: V", "Roman: VI", 
 "Roman: VII", "Roman: VIII", "Roman: IX", "Roman: X", "Roman: XI", "Roman: XII", 
 "Roman: XIII", "Roman: XIV", "Roman: XV", "Roman: XVI", "Roman: XVII", 
 "Roman: XVIII", "Roman: XIX", "Roman: XX"};
//if(i = 0){ return false;}
return romeNeum[InputNumber];

 }
Ok things are looking better now, but did you do all the things I mentioned?

Another thought: The array of Roman numerals could be a private member variable, that way other member functions could use it.

Which compiler do you use? Make sure to set the warning level to it's maximum. With g++ I use -std=c++11 -Wall -Wextra -pedantic as a minimum.
Topic archived. No new replies allowed.