0

I am coding a roman to arabic console program for class been having issue with it I am not sure how to get the next char after the the first char I have tried everything I am not allowed to use cin.peek(). Also my output loop generates p = 0 if invalid case is given which I can't figure out how to not let it do that, as it should only output the default statment.
Anyway here is my code:

#include <iostream>
using namespace std;

int main()
{
    char afterChar = '0';
    int counter = 1;

    while (counter <= 10)
    {
        char romanNumeral;

        int arabicNumeral = 0;

        cout << "Enter a Roman numeral # " << counter << ": ";

        while ((romanNumeral = cin.get()) != '\n')
        {
            romanNumeral = toupper(romanNumeral);

            switch (romanNumeral)
            {
            case 'M':

                arabicNumeral += 1000;
                break;

            case  'D':

                if (afterChar == 'M')
                {
                    arabicNumeral -= 500;
                }
                else
                {
                    arabicNumeral += 500;
                }
                break;

            case  'C':

                if (afterChar == 'M' || afterChar == 'D')
                {
                    arabicNumeral -= 100;
                }
                else
                {
                    arabicNumeral += 100;
                }
                break;
            case 'L':

                if (afterChar == 'M' || afterChar == 'D' || afterChar == 'C')
                {
                    arabicNumeral -= 50;
                }
                else
                {
                    arabicNumeral += 50;
                }
                break;

            case 'X':

                if (afterChar == 'M' || afterChar == 'D' || afterChar == 'C' || afterChar == 'L')
                {
                    arabicNumeral -= 10;
                }
                else
                {
                    arabicNumeral += 10;
                }
                break;

            case 'V':

                if (afterChar == 'M' || afterChar == 'D' || afterChar == 'C' || afterChar == 'L' || afterChar == 'X')
                {
                    arabicNumeral -= 5;
                }
                else
                {
                    arabicNumeral += 5;
                }
                break;

            case 'I':

                if (afterChar == 'M' || afterChar == 'D' || afterChar == 'C' || afterChar == 'L' || afterChar == 'X' || afterChar == 'V' )
                {
                    arabicNumeral -= 1;
                }
                else 
                {                   
                    arabicNumeral += 1;
                }
                break;

            default:
                cerr << romanNumeral << " Error - character not valid!!!" << '\n' << endl;

            }

            afterChar = romanNumeral;

            if (romanNumeral == '\n')
                break;
        }

        cout << afterChar << " = " << arabicNumeral << '\n'<< endl;

        counter++;

    }

    system("pause");
    return 0;
}

Anyway when I input the roman number IV out put is V = 6 instead it should be IV = 4 but when I input VI out put is I = 4 when it should be VI = 6, it seems to be calulating in reverse. Also how do I display everything that is enter in a cin.get() as the output only does this I = 4 or only gets the last char. I can not use string or arrays either. And sorry if my english is bad and if my explaining is bad too. I am new to programing in C++ only have had 3 class this is a hard program to program.

Please I need help and thank you in advance.

3
Contributors
2
Replies
42
Views
6 Months
Discussion Span
Last Post by tinstaafl
0

A couple of suggestions I'd make:

Instead of reading each character separately, it would be easier to read a string.

Once you get the string, change the while loop to a for loop to loop through the characters in the string. The string class includes a size function to use as the limit, and the counter will represent the index of the character to process.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.