GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Nov-2005, 15:26
SpyD3r SpyD3r is offline
New Member
 
Join Date: Nov 2005
Posts: 5
SpyD3r is on a distinguished road
Exclamation

Frustration...Roman Numeral Program


Ok...I've been working on this program for quite sometime, and I have little idea what to do. I know I am supposed to use getline for inputting my data into the program because my program is supposed to read a set of test data like this:

I+XIX
XXIII / V
CCCL +MMI
XIV- XXXV
VI * IX

So obviously, I have to set conditions and such so that it can read the roman numerals and extract the numerals and the operator.

This code is probably a train wreck...but its what I have so far...can anyone help me out? I would greatly appreciate it...

~SpyD3R (Frustrated)

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;


void stringToDecimal(char expression[], int &deciRes,
                     const int d[], const char r[]);

void  performOperation(int, int, char, int &);

bool checkForErrors(int, int, char, int);

void decimalToString(int, char romRes[], const int d[], const char r[]);

const int  decimal[8] = {0, 1, 5, 10, 50, 100, 500, 1000};
const char roman[8]   = {'O', 'I', 'V', 'X', 'L', 'C', 'D', 'M'};

int main()
{
    char   expression1[19],
           expression2[19],
           romanResult[19] = {0},
           operation;

    int    addition,
           subtraction,
           multiplication,
           division,
           decimalEx1 = 0,
           decimalEx2 = 0,
           decimalResult;

    string e1, e2, bob, oper;

    bool valid;

    getline(cin, bob);


    while (!(expression1[0] == 'O' && expression2[0] == 'O' && operation == '+'))
      {
        valid = false;

        stringToDecimal(expression1, decimalEx1, decimal, roman);
        stringToDecimal(expression2, decimalEx2, decimal, roman);

        performOperation(decimalEx1, decimalEx2, operation, decimalResult);

 valid = checkForErrors(decimalEx1, decimalEx2, operation, decimalResult);

        if (valid == true)
          {
            decimalToString(decimalResult, romanResult, decimal, roman);
            cout << bob << e1 << " " << oper << " " << e2
                 << " " << " = " << " " << exDec1 << " " << oper << " " << exDec2 << " " << "=" << " " << dResult
                 << " (" << rResult << ") " << endl;


          }


      }

    return 0;
}



void stringToDecimal(char expression[], int &deciRes,
                     const int decimal[], const char roman[])
{
  int i, n, j;
 n = strlen(expression);
  deciRes = 0;

  for(i=0; i < n; i++)
    {
      for(j=1; j < 8; j++)
        {
          if(expression[i] == roman[j])
            deciRes += decimal[j];
        }
    }

}

void performOperation(int e1, int e2, char oper, int &deciRes)
{
  switch (oper)
    {
    case '+':
      deciRes = e1 + e2;
      break;
    case '-':
      deciRes = e1 - e2;
      break;
    case '*':
      deciRes = e1 * e2;
      break;
    case '/':
      deciRes = e1 / e2;
      break;
    default:
      deciRes = e1 % e2;
    }
}

