Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,187 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,085 people online right now. Registration is fast and FREE... Join Now!




C headache with characters

2 Pages V  1 2 >  
Reply to this topicStart new topic

C headache with characters, VII != IIV,

smartC
27 Jul, 2007 - 05:51 AM
Post #1

New D.I.C Head
*

Joined: 25 Jul, 2007
Posts: 18


My Contributions
:
:
:
:
switch(character){
case 'M':
cout<<sum += 1000;

break;
case 'D':

cout<<sum sum += 500;
:
:
:
:
:
case 'I':
cout<< sum += 1;
break;

}
This is the basic for for coverting Roman figures to decimal base 10. When I try VII I get same respond as IIV that is 7 or IX and XI as 11 whereas, IIV does not exist, neither is IX = 11. Any help? Test with an input such as MIX or IVC to be 1009 and 96 respectively. Urgent!! Email: smart_ikhu@hotmail.com
Thanks.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: C Headache With Characters
27 Jul, 2007 - 06:12 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Can you provide your full code? What is your intent with this line?
CODE

cout<<sum sum += 500;


User is offlineProfile CardPM
+Quote Post

smartC
RE: C Headache With Characters
27 Jul, 2007 - 06:22 AM
Post #3

New D.I.C Head
*

Joined: 25 Jul, 2007
Posts: 18


My Contributions
QUOTE(Amadeus @ 27 Jul, 2007 - 07:12 AM) *

Can you provide your full code? What is your intent with this line?
CODE

cout<<sum sum += 500;



That line was just a typo error. It is like others cout<<sum += 500;

I want to manipulate all the possibilities with Roman to Base 10 numbers. Examples, VI = 6 and IV = 4. With the above selection code, I get IV = VI or IX = XI. I want to make sure, I can have MIV = 1004 and not 1006 or IXM = 991 and not 1011.
Thanks. I am online waiting for your help.


The complete code is

while((c = fgetc(stdin)) != '\n'){

switch©{

case 'M':
sum += 1000;
printf("%d", sum);
break;

case 'M':
sum += 1000;
printf("%d", sum);
break;


case 'M':
sum += 1000;
printf("%d", sum);
break;

case 'D':
sum += 500;
printf("%d", sum);
break;


:
:
:
case 'V':
sum += 5;
printf("%d", sum);
break;
case 'I':
sum += 1;
printf("%d", sum);
break;

default:
break;

}

}


Please, I want IX = 11, and IX = 9.
Thanks.
SmartC
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: C Headache With Characters
27 Jul, 2007 - 07:41 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
The problem is that you are making your decisions based on one character at a time. There are situations in which you will have to check the NEXT character before making a decision on what to do with the previous character. the character following the original character will drive the decision, not the original character.
User is offlineProfile CardPM
+Quote Post

smartC
RE: C Headache With Characters
27 Jul, 2007 - 07:52 AM
Post #5

New D.I.C Head
*

Joined: 25 Jul, 2007
Posts: 18


My Contributions
QUOTE(Amadeus @ 27 Jul, 2007 - 08:41 AM) *

The problem is that you are making your decisions based on one character at a time. There are situations in which you will have to check the NEXT character before making a decision on what to do with the previous character. the character following the original character will drive the decision, not the original character.



So, how do I input next character? I need this help. Your idea sounds to be on track.
Thanks
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: C Headache With Characters
27 Jul, 2007 - 08:03 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
First, I'd ask what language you are using...your first code appears to be C++, the second is C. If you are using C++, I suggest getting the entire input as one string object and parsing it, or id C, a character array.
User is offlineProfile CardPM
+Quote Post

smartC
RE: C Headache With Characters
27 Jul, 2007 - 08:08 AM
Post #7

New D.I.C Head
*

Joined: 25 Jul, 2007
Posts: 18


My Contributions
QUOTE(Amadeus @ 27 Jul, 2007 - 09:03 AM) *

First, I'd ask what language you are using...your first code appears to be C++, the second is C. If you are using C++, I suggest getting the entire input as one string object and parsing it, or id C, a character array.



I want to use both languages to test my program. C appears to be more appealling for now. I will like to test it in C first.
Please, help in the C. C++ is not appealling in this case.
Thanks.
SmartC.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: C Headache With Characters
27 Jul, 2007 - 08:31 AM
Post #8

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Not sure what you mean by appealing, but it could be accomplished in C++ much easier.

