Solved

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

Posted on 2003-10-23
24
500 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
  • 9
  • 7
  • 5
  • +3
24 Comments
 

Author Comment

by:buzbomr
Comment Utility
nevermind i firgured that part out....  
0
 

Author Comment

by:buzbomr
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility


  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
 
LVL 7

Expert Comment

by:burcarpat
Comment Utility

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
Comment Utility
output is still 102021 if you change sum to sum=sum+value
0
 

Author Comment

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

Expert Comment

by:jkr
Comment Utility
>>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
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility


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

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
>>find out please stop throwing up completed solutions

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

Author Comment

by:buzbomr
Comment Utility
i keep getting error "no conversion from int to char"??
any suggestions as to why?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 39

Expert Comment

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

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
>>i keep getting error "no conversion from int to char"??

Where exactly?
0
 

Author Comment

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

Expert Comment

by:jkr
Comment Utility
>>} 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
Comment Utility
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
Comment Utility
Um, you actually want to output

cout<<sum;

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

This should be

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

Author Comment

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

Author Comment

by:buzbomr
Comment Utility
hey i got it! thanks a million jkr
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
:o)
0
 

Expert Comment

by:BrettCollins
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now