bool checkForErrors(int deciEx1, int deciEx2, char oper, int deciRes)
{


  if (oper != '+' && oper != '-' && oper != '*' && oper != '/' &&
           oper != '%')
    {
      cout << "Error. Invalid operation" << endl;
      return false;
    }

  else if(((deciEx1 == deciEx2) && oper =='-') || deciRes == 0)
    {
 {
      cout << "Error. Result can not be zero" << endl;
      return false;
    }

  else if((oper == '+' || oper == '*') && deciRes > 4999 )
    {
      cout << "Error. Result can not be more than 4999" << endl;
      return false;
    }

  else
    return true;
}

void decimalToString(int decAns, char romAns[], const int d[], const char r[])
{
  int i, j=0, n, count=0;

  // Reset values in romanResult[]
  n= strlen(romAns); // if taken out, ex2 wont show; length romAns not defined
  for(i=0; i < n; i++)
    romAns[i] = 0;
  // End
  for(i=7; i > 0; i--)
    {
      if( decAns >= d[i])
        {
          count = decAns / d[i];
          decAns = decAns - (count * d[i]);

          while (count != 0)
            {
              romAns[j] = r[i];
              j++;
              count--;
            }
        }
    }
}

{

  cout << bob << e1 << " " << oper << " " << e2
       << " " << " = " << " " << exDec1 << " " << oper << " " << exDec2 << " " << "=" << " " << dResult
       << " (" << rResult << ") " << endl;
}
Last edited by JdS : 11-Nov-2005 at 18:33. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 11-Nov-2005, 15:38
SpyD3r SpyD3r is offline
New Member
 
Join Date: Nov 2005
Posts: 5
SpyD3r is on a distinguished road

More Specifically


Let me be a tad bit more specific...

I really just need help with the getline() statments and setting the condtions...I have no idea where to start...Thanks

SpyD3r
  #3  
Old 11-Nov-2005, 16:02
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: More Specifically


Quote:
Originally Posted by SpyD3r
Let me be a tad bit more specific...

I really just need help with the getline() statments and setting the condtions...I have no idea where to start...Thanks

SpyD3r:banana:
I do not understand what you are asking? What do you mean "getline() statements"?

I would also say that you should at least start with code that compiles.
Quote:
Originally Posted by Errors
cpp(65) : error C2065: 'exDec1' : undeclared identifier
cpp(65) : error C2593: 'operator <<' is ambiguous
cpp(65) : error C2065: 'exDec2' : undeclared identifier
cpp(66) : error C2065: 'dResult' : undeclared identifier
cpp(66) : error C2065: 'rResult' : undeclared identifier
cpp(76) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
cpp(127) : error C2181: illegal else without matching if
cpp(138) : error C2601: 'decimalToString' : local function definitions are illegal
cpp(165) : error C2065: 'bob' : undeclared identifier
cpp(165) : error C2593: 'operator <<' is ambiguous
cpp(165) : error C2065: 'e1' : undeclared identifier
cpp(166) : error C2065: 'e2' : undeclared identifier
cpp(166) : error C3861: 'exDec1': identifier not found, even with argument-dependent lookup
cpp(166) : error C3861: 'exDec2': identifier not found, even with argument-dependent lookup
cpp(167) : error C3861: 'dResult': identifier not found, even with argument-dependent lookup
cpp(167) : error C3861: 'rResult': identifier not found, even with argument-dependent lookup
cpp(438) : fatal error C1075: end of file found before the left brace '{' at 'testerforforum.cpp(112)' was matched

I would recommend shrinking / commenting out code until the errors are gone then start from there.
  #4  
Old 11-Nov-2005, 16:07
SpyD3r SpyD3r is offline
New Member
 
Join Date: Nov 2005
Posts: 5
SpyD3r is on a distinguished road

Re: Frustration...Roman Numerial Program


Ok...I need to know where to start with getline(cin, bob) for example (bob would be the name of my string that I am inputting)

As posted above, the test data that I have has roman numerals that have operators directly next to them or spaced out dramatically.

Such as: XIX+ VII
or XXII+IV

What I'm really asking is how do I separate and isolate each individual term in that line that I am pulling out of my test data.

I know I should be using substrings, but how would they work?

~SpyD3R
  #5  
Old 11-Nov-2005, 16:24
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

Re: Frustration...Roman Numerial Program


I'd start by reading the guidelines posted so that
1) We can read your code (Guideline 1)
2) We can understand your questions/difficulties (Guideline 2)

Using #2, explain where the problem is (point to the specific code), what you want to do (be specific), and what it is you need help understanding.

Quote:
Originally Posted by Spy3DR
Let me be a tad bit more specific...

I really just need help with the getline() statments and setting the condtions...I have no idea where to start
So be specific. Your specific request prompts this response:
Use getline() as you are. There are no conditions associated with getline() based on your needs so you need to explain the conditions you mean.
__________________

Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with.
  #6  
Old 11-Nov-2005, 16:37
SpyD3r SpyD3r is offline
New Member
 
Join Date: Nov 2005
Posts: 5
SpyD3r is on a distinguished road

Re: Frustration...Roman Numerial Program


