How to get my program to read in more than one roman numeral at a time

How can i make my program read in more than one Roman numeral? right now it will read it one then give out the value but the program ends there... Id like to use a do-while loop but i tried it and it wont return anything at all
HELP!
heres what ive got so far

#include <iostream>
using namespace std;

int main()
{
      int value,sum;
      char numeral;
      sum=0;

      cout<<"enter numeral ";
      cin>>numeral;


      switch (numeral)
      {
      case 'M':
            value=1000;
            break;
      case 'D':
            value=500;
            break;
      case 'C':
            value=100;
            break;
      case 'L':
            value=50;
            break;
      case 'X':
            value=10;
            break;
      case 'V':
            value=5;
            break;
      case 'I':
            value=1;
            break;
      }
      cout<<value;
      return 0;
}
buzbomrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

buzbomrAuthor Commented:
nevermind i firgured that part out....  
buzbomrAuthor Commented:
new question: how do i get this to read out 21 instead of 102021 when XXI is entered??
#include <iostream>
using namespace std;

int main()
{
      int value,sum;
      char numeral;
      sum=0;

      cout<<"enter numeral ";
do
{
      cin>>numeral;


      switch (numeral)
      {
      case 'M':
                  value=1000;
                  sum=sum+1000;
                  break;
      case 'D':
                  value=500;
                  sum=sum+500;
                  break;
      case 'C':
                  value=100;
                  sum=sum+100;
                  break;
      case 'L':
                  value=50;
                  sum=sum+50;
                  break;
      case 'X':
                  value=10;
                  sum=sum+10;
                  break;
      case 'V':
                  value=5;
                  sum=sum+5;
                  break;
      case 'I':
                  value=1;
                  sum=sum+1;
                  break;
      }
      cout<<sum;
}while (numeral !=' ');
      return 0;
}
Kyle AbrahamsSenior .Net DeveloperCommented:


  value=1;
  sum=sum+1;

you should add value to sum instead of sum +1.

Don't forget the tricky part about roman numerals:  IV, IX, etc.

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

burcarpatCommented:

you probably need something like this?

--- cut from here ---
// c/c++
#include <numeric>
#include <iostream>
#include <string>
#include <map>

struct roman_plus {

  roman_plus() {

    value_of['M'] = 1000;
    value_of['D'] = 500;
    value_of['C'] = 100;
    value_of['L'] = 50;
    value_of['X'] = 10;
    value_of['V'] = 5;
    value_of['I'] = 1;

  }; // roman_plus

  int
  operator()(int current_sum,
             char roman_numeral) {
    return current_sum + value_of[roman_numeral];
  }; // operator()

private:
  std::map<char, int> value_of;
}; // roman_plus  


int
main() {


  std::cout << "enter the roman numeral: ";

  std::string numeral_str;
  std::cin >> numeral_str;

  int sum = std::accumulate(numeral_str.begin(), numeral_str.end(), 0, roman_plus());

  std::cout << "sum = " << sum << std::endl;

  return 0;

}; // main
--- cut from here ---

-- ba
buzbomrAuthor Commented:
output is still 102021 if you change sum to sum=sum+value
buzbomrAuthor Commented:
i dont know: #include <numeric>   #include <string>   or   #include <map>
need to keep things simple
jkrCommented:
>>new question: how do i get this to read out 21 instead of 102021 when XXI is entered??

For this particular question

