14 Replies - 972 Views - Last Post: 17 August 2008 - 09:55 PM Rate Topic: -----

#1 gabehabe  Icon User is online

  • GabehabeSwamp
  • member icon




Reputation: 1346
  • View blog
  • Posts: 10,918
  • Joined: 06-February 08

String conversion

Posted 09 February 2008 - 10:11 AM

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?

Is This A Good Question/Topic? 1

Replies To: String conversion

#2 schnalf  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 129
  • Joined: 09-February 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 troy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-August 08

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..
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,032
  • Joined: 14-September 07

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?
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

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.
Was This Post Helpful? 0
  • +
  • -

#6 gabehabe  Icon User is online

  • GabehabeSwamp
  • member icon




Reputation: 1346
  • View blog
  • Posts: 10,918
  • Joined: 06-February 08

Re: String conversion

Posted 16 August 2008 - 08:29 AM

Holy crap, this was my first post!!!

:blush: What an embarassing question.

And Troy, Nick is right~ we won't give you code. Particularly if you email us and request it. >:(
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • su wtf -am -i /doing/with/my/life
  • member icon

Reputation: 2979
  • View blog
  • Posts: 19,032
  • Joined: 14-September 07

Re: String conversion

Posted 16 August 2008 - 09:47 AM

shit, I didnt notice it was necroed ;)

HAH gabe :P
Was This Post Helpful? 0
  • +
  • -

#8 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: String conversion

Posted 16 August 2008 - 01:42 PM

Gabe, did you find the answer to your question? What was the answer?
Was This Post Helpful? 0
  • +
  • -

#9 troy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-August 08

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

#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

Was This Post Helpful? 0
  • +
  • -

#10 gabehabe  Icon User is online

  • GabehabeSwamp
  • member icon




Reputation: 1346
  • View blog
  • Posts: 10,918
  • Joined: 06-February 08

Re: String conversion

Posted 17 August 2008 - 06:53 AM

Yeah, I did it, but I don't have it any more :lol:

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. :)
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

Re: String conversion

Posted 17 August 2008 - 08:45 AM

you mean something like this.
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Whizzy*


Reputation:

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.
Was This Post Helpful? 0

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2209
  • View blog
  • Posts: 9,183
  • Joined: 18-February 07

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.
Was This Post Helpful? 0
  • +
  • -

#14 Guest_Whizzy*


Reputation:

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.
Was This Post Helpful? 0

#15 troy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-August 08

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...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1