Jump to content

building strings

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
restin84

restin84

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
hey guys

I'm working on some homework for my C++ programming class and the program has to convert a number between 1 and 3999 into Roman numerals. I have to build a string but I'm not quite sure how to code it. I'm cool with figuring out all the computation and what not but I just need a couple hints I guess. I wrote down 57 and then "L" + "VII" in my notes but I totally forgot how to implement it. I'll be doing some browsing online and in my book but any help would be appreciated.

#2
KeilanS

KeilanS

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 211 posts
Well, you know you need to use the string library, right?

From there, you could probably just output the "L" and then the "VII" without a space between them to make a Roman Numeral.

If that isn't good, I haven't tried this myself, but I believe you can just "add" strings.

For example:
str1 = "L";
str2 = "VII";
str3 = str1+str2;

That should give you: str3 = LVII.

Edit: I just tested this, and the connecting strings with "+" does work.

#3
restin84

restin84

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
cool thats defintately helpful. I got the skeleton of the program laid out nicely. I did include the string library as well. Haven't gotten to the string building thing yet but I'll let ya know how it works out

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You will also need to use modulo/integer division to capture individual digits.

myint/100 % 10 gives the 100's place
myint/10 % 10 gives the 10's place.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
KeilanS

KeilanS

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 211 posts
Wow winged, the way I've been doing that is so much more complicated.

For the hundreds I was doing things like ((int%1000)-10*(int%100) - (int%10))/100.

Thanks!

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Someday I might dig up my old base conversion program that used logs to determine the number of digits.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog