Reputation: 31
#include <iostream>
#include <string>
using namespace std;
class rom2dec
{
public: void roman();
int convert();
void print();
void get();
private: int M, D, C, L, X, V, I;
char romanNumeral;
};
void rom2dec::roman()
{
M = 1000;
D = 500;
C = 100;
L = 50;
X = 10;
V = 5;
I = 1;
}
int rom2dec::convert()
{
if(romanNumeral == 'I' || 'i')
{ cout << 1; }
else if(romanNumeral == 'V' || 'v')
{ cout << 5; }
else if(romanNumeral == 'X' || 'x')
{ cout << 10; }
else if(romanNumeral == 'L' || 'l')
{ cout << 50; }
else if(romanNumeral == 'C' || 'c')
{ cout << 100; }
else if(romanNumeral == 'D' || 'd')
{ cout << 500; }
else if (romanNumeral == 'M' || 'm')
{ cout << 1000; }
else if(romanNumeral != 'I' && romanNumeral != 'i' && romanNumeral != 'V' && romanNumeral != 'v' && romanNumeral != 'X' && romanNumeral != 'x' && romanNumeral != 'L' && romanNumeral != 'l' && romanNumeral != 'C' && romanNumeral != 'c' && romanNumeral != 'D' && romanNumeral != 'd' && romanNumeral != 'M' && romanNumeral != 'm')
{ cout << "Error! Not a valid value!" << endl; }
return romanNumeral;
}
void rom2dec::print()
{ cout << romanNumeral << endl; }
void rom2dec::get(){ }
int main()
{
char romanNumeral;
cout << "Please enter a number in Roman numerals to be converted: " << endl;
cin >> romanNumeral;
return 0;
}
I get no errors when I build the program, but when I debug and try to convert a Roman numeral to a decimal I get no decimal. All suggestions welcome and if there is a simpler way to write the last else if please let me know. Thanks.
Upvotes: 1
Views: 990
Reputation: 808
There are many errors in your program:
I have fixed the code:
#include <iostream>
#include <string>
using namespace std;
class rom2dec
{
public:
rom2dec(char x): romanNumeral(x) {};
~rom2dec() {};
void roman();
int convert();
void print();
void get();
private: int M, D, C, L, X, V, I;
char romanNumeral;
};
void rom2dec::roman()
{
M = 1000;
D = 500;
C = 100;
L = 50;
X = 10;
V = 5;
I = 1;
}
int rom2dec::convert()
{
if (romanNumeral == 'I' || romanNumeral == 'i')
{ cout << 1; }
else if (romanNumeral == 'V' || romanNumeral == 'v')
{ cout << 5; }
else if (romanNumeral == 'X' || romanNumeral == 'x')
{ cout << 10; }
else if ((romanNumeral == 'L') || romanNumeral == 'l')
{ cout << 50; }
else if (romanNumeral == 'C' || romanNumeral == 'c')
{ cout << 100; }
else if (romanNumeral == 'D' || romanNumeral == 'd')
{ cout << 500; }
else if (romanNumeral == 'M' || romanNumeral == 'm')
{ cout << 1000; }
else
{ cout << "Error! Not a valid value!" << endl; }
return romanNumeral;
}
void rom2dec::print()
{ cout << romanNumeral << endl; }
void rom2dec::get(){ }
int main()
{
char romanNumeral;
cout << "Please enter a number in Roman numerals to be converted: " << endl;
cin >> romanNumeral;
rom2dec obj(romanNumeral);
obj.convert();
return 0;
}
Upvotes: 1
Reputation: 1736
I gave that a little remake.
instead of a noodle-like if-else-if-else-if
control-statement I used an unordered map to map the lowercase numerals to int (uppercase chars are converted to lowercase before looked up in the map).
the class itself is now an improved singleton, and works as a functor on chars.
there's now an overloaded istream
operator>>
that allows using the class instance as a sink for istreams (std::cin or the stringstream I used to mock user input)
To use that interactively, simply change the #if 0
to #if 1
#include <iostream>
#include <unordered_map>
#include <string>
#include <sstream>
class rom2dec
{
public:
using map_type = std::unordered_map<char,int>;
static rom2dec& instance() {
static rom2dec r2d;
return r2d;
}
int operator() (char romanNumeral) const;
void print(char romanNumeral) const;
void get(); // ?
private:
rom2dec() = default;
~rom2dec() = default;
rom2dec(const rom2dec&) = delete;
rom2dec(rom2dec&&) = delete;
rom2dec& operator=(rom2dec&&) = delete;
rom2dec& operator=(const rom2dec&) = delete;
static map_type map_;
};
rom2dec::map_type rom2dec::map_ = {
{'i', 1},
{'v', 5},
{'x', 10},
{'l', 50},
{'c', 100},
{'d', 500},
{'m', 1000}
};
int rom2dec::operator() (char romanNumeral) const
{
int rv = -1;
auto it = map_.find(std::tolower(romanNumeral));
if (it != map_.end()) {
rv = it->second;
} else {
std::cerr << "Error! '" << romanNumeral
<< "' is not a valid value!"
<< std::endl;
}
return rv;
}
void rom2dec::print(char romanNumeral) const
{
std::cout << romanNumeral << "\n";
}
void rom2dec::get() {}
std::istream& operator>>(std::istream& is, const rom2dec& r2d) {
char romanNumeral;
if (is >> romanNumeral) {
int dec = r2d(romanNumeral);
if (dec > 0)
std::cout << romanNumeral << " = " << dec << "\n";
}
return is;
}
int main()
{
auto& r2d = rom2dec::instance();
#if 0
auto& is = std::cin;
#else
std::stringstream is("C\n+\n+\nM\nM\nX\nI\nV");
#endif
do {
std::cout << "Please enter a single Roman numeral to be converted"
<< "(press <CTRL>+<D> to terminate):\n";
} while (is >> r2d);
return EXIT_SUCCESS;
}
live at Coliru's
Upvotes: 1