Im trying to make a program that converts a number to roman numerals. Ive got it all figured out, but Im stuck on how to take each character from the inputted string individually?
Ive declared the string as an array of characters, and created pointers to each of the characters. Ive also used atoi to convert each character to a number.
The problem is the pointers. If I point to char string [0], it doesnt just point to that character, it points to that character and all others that follow it. How can I take one character at a time instead of this?
String conversion
Page 1 of 114 Replies - 972 Views - Last Post: 17 August 2008 - 09:55 PM
Replies To: String conversion
#2
Re: String conversion
Posted 09 February 2008 - 10:22 AM
hi,
why do you write your own function to convert it? you have only to look up the number in the ascii table and change it.
e.g. if string[0] == 1, you have to change the value of string[0] to 49.
why do you write your own function to convert it? you have only to look up the number in the ascii table and change it.
e.g. if string[0] == 1, you have to change the value of string[0] to 49.
#3
Re: String conversion
Posted 16 August 2008 - 07:35 AM
hi do you still have the code in converting a number into a roman numeral?.... im studying c right now and i rily dont have any idea to make that program.. its my project in our skul for midterm =(.. can you help me about this program? please?... can you send me the code if it is ok... thanks alot..
#4
Re: String conversion
Posted 16 August 2008 - 07:52 AM
How about one pointer to the first element and pointer arithmetic to access the other elements in the array?
#5
Re: String conversion
Posted 16 August 2008 - 08:18 AM
@troy -- Please read the forum rules. Asking a moderator to send you his code is probably not a good idea! The site has a "no cheating" polocy which forbids us from just sending you code. We can help you get your code working but until you show some effort we can't send you code.
That being said, when you are in the C++ forum you will notices a search box at the top. Since this is a common homework assignment you will find posts form others who had troubles getting this to work. Type in "Roman Numerals" and see what comes up. There was a pretty good discussion on that topic a few weeks ago.
That being said, when you are in the C++ forum you will notices a search box at the top. Since this is a common homework assignment you will find posts form others who had troubles getting this to work. Type in "Roman Numerals" and see what comes up. There was a pretty good discussion on that topic a few weeks ago.
#6
Re: String conversion
Posted 16 August 2008 - 08:29 AM
Holy crap, this was my first post!!!
What an embarassing question.
And Troy, Nick is right~ we won't give you code. Particularly if you email us and request it. >
And Troy, Nick is right~ we won't give you code. Particularly if you email us and request it. >
#7
Re: String conversion
Posted 16 August 2008 - 09:47 AM
shit, I didnt notice it was necroed 
HAH gabe
HAH gabe
#8
Re: String conversion
Posted 16 August 2008 - 01:42 PM
Gabe, did you find the answer to your question? What was the answer?
#9
Re: String conversion
Posted 16 August 2008 - 11:41 PM
im sorry hehe..
hirs my code.. its not working dunno how to debug it..im new in c..
hirs my code.. its not working dunno how to debug it..im new in c..
#include<stdio.h>
main()
{
int num;
int convert (int num);
while (1)
{
printf("Enter the number(1-1000) that you wanna convert to Roman numeral: " );
scanf ("%d", &num);
convert(num);
if (num> 1001)
{
printf("Invalid Number.Please enter again.\n\n");
}
}
}
convert(int num);
{
int i;
{
i=(num/1000); //thousands place
if(i==1)
{
printf("m");
}
i=(num%1000)-(year/1000); //hundreds place
switch (i)
{
case 1:{
printf("c");
break;}
case 2:{
printf("cc");
break;}
case 3:{
printf("ccc");
break;}
case 4:{
printf("cd");
break;}
case 5:{
printf("d");
break;}
case 6:{
printf("dc");
break;}
case 7:{
printf("dcc");
break;}
case 8:{
printf("dccc");
break;}
case 9:{
printf("cm");
break;}
}
i=(year-((year/100)*100))/10; //tens place
switch(i)
{
case 1:{
printf("x");
break;}
case 2:{
printf("xx");
break;}
case 3:{
printf("xxx");
break;}
case 4:{
printf("xl");
break;}
case 5:{
printf("l");
break;}
case 6:{
printf("lx");
break;}
case 7:{
printf("xll");
break;}
case 8:{
printf("xlll");
break;}
case 9:{
printf("xc");
break;}
}
i=num%10; //ones place
switch(i);
{
case 1:{
printf("i");
break;}
case 2:{
printf("ii");
break;}
case 3:{
printf("iii");
break;}
case 4:{
printf("iv");
break;}
case 5:{
printf("v");
break;}
case 6:{
printf("vi");
break;}
case 7:{
printf("vii");
break;}
case 8:{
printf("viii");
break;}
case 9:{
printf("ix");
break;}
}
}
return 0;
}
This post has been edited by troy: 16 August 2008 - 11:45 PM
#10
Re: String conversion
Posted 17 August 2008 - 06:53 AM
Yeah, I did it, but I don't have it any more 
I'll write one as a snippet tomorrow.
Basically, you could use some modulus magic to split a number up into single digits, and convert them into roman numerals, which are stored within an array.
I'll write one as a snippet tomorrow.
Basically, you could use some modulus magic to split a number up into single digits, and convert them into roman numerals, which are stored within an array.
#12 Guest_Whizzy*
Re: String conversion
Posted 17 August 2008 - 08:48 AM
Well, I don't know all this converting this over and that over stuff, but I do have my own little hack/slash way of getting things done.
It seems to me, that you don't need pointers, and all those hi-tech conversions all the time.
The basic numbers you need to convert are:
Traditional 1 5 10 50 100 500 1000
Roman I V X L C D M
Ok, why can't you do it with seven little functions?
Consider:
cin num
while num>1{
if num>1000
grand(num)
a=a+"M"
else
if num>500
fivehun(num)
a=a+"D"
else
if num>100
hundred(num)
a=a+"C"
else
if num>50
fifty(num)
a=a+"L"
else
if num>10
ten(num)
a=a+"X"
else
if num>5
five(num)
a=a+"V"
else
if num>1
one(num)
a=a+"I"
cout << "Your conversion is "<< a
And you functions would simply subtract the value it is dealing with from num, and return the remainder.
I don't see why it wouldn't work.
It seems to me, that you don't need pointers, and all those hi-tech conversions all the time.
The basic numbers you need to convert are:
Traditional 1 5 10 50 100 500 1000
Roman I V X L C D M
Ok, why can't you do it with seven little functions?
Consider:
cin num
while num>1{
if num>1000
grand(num)
a=a+"M"
else
if num>500
fivehun(num)
a=a+"D"
else
if num>100
hundred(num)
a=a+"C"
else
if num>50
fifty(num)
a=a+"L"
else
if num>10
ten(num)
a=a+"X"
else
if num>5
five(num)
a=a+"V"
else
if num>1
one(num)
a=a+"I"
cout << "Your conversion is "<< a
And you functions would simply subtract the value it is dealing with from num, and return the remainder.
I don't see why it wouldn't work.
#13
Re: String conversion
Posted 17 August 2008 - 09:02 AM
Well the basic problem with your pseudo code above is that it does not loop. I am assuming that grand(num) := num -= 1000; and so on and so forth for the rest of the functions.
Basically your logic is sort of correct. There are a few kinks.
#1 You code could not deal with number greater than 3999 so you need to check to ensure you don't try. (saying "hey the user should not enter a value that large" is just unacceptable because IF the user did, you would give a wrong answer).
#2 You need to loop.
#3 -- what about 9 as IX and 4 as IV.. your program would dislay VIIII and IIII respectively. Generally this is the *catch* in the roman numerals assignment.
The approach is valid, but needs some work.
Basically your logic is sort of correct. There are a few kinks.
#1 You code could not deal with number greater than 3999 so you need to check to ensure you don't try. (saying "hey the user should not enter a value that large" is just unacceptable because IF the user did, you would give a wrong answer).
#2 You need to loop.
#3 -- what about 9 as IX and 4 as IV.. your program would dislay VIIII and IIII respectively. Generally this is the *catch* in the roman numerals assignment.
The approach is valid, but needs some work.
#14 Guest_Whizzy*
Re: String conversion
Posted 17 August 2008 - 09:14 AM
--
V
Larger numbers were indicated by putting a horizontal line over them, which meant to multiply the number by 1,000. So to indicate 5,000, you would put a V with a line on top. It is my understanding however, that this usage is no longer current, because the largest numbers usually expressed in the Roman system are dates.
I put a while statement to indicate it needed to loop. But you are correct in the fact that odd numbers such 4,6,7,8 and 9 where not represented. I see your point.
V
Larger numbers were indicated by putting a horizontal line over them, which meant to multiply the number by 1,000. So to indicate 5,000, you would put a V with a line on top. It is my understanding however, that this usage is no longer current, because the largest numbers usually expressed in the Roman system are dates.
I put a while statement to indicate it needed to loop. But you are correct in the fact that odd numbers such 4,6,7,8 and 9 where not represented. I see your point.
#15
Re: String conversion
Posted 17 August 2008 - 09:55 PM
i dont get it... huhu =(
what should i do when i will input "3" the equivalent should be III...
ex. output..
enter num: 3
equivalent is: III
whew when i input 1 5 10 50 ... 1000 yah it works but when i input 3 it will print I instead of III...
what should i do when i will input "3" the equivalent should be III...
ex. output..
enter num: 3
equivalent is: III
whew when i input 1 5 10 50 ... 1000 yah it works but when i input 3 it will print I instead of III...
Page 1 of 1
|
|

New Topic/Question
Reply






MultiQuote








|