[C++

getline(cin, bob);

I know I need to extract substrings from my test data after my getline statement.

Example of test data:
VIV+ III <-- How would I go about separating that into substrings? (Note: There is no space in between the first roman numeral and operator).

Another Example:
XIX -X <--Same thing except the subtraction operator is right next to 10...how would I separate that?

Finally,
XXI+V <--All of the information together with no spaces...how is this separated using substrings?

I just need an idea of how to separate things from one string into substrings, and then from those substrings, assign them to my variables which I am using (e1 (Roman numeral 1), oper(+, -, \, *), and e2(Roman numeral 2).

Hoped that helped...

~SpyD3R

Basically, I am trying to do this:

read and display the original expression
convert each Roman numeral to its Arabic equivalent
compute the result
display the expression with Arabic numbers
display the result as an Arabic number

Assumptions
All data will be read from a data file via Linux redirection.
All expressions in the data file will be valid (valid operators and Roman numerals).
The Roman numerals and operator in each expression may be separated by 0 or more blanks.
Each line in the data file will be terminated by a linefeed (endl).
  #7  
Old 11-Nov-2005, 16:49
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Frustration...Roman Numeral Program


Quote:
Originally Posted by SpyD3r
I just need an idea of how to separate things from one string into substrings, and then from those substrings, assign them to my variables which I am using (e1 (Roman numeral 1), oper(+, -, \, *), and e2(Roman numeral 2).
You should look into "find_first_not_of", "find_first_of" and "substr".
This is a small example of how it could be done using those functions.

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string bob = "XXIII / V";

	//what we are looking for
	string separators = " +/*-";

	//find the first that is not a separator
	size_t start = bob.find_first_not_of(separators);
	size_t end = 0; 

	while(start != string::npos) 
	{//the end will be a separator 
		end = bob.find_first_of(separators, start + 1); 

		if(end == string::npos)
			//when end == npos that will not be a valid position
			//so set end to a valid position to get the last one 
			end = bob.length(); 

		//display the substring 
		cout << bob.substr(start, end - start)       
			<< endl;

		//find the beginning of next substring 
		start = bob.find_first_not_of(separators, end + 1);
	}

	string roman = "OIVXLCDM";
	start = bob.find_first_not_of(roman);
	end = 0; 
	while(start != string::npos) 
	{
		end = bob.find_first_of(roman, start + 1); 
		if(end == string::npos)                        
			end = bob.length();                         

		cout << "\"" << bob.substr(start, end - start)       
			<< "\"" << endl;

		start = bob.find_first_not_of(roman, end + 1);
	}
	return 0;
}
Last edited by JdS : 11-Nov-2005 at 18:31. Reason: Please insert your C code between [c] & [/c] tags
  #8  
Old 11-Nov-2005, 18:08
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Frustration...Roman Numerial Program


Don't use string, use char.

CPP / C++ / C Code:
char bob[100];

Then:
bob[0] is the first character (V in first example)
bob[1] is the second character (I)
bob[2] is the third character (V)
etc.

Or if you must use string look up the substr() method:
bob.substr(0,1); is the first character (V)
bob.substr(1,1) is the second character (I)
bob.substr(2,1) is the third character (V)
etc.

And Sokar, with 53 posts under your belt, I know you've been told about the Guidelines and code tags. Reread Guideline #3. It'll help you post with the proper tags.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #9  
Old 11-Nov-2005, 20:26
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Frustration...Roman Numerial Program


Quote:
Originally Posted by WaltP
And Sokar, with 53 posts under your belt, I know you've been told about the Guidelines and code tags. Reread Guideline #3. It'll help you post with the proper tags.
No, this would be the first time anyone specifically mentioned them to me. As for your kind advice, I have never read the "Guidelines" and have no intention of reading them. As for the code tags it does not really matter to me which I use and I could care less. If you do not like what I post, then just ask me to stop posting and I will stop. It does not make any difference to me.
  #10  
Old 11-Nov-2005, 20:47
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Smile

Hello Sokar,


Hi Sokar,

I think WaltP's post was not intended to discourage you.
Just to give you information. Thats it.

Quote:
Originally Posted by Sokar
As for your kind advice, I have never read the "Guidelines" and have no intention of reading them
Yes. The guidelines are only for those who request help. The title itself indicates that: Guidelines for posting requests for help:

GIDForums™ is now the only forum in the world offering custom [c] and [c++] and other syntax highlighting bbcode.
So, it would be easier to look at the code and the syntax errors when we post the code between
[c] and [/c] or [c++] and [/c++]tags.
So, we can make use of it to the fullest extent possible and help the Original posters.

Please dont stop posting. It makes a great difference to us. Your posts are always friendly and correct, that is very much appreciated.

Thank You,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Frustrating C++ problem... Roman numerals Elsydeon C++ Forum 10 01-Sep-2005 08:37
Type casts ? kai85 C++ Forum 12 23-Jun-2005 13:04
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 14:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 21:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:08.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.