There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijackthis internet internet explorer itunes laptop mac malware motherboard mouse network networking outlook 2007 power printer problem ram router screen slow sound trojan usb virus vista vista 32-bit windows windows vista windows xp winxp wireless wmp
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Intro C++ problem


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
big papa's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2003
11-Nov-2003, 04:18 PM #1
Intro C++ problem
I have a class project to take roman numerals from an input file, through functions convert them to integers, perform a mathmatical function, convert them back to Roman numerals, and send them to an output file.

I've managed to establish the input and output files, set up rough functions for the conversion of single Roman numerals, and perform the mathmatical functions in integer form. I say rough functions since I've been told that I can call the same conversion function for all the Roman numerals but have trouble understanding how to define each total for the different numbers.

I'm having trouble seeing the data from the input file and bringing it into the code, not to mention I have no idea how to go about the looping for Roman numerals larger than one character for the conversion to integer and then back to symbol form.

Any help or suggestions would be appreciated.

******************

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int convertDigit( string );
int mathFunc( string, int, int );

openInput (ifstream);
openOutput (ofstream);

int main()

{
string romanNum;
string romanNum2;

int digit1;
int digit2;
string math;
int total1;
ifstream inData;
ofstream outData;

inData.open("proj4.in"); // open files
outData.open("proj4.out");

inData >> romanNum >> romanNum2;

/* getline(inData, romanNum); // get data from first line

*/ getline(inData, romanNum2); // get data from second line

outData << "The first number is " << romanNum << endl; // output statement for first Roman numeral

outData << "The second number is " << romanNum2 << endl; // output statement for second Roman numeral

outData << "The total of " << romanNum << " and " << // mathematical total in Roman numerals
romanNum2 << " is " << total1 << endl;

total1 = mathFunc( math, digit1, digit2 );

outData << "Which in decimal notation is " << total1 << endl; // total in decimal notation

inData.close(); // close files
outData.close();

cout << "\n Complete" << endl << endl;

return 0;

}


//*******************************************
// function to translate Roman numbers to integers

int convertDigit1( string romanNumber)
{

int digit;

if (romanNumber == "M")
{
digit = 1000;
}
else if (romanNumber == "D")
{
digit = 500;
}
else if (romanNumber == "C")
{
digit = 100;
}
else if (romanNumber == "L")
{
digit = 50;
}
else if (romanNumber == "X")
{
digit = 10;
}
else if (romanNumber == "V")
{
digit = 5;
}
else
digit = 1;

return digit;
}


//*******************************************
// function to calculate totals of first group of numbers

int mathFunc( string symbol, int digit1, int digit2 )
{

int total1;

if (symbol == "+")
{
total1 = digit1 + digit2;
}

else if (symbol == "-")
{
total1 = digit1 - digit2;
}

else if (symbol == "*")
{
total1 = digit1 * digit2;
}

else
{
total1 = digit1 / digit2;
}

return total1;
}
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
11-Nov-2003, 09:54 PM #2
Ok bigpapa. Glad you have done some work yourself before posting here.

You have a function 'convertdigit1' which should do what you want of it. The next step is to look at how to combine the various Roman digits to give an overall figure representing the total of one overall Roman number.

The key is to realise that both position and relative size play a role. Consider two simultaneous Roman digits. If the first is bigger than the second then it is added to it. If the first is smaller than the second it is subtracted from it.

eg consider X=10 and I=1.
XI is 11, 10 is bigger than 1 and is before it, therefore 10 + 1 = 11
IX is 9, 1 is smaller than 10 and is before it, therefore 10 - 1 = 9

It becomes more complicated when we consider larger groups of digits. If you look at Roman numbers though, you will see that there may be one digit to subtract from a larger one coming immediately after it, but there are never two digits to subtract.

eg XIX is 19, XIIX does not happen, 18 is XVIII, all additions

Here we have the bones of an algorithm for writing a function to produce a running total.