c it is...

Can you modify your program to take the user input as a single string (character array)?
User is offlineProfile CardPM
+Quote Post

smartC
RE: C Headache With Characters
27 Jul, 2007 - 12:16 PM
Post #9

New D.I.C Head
*

Joined: 25 Jul, 2007
Posts: 18


My Contributions
QUOTE(smartC @ 27 Jul, 2007 - 09:08 AM) *

QUOTE(Amadeus @ 27 Jul, 2007 - 09:03 AM) *

First, I'd ask what language you are using...your first code appears to be C++, the second is C. If you are using C++, I suggest getting the entire input as one string object and parsing it, or id C, a character array.



I want to use both languages to test my program. C appears to be more appealling for now. I will like to test it in C first.
Please, help in the C. C++ is not appealling in this case.
Thanks.
SmartC.


That is like char array1[3] = "IV"; char array2[3] = "IX"; char array3[3] = "XL"; etc, for the special cases, but when I thought along this line, it is another up hill task since so many variables will be involved. I beleive that your idea is radiating a possible solution. Specifically, I am using C. Thanks thus far.
SmartC.


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: C Headache With Characters
27 Jul, 2007 - 05:12 PM
Post #10

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
I was thinking more along the lines of:
CODE

char str1[10];
printf("Please enter a Roman Numeral\n");
scanf("%s",str1);
printf("The string you entered was %s, and it has %d characters.\n",str1,strlen(str1));

You now have the roman numeral in a single character array, and can check any of the characters, including the next one.
User is offlineProfile CardPM
+Quote Post

smartC
RE: C Headache With Characters
28 Jul, 2007 - 02:14 AM
Post #11

New D.I.C Head
*

Joined: 25 Jul, 2007
Posts: 18


My Contributions
QUOTE(Amadeus @ 27 Jul, 2007 - 06:12 PM) *

I was thinking more along the lines of:
CODE

char str1[10];
printf("Please enter a Roman Numeral\n");
scanf("%s",str1);
printf("The string you entered was %s, and it has %d characters.\n",str1,strlen(str1));

You now have the roman numeral in a single character array, and can check any of the characters, including the next one.



Ok,
I have this codes, but it is in the reverse. From Decimal to Roman. Could you be kind enough to make it perform the other way round. From Roman to decimal? I will be glad you do. Thanks.
smartC

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>



unsigned int roman_ranges[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4,

1, 0 };
unsigned int roman_addsin[] = {-1000, +100, -500, +100, -100, +10, -50, +10, -10, +1, -5, +1,

-1, 0 };
char roman_letters[] = { 'M', 'C', 'D', 'C', 'C', 'X', 'L', 'X', 'X', 'I', 'V', 'I',

'I', 0 };

size_t find_index(unsigned int li) {
unsigned int *roman_ranges_ptr = roman_ranges;
while (*roman_ranges_ptr && li < *roman_ranges_ptr) {
++roman_ranges_ptr;
}
return roman_ranges_ptr - roman_ranges;
}



char* de2roman(char* buf, unsigned int li, size_t bufsize) {
size_t index;
if (bufsize < 1) {
return NULL; // buffer size is not enough for convertion
}
if ( (*buf = roman_letters[ (index = find_index(li)) ]) ) {
return de2roman(buf+1, li + roman_addsin[index], bufsize-1);
}
return buf;
}

#define ROMAN_NUMBER_BUF_SIZE 64

int main() {
unsigned int nr;
char roman_str[ROMAN_NUMBER_BUF_SIZE];

/* printf("%s", "Enter an integer: ");
scanf("%s", roman_str);*/
// while((nr = fgetc(stdin)) != '\n'){


while((scanf("%s", roman_str)) != '\n'){

de2roman(roman_str, nr, ROMAN_NUMBER_BUF_SIZE);
printf(" %d\n", nr);

}
return 0;
}

This post has been edited by smartC: 28 Jul, 2007 - 02:16 AM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: C Headache With Characters
28 Jul, 2007 - 02:22 AM
Post #12

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
you already have the solution in the code above, try to think this through.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/2/08 01:15AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month