int main()
{
    int value,sum;
    char numeral [ 1024];
    sum=0;

    cout<<"enter numeral ";
do
{
    cin>>numeral;

    char num;
    int i = 0;
    while ( num = numeral[i++]) {
    switch (num) {

        //....
    }
} while ( numeral [0] != ' ');

But, keep in mind what ged325 wrote: Don't forget the tricky part about roman numerals:  IV, IX, etc. The above code won't cover that.
jkrCommented:
Ooops, forgot a bracket or two:

int main()
{
   int value,sum;
   char numeral [ 1024];
   sum=0;

   cout<<"enter numeral ";
do
{
   cin>>numeral;

   char num;
   int i = 0;
   while ( num = numeral[i++]) {
   switch (num) {

       //.... your "case"s here...
   }
   }
} while ( numeral [0] != ' ');

}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kyle AbrahamsSenior .Net DeveloperCommented:


try initializing sum to 0.  When you declare a local variable, depending on the compiler, it may be filled with garbage.
Kyle AbrahamsSenior .Net DeveloperCommented:
Hey guys, I believe this is HW, (considering the experience of the questioner, no offense) until we find out please stop throwing up completed solutions.
jkrCommented:
>>find out please stop throwing up completed solutions

Um, the approach was *cough* close already :o)
buzbomrAuthor Commented:
i keep getting error "no conversion from int to char"??
any suggestions as to why?
Kyle AbrahamsSenior .Net DeveloperCommented:
LOL, yeah, I guess, wonder if he forgot about the IX or if that's not part of the assignment.
Kyle AbrahamsSenior .Net DeveloperCommented:
switch only takes an int paramater.  

do this:

int x = atoi(numeral);

switch on x instead of numeral.

atoi stands for ascii to integer, and will convert it to it's integer form from the ascii table.  When you compare the int with the char (ie case 'c') it should convert it to integer as well for a valid comparison.
jkrCommented:
>>i keep getting error "no conversion from int to char"??

Where exactly?
buzbomrAuthor Commented:
error occurs at: } while ( numeral [0] != ' ');
jkrCommented:
>>} while ( numeral [0] != ' ');

Hum, given that the array is of type "char", it should be OK - try

} while ( numeral [0] != (char) ' ');

to convince the compiler that we are talking about chars :o)
buzbomrAuthor Commented:
jkr:
this is my program so far and it doesnt work, it will compile and run with no errors but wont return the correct values, it only return the value for the given numeral not the sum of the numerals given????


#include <iostream>
using namespace std;
int main()
{
   int value,sum;
   char numeral [1024];
   sum=0;

   cout<<"enter numeral ";
do
{
   cin>>numeral;

   char num;
   int i = 0;
   while ( num = numeral[i++]) {
   switch (num) {
               
      case 'M':
                  value=1000;
                  sum=sum+value;
                  break;
      case 'D':
                  value=500;
                  sum=sum+value;
                  break;
      case 'C':
                  value=100;
                  sum=sum+value;
                  break;
      case 'L':
                  value=50;
                  sum=sum+value;
                  break;
      case 'X':
                  value=10;
                  sum=sum+value;
                  break;
      case 'V':
                  value=5;
                  sum=sum+value;
                  break;
      case 'I':
                  value=1;
                  sum=sum+value;
                  break;

   }
   }cout<<value;
} while ( numeral [0] != 'char');
return 0;
}
jkrCommented:
Um, you actually want to output

cout<<sum;

>>} while ( numeral [0] != 'char');

This should be

} while ( numeral [0] != (char) ' ');
buzbomrAuthor Commented:
even when cout<<sum; I still get an output of 102021 when i cin>>XXI
buzbomrAuthor Commented:
hey i got it! thanks a million jkr
jkrCommented:
:o)
BrettCollinsCommented:
the "cout<<sum" is in the wrong place, it is in the do loop so first exe of loop outputs 10, second 20 and third 21 resulting in 102021.

move the line to just before return statement.

Mustak_ShaikhCommented:
following prog will definetly give you the desire result

#include<iostream.h>
#include<conio.h>
int roman_convert(char const str);
void main()
{
      char roman[30],ch;
      int sum = 0, value = 0,i = 0;
      cout<<"Please enter the roman number"<<endl;
      cin>>roman;

      while(roman[i]!='\0')
      {
            value = roman_convert(roman[i]);
            if(roman[i+1] == '\0')
            {
                  sum += value;
                  break;
            }
            else
            {
                  if(value < roman_convert(roman[i+1]))
                        value = -value;
            }
            sum += value;
            i++;
      }

      cout<<sum<<endl;
      getch();
}

int roman_convert(char const str)
{
      int value = 0;
      switch(str)
      {
      case 'I':
            value = 1;
            break;
      case 'X':
            value = 10;
            break;
      case 'L':
            value = 50;
            break;
      case 'C':
            value = 100;
            break;
      case 'M':
            value = 1000;
            break;
      default:
            return 0;
      }
      return value;
}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.