Solved

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

Posted on 2003-10-23
24
503 Views
Last Modified: 2010-04-01
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;
}
0
Comment
Question by:buzbomr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 5
  • +3
24 Comments
 

Author Comment

by:buzbomr
ID: 9611541
nevermind i firgured that part out....  
0
 

Author Comment

by:buzbomr
ID: 9611558
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;
}
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 9611574


  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.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:burcarpat
ID: 9611594

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
0
 

Author Comment

by:buzbomr
ID: 9611598
output is still 102021 if you change sum to sum=sum+value
0
 

Author Comment

by:buzbomr
ID: 9611609
i dont know: #include <numeric>   #include <string>   or   #include <map>
need to keep things simple
0
 
LVL 86

Expert Comment

by:jkr
ID: 9611618
>>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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 9611627
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] != ' ');

}
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 9611633


try initializing sum to 0.  When you declare a local variable, depending on the compiler, it may be filled with garbage.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 9611642
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9611655
>>find out please stop throwing up completed solutions

Um, the approach was *cough* close already :o)
0
 

Author Comment

by:buzbomr
ID: 9611686
i keep getting error "no conversion from int to char"??
any suggestions as to why?
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 9611687
LOL, yeah, I guess, wonder if he forgot about the IX or if that's not part of the assignment.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 9611696
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9611736
>>i keep getting error "no conversion from int to char"??

Where exactly?
0
 

Author Comment

by:buzbomr
ID: 9611800
error occurs at: } while ( numeral [0] != ' ');
0
 
LVL 86

Expert Comment

by:jkr
ID: 9611818
>>} 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)
0
 

Author Comment

by:buzbomr
ID: 9611860
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;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 9611870
Um, you actually want to output

cout<<sum;

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

This should be

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

Author Comment

by:buzbomr
ID: 9611879
even when cout<<sum; I still get an output of 102021 when i cin>>XXI
0
 

Author Comment

by:buzbomr
ID: 9611881
hey i got it! thanks a million jkr
0
 
LVL 86

Expert Comment

by:jkr
ID: 9611898
:o)
0
 

Expert Comment

by:BrettCollins
ID: 9614196
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.

0
 
LVL 1

Expert Comment

by:Mustak_Shaikh
ID: 9616582
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;
}
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to convert c++ code to Android App 3 131
GUI: DIalog Stacking and Popping in MS C++ 4 88
How to gracefully close the c++ 11 thread? 3 122
c++, dynamic object by json 1 60
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question