I wrote two programs, this one:

#include<iostream>
using namespace std;
main()
{
          char rom,invalid;
          double tot;
          tot=0;
    cout<<"Please enter Roman numerals ending with a period."<<endl;
    while (rom != '.')
     {
  cin.get(rom);
 
  if(rom=='M' || rom=='m')
{
  tot=tot+1000;
}
else if(rom=='D' || rom=='D')
{

  tot=tot+500;
}
  else if(rom=='C' || rom=='C')
{
  tot=tot+100;
}
  else if(rom=='L' || rom=='l')
{

  tot=tot+50;
}
  else if(rom=='X' || rom=='x')
{

  tot=tot+10;
}
  else if(rom=='V' || rom=='v')
{

  tot=tot+5;
}
else if(rom=='I' || rom=='i')
{

  tot=tot+1;
}
else 
{
     invalid;
     }
}
cout<<"The value in Roman numerals is "<<tot<<endl;


   system("PAUSE");
    return EXIT_SUCCESS;
}

and this one:

#include <iostream>
using namespace std;
int main()
{
    char repeat='y';
    while (repeat=='y' || repeat=='Y')
    {
    cout << "Enter a number to convert to Roman numerals ";
    int num;
    cin >> num;
    while (num >=1000) {
        cout <<"M";
        num = num - 1000;
}
 while (num ==900) {
        cout <<"CM";
        num = num - 900;
}
 while (num >=500) {
        cout <<"D";
        num = num - 500;
}
 while (num ==400) {
        cout <<"CD";
        num = num - 400;
}
 while (num >=100) {
        cout <<"C";
        num = num - 100;
}
 
 while (num >=90 && num<=99) {
        cout <<"XC";
        num = num - 90;
}
 while (num >=50) {
        cout <<"L";
        num = num - 50;
}
 while (num ==40) {
        cout <<"XL";
        num = num - 40;
}
 while (num >=10) {
        cout <<"X";
        num = num - 10;
}
  while (num ==9) {
        cout <<"IX";
        num = num - 9;
}
 while (num >=5) {
        cout <<"V";
        num = num - 5;
}
 while (num ==4) {
        cout <<"IV";
        num = num - 4;
}
 while (num >=1) {
        cout <<"I";
        num = num - 1;
}
cout<<endl;
cout<<endl;
cout<<endl;
    cout<<"\nWould you like to repeat this program? (y or n)"<<endl;
        cin>>repeat;
        system("cls");
    }
    cout<<"Thank you for using this program\n";
  system("PAUSE");
    return EXIT_SUCCESS;
}

I want the first program to be like the second, so that When I type IV it prints out 4 instead of 6... i know the cin.get command reads each character by itself, so i cant put something like

if(rom=='IV' || rom=='iv')
{
  tot=tot+4;
}

so is there a way to make it check the next character in line when it enters the if statement?? and if false, to exit that statement, and go on to the next?

Recommended Answers

All 2 Replies

Because you need to look-ahead, it's probably better to read the whole string into memory.

Then if you're looking at an 'i', you can look at the next character to see whether it is another 'i', or a 'v' or an 'x', then decide what to do about it.

if(rom=='IV' || rom=='iv')
{
  tot=tot+4;
}

That ain't character comparision. 'iv' is not counted as a character. A character implies "only one character" so you end up comparing only the first character i.e. 'i' in your case. Try using string comparision functions like strcmp() .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.