Create a Running total, a Current digit and a Next digit.

Start at the left digit.
1. Test the next Roman digit to the right to see if it is larger.
2. If it is not, then add the current number to our running total, move right to the next digit and start again at step 1.
3. If it is larger, subtract our current digit from the total, add the following larger digit to the running total, then move two digits to the right from our current position to step over it and start again with an unused digit at step 1.

Take a deep breath! For example.

MCMLXXIX

Start: MCMLXXIX; Running total=0; Current digit=M(1000); Next digit=C(100).

1. Is C>M? No.

2. Running total=0+1000=1000; one step right; MCMLXXIX; Current digit=C(100); Next digit=M(1000).

1. Is M>C? Yes.

3. Running total=1000-100+1000=1900; two steps right; MCMLXXIX; current digit=L; Next digit=X.

1. Is X>L? No.

2. Running total=1900+50=1950; one step right; MCMLXXIX; Current digit=X(10); Next digit=X(10).

1. Is X>X? No. (Make sure you can see the truth of this statement!)

2. Running total=1950+10=1960; one step right; MCMLXXIX; Current digit=X(10); Next digit=I(1).

1. Is I>X? No.

2. Running total=1960+10=1970; one step right; MCMLXXIX; Current digit=I(1); Next digit=X(10).

1. Is X>I? Yes.

3. Running total=1970-1+10=1979; two steps right; MCMLXXIX?; No more digits.

Hope that didn't lose you , and also that I got it right!

I think the algorithm should give you enough to go on in creating your functions.

Do a bit more work and report back here on your progress. If needed I will help a little more if I can, if not at least I will know you got there. Who knows, by then I may have something more concrete worked out myself. Curiosity killed the cat after all, (but satisfaction brought it back ).

Good luck.
__________________
1. "I make no personal claim to the truth, only the right to seek it, prove it in argument, and to be wrong many times in order to reach it."

2. "We have made a cage of words and placed our God inside, as boys trap a cricket, to make him sing for us alone."

Galileo Galilei

Last edited by AlbertB : 11-Nov-2003 10:05 PM.
big papa's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2003
12-Nov-2003, 12:48 PM #3
Thnx for your input. I managed to work out my input problem so now the program can see the data it is to work upon.

I'll start working on the looping issues in a little bit and check in on the progress. (Also can up with some ideas for debugging that I need to see about making work.)

Big Papa
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
12-Nov-2003, 02:29 PM #4
Good stuff, keep me informed.
Snake~eyes's Avatar
Senior Member with 640 posts.
 
Join Date: Apr 2002
13-Nov-2003, 08:34 PM #5
this looks like a problem from Mercers book? is it?
big papa's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2003
19-Nov-2003, 11:24 AM #6
Its not from Mercers but "Programming an Problem Solving with C++". The teacher decided to pull one of the practice problems from a chapter for a project.

I have included updated code that handles the functions of converting the Roman numbers to intergers, the mathmatic functions, and the conversion back to Roman characters.

While able to develop the more complex code for the conversions, I have been unable to figure out the loop to check the input file if there are additional lines to process. I've tried what little I know of loops and nothing works.

Any help is appreciated.


// The following is a Roman number calculator. The program
// enters Roman numbers from the input file, translates the
// Roman numbers to integers and performs a mathematic operator
// on two numbers, translates the integer results back to Roman
// numerals, and sends the results to the output file.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int convertDigit( string );
int convertDigit2( string );
int mathFunc( string, int, int );
string convertRoman( int );

openInput (ifstream);
openOutput (ofstream);

int main()

