In my Computer Science 221 class we are focusing on C++ and more specifically cl
ID: 3725940 • Letter: I
Question
In my Computer Science 221 class we are focusing on C++ and more specifically classes. I am having trouble finding out what is wrong with his problem, any help would be greatly appreciated!
Question:
1. Write a program that converts a number entered in Roman numerals to a positive integer. Your program should consist of a class, say, romanType. An object of type romanType should do the following:
• Store the number as a Roman numeral.
• Convert and store the number as a positive integer.
• Print the number as a Roman numeral or positive integer as requested by the user.
The integer values of the Roman numerals are:
M 1000
D 500
C 100
L 50
X 10
V 5
I 1
Code:
"romanType.h"
#pragma once
#include <iostream>
#include <string>
using namespace std;
const int numOfNumeral = 7;
class romanType {
public:
void printOutput(char []);
void setNum();
private:
string romanInteger[numOfNumeral];
};
"testingRomanImp.cpp"
#include <iostream>
#include <string>
#include "romanType.h"
#include "testingRomanImp.h"
using namespace std;
int main()
{
romanType numeral;
numeral.setNum();
system("pause");
return 0;
}
"romanTypeImp.cpp"
#include <iostream>
#include <string>
#include "romanType.h"
#pragma once
const int numAmt = 7;
void romanType::printOutput(char romanNum[8])
{
char romanInteger[numAmt] =
{
'M', 'D', 'C',
'L', 'X', 'V', 'I'
};
int intVal[numAmt] = { 1000,500,100,50,10,5,1 };
int length = 0, final = 0, n = 0, m = 0;
int value[10];
for (int i = 0; i <= numAmt; i++)
{
if (romanNum[i] != '0')
{
length++;
}
else {
n = 7;
}
}
for (int i = 0; i < length; i++)
{
for (int j = 0; j < numAmt; j++)
{
if (romanNum[i] == romanInteger[j])
{
value[i] = intVal[j];
}
}
}
for (int x = 0; x < length; x++)
{
if (length > x + 1)
{
if (value[x] >= value[x + 1])
{
final = final + value[x];
}
else
{
final = final + value[x + 1] - value[x];
x++;
}
}
else
{
final = final + value[x];
}
}
cout << "The number is: " << final << endl;
}
void romanType::setNum()
{
char romanNum[8];
int n;
cout << "Please enter Roman Numerals: ";
cin >> romanNum;
cout << endl;
for (int i = 0; i < 8; i++)
{
romanNum[i] = toupper(romanNum[i]);
}
printOutput(romanNum);
}
Explanation / Answer
i have done changes in the romanTypeImp.cpp, now it is working fine.
romanTypeImp.cpp:
#include <iostream>
#include <string>
#include <string.h>
#include "romanType.h"
#pragma once
const int numAmt = 7;
void romanType::printOutput(char romanNum[8])
{
char romanInteger[numAmt] =
{
'M', 'D', 'C',
'L', 'X', 'V', 'I'
};
int intVal[numAmt] = { 1000,500,100,50,10,5,1 };
int length = 0, number = 0, i = 0, j = 0;
length = strlen(romanNum) > 8 ? 8 : strlen(romanNum);
for(i=0; i < length; i++)
{
for( j = 0; j < numAmt; j++)
{
if (romanInteger[j] == romanNum[i])
{
number += intVal[j];
break;
}
}
}
cout << "The number is: " << number << endl;
}
void romanType::setNum()
{
char romanNum[8];
int n;
cout << "Please enter Roman Numerals: ";
cin >> romanNum;
cout << endl;
for (int i = 0; i < 8; i++)
{
romanNum[i] = toupper(romanNum[i]);
}
printOutput(romanNum);
}
Output after running this program:
$ ./a.out
Please enter Roman Numerals: X
The number is: 10
$ ./a.out
Please enter Roman Numerals: mx
The number is: 1010
Please share your feedback and concern
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.