for class I have to create a code so that it could ask the user for an integer between 1-99 and be able to print that integer in roman numerals. Issue is after creating my code it would only print the numbers fully up to 39. Once it hits 40 it give's no Roman Numeral output and then from 41-99 it wont print the tenth place value(Ex. 45 will come out as V). Here's my code at the moment.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num;
int tenum;
int remnum;
string romnum;
string fromnum;
cout << "Enter a number between 1 and 99. \n";
cin >> num;
tenum = num / 10;
remnum = num % 10;
if (tenum == 9)
{
fromnum == "XC";
}
else if (tenum == 8)
{
fromnum == "LXXX";
}
else if (tenum == 7)
{
fromnum == "LXX";
}
else if (tenum == 6)
{
fromnum == "LX";
}
else if (tenum == 5)
{
fromnum == "L";
}
else if (tenum == 4)
{
fromnum == "XL";
}
else if (tenum == 3)
{
fromnum = "XXX";
}
else if (tenum == 2)
{
fromnum == "XX";
}
else if (tenum == 1)
{
fromnum == "X";
}
if (remnum == 9)
{
romnum = "IX";
}
else if (remnum == 8)
{
romnum = "VIII";
}
else if (remnum == 7)
{
romnum = "VII";
}
else if (remnum == 6)
{
romnum = "VI";
}
else if (remnum == 5)
{
romnum = "V";
}
else if (remnum == 4)
{
romnum = "IV";
}
else if (remnum == 3)
{
romnum = "III";
}
else if (remnum == 2)
{
romnum = "II";
}
else if (remnum == 1)
{
romnum = "I";
}
cout << tenum << endl;
cout << remnum << endl;
cout << fromnum;
cout << romnum << endl;
return 0;
}
You've mixed up == and =
change lines like
fromnum == "XL"
to
fromnum = "XL"
Demo
(I used a switch statement for brevity)
same answer with: Converting integer to Roman Numeral
#include <iostream>
#include <string>
std::string to_roman(int value)
{
struct romandata_t { int value; char const* numeral; };
static romandata_t const romandata[] =
{ 1000, "M",
900, "CM",
500, "D",
400, "CD",
100, "C",
90, "XC",
50, "L",
40, "XL",
10, "X",
9, "IX",
5, "V",
4, "IV",
1, "I",
0, NULL }; // end marker
std::string result;
for (romandata_t const* current = romandata; current->value > 0; ++current)
{
while (value >= current->value)
{
result += current->numeral;
value -= current->value;
}
}
return result;
}
int main()
{
for (int i = 1; i <= 4000; ++i)
{
std::cout << to_roman(i) << std::endl;
}
}
this code converts up to 1000. just take what you need and you are good to go!
from http://rosettacode.org/wiki/Roman_numerals/Encode#C.2B.2B
Related
I am writing a C++ program that converts between Arabic and Roman numbering systems. I wrote one program that converts Arabic to Roman and have another program that converts Roman to Arabic.
The problem is that I can't figure out how to merge them into one single program so that the user can input an Arabic or Roman number and as a result, the program would convert said number to the other.
My question is HOW can I merge these two programs into one?
Code for Roman to Arabic
#include <iostream>
using namespace std;
int main()
{
char roman_Numeral;
int arabic_Numeral = 0;
cout << "Enter the Roman Numeral in Capital letters (e.g. CCXIX) : ";
while (cin.get(roman_Numeral))
{
if (arabic_Numeral > 100)
{
cout << "\nInvalid Value. Number must be between I and C" << endl;
return 0;
}
else if (roman_Numeral == 'C')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D')
{
arabic_Numeral = arabic_Numeral - 100;
}
else
{
arabic_Numeral = arabic_Numeral + 100;
}
}
else if (roman_Numeral == 'L')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C')
{
arabic_Numeral = arabic_Numeral - 50;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 50;
continue;
}
}
else if (roman_Numeral == 'X')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C' || roman_Numeral == 'L')
{
arabic_Numeral = arabic_Numeral - 10;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 10;
continue;
}
}
else if (roman_Numeral == 'V')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C' || roman_Numeral == 'L'
|| roman_Numeral == 'X')
{
arabic_Numeral = arabic_Numeral - 5;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 5;
continue;
}
}
else if (roman_Numeral == 'I')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C' || roman_Numeral == 'L'
|| roman_Numeral == 'X' || roman_Numeral == 'V')
{
arabic_Numeral = arabic_Numeral - 1;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 1;
continue;
}
}
else
break;
}
cout << arabic_Numeral << endl;
return 0;
}
Code for Arabic to Roman
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#define MAX_INPUT 100 // These constants hold high and low integer numbers,
#define MIN_INPUT 1
#define ARRAY_SIZE 4 // and the array size declarator.
string convert(int digit, string low, string mid, string high);
int main()
{
string answers[ARRAY_SIZE] = { "", "", "", "" }; //An array of string to hold the output from the convert function.
int accumulator = 0; // Variable to hold number of arabic numbers converted.
int userNum = 0;
string strUserNum;
do
{
cout << "";
cout << "Enter an arabic number between 1 and 100: ";
cin >> strUserNum;
userNum = std::stoi(strUserNum);
if (userNum == 0 || userNum > MAX_INPUT)
{
cout << "\nInvalid Value. Number must be between 1 and 100" << endl;
return 0;
}
else if (userNum == 'end')
{
cout << "Exiting program:";
break;
}
int thous = userNum / 1000;
int hund = userNum % 1000 / 100;
int tens = userNum % 100 / 10;
int ones = userNum % 10 / 1;
answers[0] = convert(thous, "M", "M", "M");
answers[1] = convert(hund, "C", "D", "M");
answers[2] = convert(tens, "X", "L", "C");
answers[3] = convert(ones, "I", "V", "X");
cout << answers[0] << answers[1] << answers[2];
cout << answers[3] << endl;
cout << endl;
break;
} while (userNum != 'end');
system("PAUSE");
return 0;
}
string convert(int digit, string low, string mid, string high)
{
if (digit == 1)
{
return low;
}
if (digit == 2)
{
return low + low;
}
if (digit == 3)
{
return low + low + low;
}
if (digit == 4)
{
return low + mid;
}
if (digit == 5)
{
return mid;
}
if (digit == 6)
{
return mid + low;
}
if (digit == 7)
{
return mid + low + low;
}
if (digit == 8)
{
return mid + low + low + low;
}
if (digit == 9)
{
return low + high;
}
if (digit == 0)
{
return "";
}
}
Put code (that inside the main block) for Roman to Arabic into void procedureRomanToArabic()
The same for Roman to Arabic. Put it into void procedureArabicToRoman().
Remove remove ArabicToRoman do-while and move it to main.
void procedureRomanToArabic(){
char roman_Numeral;
int arabic_Numeral = 0;
//...
cout << arabic_Numeral << endl;
}
void procedureArabicToRoman()
{
string answers[ARRAY_SIZE] = { "", "", "", "" }; //An array of string to hold the output from the convert function.
int accumulator = 0; // Variable to hold number of arabic numbers converted.
int userNum = 0;
string strUserNum;
//remove do while here
cout << "";
cout << "Enter an arabic number between 1 and 100: ";
cin >> strUserNum;
//...
answers[2] = convert(tens, "X", "L", "C");
answers[3] = convert(ones, "I", "V", "X");
cout << answers[0] << answers[1] << answers[2];
cout << answers[3] << endl;
cout << endl;
}
int main(){
char type;
do{
cout<<"2 for Arabic to Roman, 1 for Roman to Arabic, or anything else for exit";
cin>>type;
if(type=='1')
procedureArabicToRoman();
else if(type =='2')
procedureRomanToArabic();
else break; //end program
} while(true);
}
I am currently developing some code that is used to convert an inputted integer to ROMAN numerals. It is working how it should be except for the cases where I am using the character I, it isn't adding them up correctly. I have done the same syntax as the other characters which work so I am baffled as to why it doesn't work for the "I" character. Here is a snippet of what I have:
else if(roman_Numeral == 'V')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 2) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 5;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 5;
continue;
}
}
else if(roman_Numeral == 'I')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral), 2 != std::string::npos)
{
arabic_Numeral = arabic_Numeral -1;
break;
}
else
{
arabic_Numeral += 1 ;
continue;
}
}
else
break;
}
cout << arabic_Numeral << endl;
return 0;
The other characters, such as M, C, D etc are similar in layout, however with I obviously the desired output for something such as VI is 6 and it outputs 4. Any help would be appreciated.
The complete code is:
#include <string>
#include <iostream>
using namespace std;
std::string numerals = "IVXLCDM";
int main()
{
char roman_Numeral;
int arabic_Numeral = 0;
cout << "Enter the Roman Numeral in Capital letters (e.g. CCXIX) : ";
while(cin.get(roman_Numeral))
{
if(roman_Numeral == 'M')
arabic_Numeral = arabic_Numeral + 1000;
else if(roman_Numeral == 'D')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 5) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 500;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 500;
continue;
}
}
else if(roman_Numeral == 'C')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 4) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 100;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 100;
continue;
}
}
else if(roman_Numeral == 'L')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 3) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 50;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 50;
continue;
}
}
else if(roman_Numeral == 'X')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 2) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 10;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 10;
continue;
}
}
else if(roman_Numeral == 'V')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 2) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 5;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 5;
continue;
}
}
else if(roman_Numeral == 'I')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral), 2 != std::string::npos)
{
arabic_Numeral = arabic_Numeral -1;
break;
}
else
{
arabic_Numeral += 1 ;
continue;
}
}
else
break;
}
cout << arabic_Numeral << endl;
return 0;
}
I initially found the code online and will be making additional changes later on. Currently the most of the test combinations of my code works. It's just implementing the I's so I can get test for singular cases of I such as III = 3, etc. III currently outputs -1 which is obviosuly not correct. I believe the issue is soemthing to do with the way it adds them together but not entirely sure. I am fairly new to coding after all.
Kind regards.
Your observation is correct. The code indeed has a bug for the 'I' section. Just read the code carefully and try to compare it with other working sections. Hint: missing parenthesis ;)
this is my first post in stackoverflow.
I write a program that should take a bmp file in input, and black and white it and then write it into the out.bmp.
when I started to write the code, i delete the bmp file format at the end of the name of input and then open it with text editor, then write the code, and the output style is like the input.
when I type ./a.out <in.bmp>out.bmp in terminal, I get an Abort error (Aborted (core dumped)) and when I give the ./a.out < in > out.bmp gimp say to me it is not a bmp file.
here is the code:
// In the Name of God
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <math.h>
#include <sstream>
using namespace std;
bool TypeIsTrue (string type){
if (type == "424d")
return true;
return false;
}
int GrayIt (int b , int g , int r){
return (b + g + r) / 3 ;
}
int ConvertToDec(string hex){
long int dec = 0;
reverse(hex.begin(), hex.end());
for (int i = 0 ; i <hex.size();i++){
if (hex[i] == 'a')
dec = dec + 10 *(pow(16,i));
else if (hex[i] == 'b')
dec = dec + 11 *(pow(16,i));
else if (hex[i] == 'c')
dec = dec + 12 *(pow(16,i));
else if (hex[i] == 'd')
dec = dec + 13 *(pow(16,i));
else if (hex[i] == 'e')
dec = dec + 14 *(pow(16,i));
else if (hex[i] == 'f')
dec = dec + 15 *(pow(16,i));
else
dec = dec + ((hex[i] - '0')*(pow(16,i)));
}
return dec;
}
string ConvertToHex(int dec){
string hex;
int reminded,Divided;
reminded = dec % 16 ;
Divided = dec / 16;
if (Divided == 10){
hex = "a";
}
else if (Divided == 11){
hex = "b";
}
else if (Divided == 12){
hex = "c";
}
else if (Divided == 13){
hex = "d";
}
else if (Divided == 14){
hex = "e";
}
else if (Divided == 15){
hex = "f";
}
else if (Divided == 0){
hex = "0";
}
else if (Divided == 1){
hex = "1";
}
else if (Divided == 2){
hex = "2";
}
else if (Divided == 3){
hex = "3";
}
else if (Divided == 4){
hex = "4";
}
else if (Divided == 5){
hex = "5";
}
else if (Divided == 6){
hex = "6";
}
else if (Divided == 7){
hex = "7";
}
else if (Divided == 8){
hex = "8";
}
else if (Divided == 9){
hex = "9";
}
if (reminded == 10){
hex = hex+"a";
}
else if (reminded == 11){
hex = hex+"b";
}
else if (reminded == 12){
hex = hex+"c";
}
else if (reminded == 13){
hex = hex+"d";
}
else if (reminded == 14){
hex = hex+"e";
}
else if (reminded == 15){
hex = hex+"f";
}
else if (reminded == 0){
hex = hex+"0";
}
else if (reminded == 1){
hex = hex+"1";
}
else if (reminded == 2){
hex = hex+"2";
}
else if (reminded == 3){
hex = hex+"3";
}
else if (reminded == 4){
hex = hex+"4";
}
else if (reminded == 5){
hex = hex+"5";
}
else if (reminded == 6){
hex = hex+"6";
}
else if (reminded == 7){
hex = hex+"7";
}
else if (reminded == 8){
hex = hex+"8";
}
else if (reminded == 9){
hex = hex+"9";
}
return hex;
}
int main (){
vector <string> a;
vector <string> r;
vector <string> g;
vector <string> b;
vector <string> out;
string temp;
int red,green,blue;
while(cin >> temp){
a.push_back (temp);
}
if(!TypeIsTrue(a[0])){
cout<<"The file is not bmp\nRerun program"<<endl;
abort();
}
int phase = 1;
for (int i = 27 ; i < a.size(); i++){ //int i = 27
string first;
string last;
first = a[i].substr(0,2);
last = a[i].substr(2,3);
if(phase == 4)
phase = 1;
if(phase == 1){
b.push_back(first);
g.push_back(last);
phase ++;
// cout<<"push_backed"<<endl;
}
else if(phase == 2){
r.push_back(first);
b.push_back(last);
phase ++;
// cout<<"push_backed"<<endl;
}
else if(phase == 3){
g.push_back(first);
r.push_back(last);
phase ++;
// cout<<"push_backed"<<endl;
}
}
for (int i = 0 ; i <27 ; i++){
out.push_back(a[i]);
}
for(int i = 27 ; i<b.size() ; i++){
blue = ConvertToDec(b[i]);
green = ConvertToDec(g[i]);
red = ConvertToDec(r[i]);
out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
}
int j = 1 ;
for (int i = 0 ; i < 27 ; i++){
cout<< out[i] << " ";
if (j == 8){
cout<<endl;
j = 0;
}
j++;
}
j=1;
bool space = false;
for (int i = 27 ; i < out.size(); i++){
if( i == 27 + 10){
cout<<endl;
j = 1;
}
cout<<out[i];
if (space)
cout<<" ";
j++;
if(j == 17){
cout<<endl;
j = 1 ;
}
space=!space;
}
return 0;
}
You're getting an abort error because you asked for one.
if(!TypeIsTrue(a[0])){
cout<<"The file is not bmp\nRerun program"<<endl;
abort();
}
If your program is designed to have its output redirected, it is very important to send error messages to stderr (and std::cerr or std::clog) and not stdout.
BTW, the type test is failing because BMP files are not text, it makes no sense to read them using cin >> variable.
In addition, there's no guarantee that a[0] even exists. You need to test a.size() first.
if(a.size() < 1 || !TypeIsTrue(a[0])){
cerr << "The file is not bmp\nRerun program\n";
abort();
}
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>
using namespace std;
int getTreeNo(int);
int getSpeciesCode(int);
float getDbh(float);
int getTotHt(int);
int getTotHt(int);
double calcTotVol(double[], double[], float, int);
int main() {
int i = 0;
int treeNo = 0;
int speciesCode = 0;
string speciesDesc[6] = {};
float dbh = 0.00;
int totalHt = 0;
double totalVol = 0.00;
int Species[6] = {};
double b0[6] = {};
double b1[6] = {};
int noTrees = 0;
treeNo = getTreeNo(treeNo);
/* Gets number of trees for calculations*/
speciesCode = getSpeciesCode(speciesCode);
/* Get Species code from user*/
dbh = getDbh(dbh);
/* get DBH from user*/
totalHt = getTotHt(totalHt);
/*Gets tree height from user*/
totalVol = calcTotVol(b0, b1, dbh, totalHt);
/* Calculates values */
/* All constants and variables
int treeNo;
int speciesCode;
string speciesDesc[6];
float dbh;
int totalHt;
double totalVol;
int Species[6];
double b0[6];
double b1[6];
int noTrees;
double avgTotVol;
*/
if (speciesCode = 11) {
speciesDesc[0] = "Loblolly Pine";
} else if (speciesCode = 12) {
speciesDesc[1] = "White Pine";
} else if (speciesCode = 13) {
speciesDesc[2] = "Red Pine";
} else if (speciesCode = 21) {
speciesDesc[3] = "White Oak";
} else if (speciesCode = 22) {
speciesDesc[4] = "Red Oak";
} else if (speciesCode = 23) {
speciesDesc[5] = "Other Oak";
}
Species[0] = 11;
Species[1] = 12;
Species[2] = 13;
Species[3] = 21;
Species[4] = 22;
Species[5] = 23;
if (speciesCode = 11) {
b0[0] = 1.2446;
} else if (speciesCode = 12) {
b0[1] = 0.000;
} else if (speciesCode = 13) {
b0[2] = 2.0822;
} else if (speciesCode = 21) {
b0[3] = .7316;
} else if (speciesCode = 22) {
b0[4] = 1.6378;
} else if (speciesCode = 23) {
b0[5] = .7554;
}
if (speciesCode = 11) {
b1[0] = .002165;
} else if (speciesCode = 12) {
b1[1] = .002364;
} else if (speciesCode = 13) {
b1[2] = .002046;
} else if (speciesCode = 21) {
b1[3] = .001951;
} else if (speciesCode = 22) {
b1[4] = .002032;
} else if (speciesCode = 23) {
b1[5] = .002174;
}
totalVol = b0[6] + b1[6] * pow(dbh, 2) * totalHt;
for (i = 0; i < treeNo; i++) {
cout << "Tree Number " << i + 1 << ": " << speciesCode << dbh << totalHt
<< speciesDesc[6] << b0[6] << b1[6] << totalVol;
}
}
/************************************************************************************************
* *************/
int getTreeNo(int treeNo) {
do {
cout << "Please enter the number of trees :" << endl;
cin >> treeNo;
if ((treeNo <= 0) || (treeNo > 999)) {
cout << "ERROR!!!,You cannot have more then999 entries" << endl;
}
} while ((treeNo <= 0) || (treeNo > 999));
return treeNo;
}
int getSpeciesCode(int speciesCode) {
do {
cout << "Please enter your Species Code";
cin >> speciesCode;
} while ((speciesCode != 11) || (speciesCode != 12) ||
(speciesCode != 13) || (speciesCode != 21) ||
(speciesCode != 22) || (speciesCode != 23));
cout << "ERROR!!!,That information does not exist wthin our system" << endl;
return speciesCode;
}
float getDbh(float dbh) {
do {
cout << "Please enter the DBH of the tree, The DBH must be greter then "
"and equal to five and less then 50.6" << endl;
cin >> dbh;
if ((dbh < 5) || (dbh > 50.6)) {
cout << "ERROR!!!, The DBH must be greter then and equal to five "
"and less then 50.6" << endl;
}
} while ((dbh < 5) || (dbh > 50.6));
return dbh;
}
int getTotHt(int totalHt) {
do {
cout << "Please enter the height of the tree" << endl;
cin >> totalHt;
if ((totalHt < 24) || (totalHt > 160)) {
cout << "ERROR!!!, Please enter a height thats not larger then "
"160, but greater then 24" << endl;
}
} while (totalHt < 24 || totalHt > 160);
{ return totalHt; }
}
double calcTotVol(double array[], double array1[], float dbh, int totalHt) {
double totalVol;
totalVol = array[6] + array1[6] * pow(dbh, 2) * totalHt;
return totalVol;
}
can anyone help me my speciesCode function keeps looping and will not accept my input actually I do not believe any function is accepting it I have worked for hours to try to figure out why and am stuck
thanks for any help
the condition in this while statement will never ever be false, and your loop will never exit. In order for it to be false, speciesCode has to have more than 1 value at a time.
while((speciesCode != 11) || (speciesCode != 12) || (speciesCode != 13) || ...)
You probably want to keep repeating until you get a valid code. In that case use &&
while((speciesCode != 11) && (speciesCode != 12) && (speciesCode != 13) && ...)
I'm trying to create a roman calculator that reads from a file. I'm struggling to figure out how to add characters to a string. I would like a new character to be added with no spaces after each iteration of a loop this would be used when the program is writing the answer.
I've tried this.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
string convert_to_Roman(int num)
{
string c;
while (num>0)
{
string c;
if (num >= 1000)
{
num = num - 1000;
return c='M';
}
else if (num >= 500 && num<1000)
{
num = num -500;
return c = 'D';
}
else if (num >= 100 && num<500)
{
num = num -100;
return c= 'C';
}
else if (num >= 50 && num<100)
{
num = num - 50;
return c = 'L';
}
else if (num >= 10 && num<50)
{
num = num - 10;
return c = 'X';
}
else if (num >= 5 && num<10)
{
num = num - 5;
return c = 'V';
}
else if (num<5)
{
num = num - 1;
return c = 'I';
}
c +=c;
//cout<<"answer= "<< + answer<<endl;
}
cout << c;
}
int convert_from_Roman(string & s)
{
int num=0;
int length; //length of string
length = s.length();
for (int i = 0; i < length; i++)
{
char c = s[i];
int digit;
if (c == 'M')
{
return num = 1000;
}
else if (c == 'D')
{
return num = 500;
}
else if (c == 'C')
{
return num = 100;
}
else if (c == 'L')
{
return num = 50;
}
else if (c == 'X')
{
return num = 10;
}
else if (c == 'V')
{
return num = 5;
}
else if (c == 'I')
{
return num = 1;
}
else
{
cout << "invalid entry" << endl;
continue;
}
num += num;
}
cout<<num<<endl;
}
void print_Result(/* figure out the calling sequence */)
{
// fill in your code
}
// Note the call by reference parameters:
string finalAnswer()
{
string operand1, operand2;
char oper;
cout << "enter operation: " << endl;
cin >> operand1 >> operand2 >> oper;
int value1, value2, answer;
value1 = convert_from_Roman(operand1);
value2 = convert_from_Roman(operand2);
switch (oper)
{
case '+':
{
answer = value1 + value2;
break;
}
case '-':
{
answer = value1 - value2;
break;
}
case '*':
{
answer = value1*value2;
break;
}
case '/':
{
answer = value1 / value2;
break;
}
default:
{
cout << "bad operator : " << oper << endl;
return;
}
string answerInRoman = convert_to_Roman(answer);
return answerInRoman;
cout << "answer= " << answerInRoman << " (" << answer << ") " << endl;
}
You can simply use concatenation like so.
char addThis;
string toThis;
addThis = 'I';
toThis = "V";
toThis += addThis;
or
toThis = toThis + addThis;
If you want to place the number somewhere other than the end of a string, you can access the elements of a string like an array toThis[0] is equal to 'V'.
If you are not using std::string as mentioned below, this can be done with a dynamic character array and an insert method that resizes the array properly as follows:
#include <iostream>
using namespace std;
void addCharToArray(char * & array, int physicalSize, int & logicalSize, char addThis)
{
char * tempPtr;
if (physicalSize == logicalSize)
{
tempPtr = new char[logicalSize + physicalSize];
for (int i = 0; i < logicalSize; i++)
{
tempPtr[i] = array[i];
}
delete [] array;
array = tempPtr;
}
array[logicalSize] = addThis;
logicalSize++;
}
int main()
{
char addThis = 'I';
char * toThis;
int physicalSize = 1;
int logicalSize = 0;
toThis = new char[physicalSize];
toThis[0] = 'V';
logicalSize++;
//when adding into the array, you must perform a check to see if you must add memory
addCharToArray(toThis, physicalSize, logicalSize, addThis);
for (int i = 0; i < logicalSize; i++)
{
cout << toThis[i];
}
cout << endl;
return 0;
}