{
string romanNum1;
string romanNum2;
string math;
int digit1;
int digit2;
int total1;
string total2;
ifstream inData;
ofstream outData;

inData.open("proj4.in"); // open input file

if (!inData)
{
cout << "Cannot open file." << endl << endl // test for proper input file
<< "Program terminated." << endl << endl;

return 1;
}

outData.open("proj4.out"); // open output file

getline(inData, romanNum1); // get Roman numeral from first line

getline(inData, romanNum2); // get Roman numeral from second line

getline(inData, math); // get mathematical operator from third line

outData << "The first number is " << romanNum1 << endl; // output statement for first Roman numeral

digit1 = convertDigit( romanNum1 ); // call to function to convert the first Roman numeral
// to an integer
outData << "The second number is " << romanNum2 << endl; // output statement for second Roman numeral

digit2 = convertDigit2( romanNum2 ); // call to function to convert the first Roman numeral
// to an integer
total1 = mathFunc( math, digit1, digit2 ); // call to function for mathematical operation on
// the first and second Roman numeral in integer form
total2 = convertRoman( total1 ); // call to function to convert the mathematical total to Roman numerals
outData << "The total of " << romanNum1 << " and " <<
romanNum2 << " is " << total2 << endl; // output statement showing mathematical total in Roman numerals

outData << "Which in decimal notation is " << total1 << endl; // output statement showing total in decimal notation

inData.close(); // close files
outData.close();

cout << "\n Complete" << endl << endl;

return 0;

}

//*******************************************
// function to translate Roman numbers to integers

int convertDigit( string romanNumber)
{

int index; // loop counter
int digit1;
digit1 = 0; // inital value of digit1

for (index = 0; index < romanNumber.length(); index++)
{

if (romanNumber.substr( index, 1 ) == "M")
{ // value of "M" is 1000 for each location in the line
digit1 = digit1 + 1000;
}
else if (romanNumber.substr( index, 1 ) == "D")
{ // value of "D" is 500 for each location in the line
digit1 = digit1 + 500;
}
else if (romanNumber.substr( index, 1 ) == "C")
{ // value of "C" is 100 for each location in the line
digit1 = digit1 + 100;
}
else if (romanNumber.substr( index, 1 ) == "L")
{ // value of "L" is 50 for each location in the line
digit1 = digit1 + 50;
}
else if (romanNumber.substr( index, 1 ) == "X")
{ // value of "X" is 10 for each location in the line
digit1 = digit1 + 10;
}
else if (romanNumber.substr( index, 1 ) == "V")
{ // value of "V" is 5 for each location in the line
digit1 = digit1 + 5;
}
else
digit1 = digit1 + 1; // value of "I" is 1 for each location in the line
}

return digit1; // return total value of digit1 to main
}

//*******************************************
// function to translate Roman numbers to integers

int convertDigit2( string romanNumber )
{

int index; // loop counter
int digit2;
digit2 = 0; // inital value of digit2

for (index = 0; index < romanNumber.length(); index++)
{
if (romanNumber.substr( index, 1 ) == "M")
{ // value of "M" is 1000 for each location in the line
digit2 = digit2 + 1000;
}
else if (romanNumber.substr( index, 1 ) == "D")
{ // value of "D" is 500 for each location in the line
digit2 = digit2 + 500;
}
else if (romanNumber.substr( index, 1 ) == "C")
{ // value of "C" is 100 for each location in the line
digit2 = digit2 + 100;
}
else if (romanNumber.substr( index, 1 ) == "L")
{ // value of "L" is 50 for each location in the line
digit2 = digit2 + 50;
}
else if (romanNumber.substr( index, 1 ) == "X")
{ // value of "X" is 10 for each location in the line
digit2 = digit2 + 10;
}
else if (romanNumber.substr( index, 1 ) == "V")
{ // value of "V" is 5 for each location in the line
digit2 = digit2 + 5;
}
else
digit2 = digit2 + 1; // value of "I" is 1 for each location in the line

}
return digit2; // return total value of digit1 to main
}

//*******************************************
// function to calculate totals of first group of numbers

int mathFunc( string symbol, int digit1, int digit2 )
{

int total1;

if (symbol == "+")
{ // addition operation on digit1 and digit2
total1 = digit1 + digit2;
}
else if (symbol == "-")
{ // subtraction operation on digit1 and digit2
total1 = digit1 - digit2;
}
else if (symbol == "*")
{ // multiplication operation on digit1 and digit2
total1 = digit1 * digit2;
}
else
{ // division operation on digit1 and digit2
total1 = digit1 / digit2;
}
return total1; // return value of total1 to main
}

//****************************************
//function to convert integer total to Roman numerals

string convertRoman( int total1 )
{
int M, D, C, L, X, V, I;
int i; // loop counter
string total2;
total2 = ""; // intial value of total2

M = total1 / 1000; // divide total1 by 1000 to determine value of M
total1 = total1 - (1000 * M);
D = total1 / 500; // divide total1 by 500 to determine value of D
total1 = total1 - (500 * D);
C = total1 / 100; // divide total1 by 100 to determine value of C
total1 = total1 - (100 * C);
L = total1 / 50; // divide total1 by 50 to determine value of L
total1 = total1 - (50 * L);
X = total1 / 10; // divide total1 by 10 to determine value of X
total1 = total1 - (10 * X);
V = total1 / 5; // divide total1 by 5 to determine value of V
total1 = total1 - (5 * V);
I = total1; // remaining characters are the value of I

for (i = 1; i <= M; i++)
{ // value of total2 is "M" for each location in the line
total2 = total2 + "M";
}
for (i = 1; i <= D; i++)
{ // value of total2 is "D" for each location in the line
total2 = total2 + "D";
}
for (i = 1; i <= C; i++)
{ // value of total2 is "C" for each location in the line
total2 = total2 + "C";
}
for (i = 1; i <= L; i++)
{ // value of total2 is "L" for each location in the line
total2 = total2 + "L";
}
for (i = 1; i <= X; i++)
{ // value of total2 is "X" for each location in the line
total2 = total2 + "X";
}
for (i = 1; i <= V; i++)
{ // value of total2 is "V" for each location in the line
total2 = total2 + "V";
}
for (i = 1; i <= I; i++)
{ // value of total2 is "I" for each location in the line
total2 = total2 + "I";
}
return total2; // return value of total2 to main
}
8
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
19-Nov-2003, 02:29 PM #7
At first glance your translation functions to produce 'digit1' and 'digit2' will not work either. You have not taken into account that if a letter is before a larger letter it must be subtracted and not added.

What is the value of 'XXI' ?

What is the value of 'XIX' ?

Why are they different?

Mentally run these two numbers through your function and check its operation.
__________________
1. "I make no personal claim to the truth, only the right to seek it, prove it in argument, and to be wrong many times in order to reach it."

2. "We have made a cage of words and placed our God inside, as boys trap a cricket, to make him sing for us alone."

Galileo Galilei
big papa's Avatar
Junior Member with 5 posts.
 
Join Date: Nov 2003
19-Nov-2003, 06:07 PM #8
i see what you're saying. i don't have any way of qualifying the order of the characters. also for the purposes of this problem the teacher recinded the rule of I before a character reduces it by 1. so for this problem the equivalent of 9 is VIIII instead of IX.

and before it is mentioned i realize that i should be able to call the intial function for translation from Roman to integer once to be able to convert more than one set one numerals, but once again my limited knowledge with coding isn't taking me very far.
Snake~eyes's Avatar
Senior Member with 640 posts.
 
Join Date: Apr 2002
19-Nov-2003, 07:22 PM #9
I would use a case to do this. This code won't be right syntax but you'll get the idea.

romanNum; //the string of characters
for(counter=0; counter < charNum; counter++) //char num is how many characters
{
switch(romanNum[counter])
{
case: 'I'
total = total + 1;
break;
case: 'X'
total = total + 5;
break;
...continue...

}
}

That is exactly how I would do it.

I think that's what you want but im not totally sure.
__________________
Epic Servers

Please do not email/pm for support, use the forums
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 08:00 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.