Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++....This is a program that is suppose to read a text document with roman nume

ID: 3841826 • Letter: C

Question

C++....This is a program that is suppose to read a text document with roman numerals and convert them to digits. I am trying to understand how this code works and what each line of code does. I understand the switch for the different roman numerals, can someone explain the other lines of code and what they do?

#include <iostream>
#include <fstream>
#include<cctype>
#include<cstdlib>
#include<string.h>
using namespace std;

int value(char roman)
{
switch(roman)
{
case 'I':return 1;
case 'V':return 5;
case 'X':return 10;
case 'L':return 50;
case 'C':return 100;
case 'D':return 500;
case 'M':return 1000;
default : return -1;
}
}


int get(const string& input)
{
int total=0; char prev='%';
for(int i=(input.length()-1); i>=0; i--)
{
if(value(input[i])==-1)
return -1;
if(value(input[i])<total && (input[i]!=prev))
{ total -= value(input[i]);
prev = input[i];
}
else
{
total += value(input[i]);
prev = input[i];
}
}
return total;
}


int main()
{
char str[255];
int val;
ifstream in("Roman.txt");

if(!in) {
cout << "Cannot open input file. ";
return 1;
}

  

while(in) {
in.getline(str, 255); // delim defaults to ' '
  
if(in)
{
val=get(str);
if(val==-1)
cout << "Invalid character"<<endl;
else
cout << str << " = "<<val<<endl;
  
}
  

}

in.close();

return 0;
}

Explanation / Answer

/* required headers */
#include <iostream>
#include <fstream>
#include<cctype>
#include<cstdlib>
#include<string.h>
using namespace std;


/* which will get roman value as input and return correspoding digits */
int value(char roman)
{
switch(roman)
{
case 'I':return 1;
case 'V':return 5;
case 'X':return 10;
case 'L':return 50;
case 'C':return 100;
case 'D':return 500;
case 'M':return 1000;
default : return -1;
}
}

/* get method which will take single line input and iterates single line which is converted to digits using value() method then it will calculate the total of line */
int get(const string& input)
{
/* inital assignments */
int total=0; char prev='%';
/* iterates the single line with each charector at a time
for(int i=(input.length()-1); i>=0; i--)
{
if(value(input[i])==-1)
return -1;
if(value(input[i])<total && (input[i]!=prev))
{ total -= value(input[i]);
prev = input[i];
}
else
{
total += value(input[i]);
prev = input[i];
}
}
return total;
}

/* program starts here */
int main()
{
/* variable declaration */
char str[255];
int val;
/* The ifstream is a file stream class used for file handling. here we will read the file */
ifstream in("Roman.txt");
/* error handling case */
if(!in) {
cout << "Cannot open input file. ";
return 1;
}
/* until contents in file ends run while loop /*
while(in) {
/* Extracts characters from the stream as unformatted input and stores them into s as a c-string /*
in.getline(str, 255); // delim defaults to ' '

if(in)
{
// sends single line to get method as a parameter
val=get(str);
if(val==-1)
cout << "Invalid character"<<endl;
else
cout << str << " = "<<val<<endl;

}

}
/* close once done with file */
in.close();

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote