Join 149,472 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,924 people online right now. Registration is fast and FREE... Join Now!
At this point I am only looking for help in determing which string function I should to take information entered from a user and to put it in a string. I need the string to only accept the letters that represent roman numerals (I, V, X, L, C, D, M) and exclude all other values. I would like to use the fgets function but I cannot figure out how to get to exclude all the other letters. If I cannot use the fgets function I am assuming I would use the scanf function to read and store until it reaches an invalide character or the end of the line. Is this correct?
Any help would be great. I have not wrote any code yet since I am not sure where to start at this point.
// M =1000, D = 500 , C = 100 , L = 50 , X = 10 , V = 5 , I = 1 ; /* Program by K.Sri Hari */ /* Please mail your opinions to me */
int num,temp,a,i ;
clrscr() ; printf ( " \n Enter the Number " ) ; scanf ( "%u", &num ) ; temp = num ;
while( temp <= 0 || temp >= 5000) { printf ( "\n The Number cannot be ROMANIZED" ) ; getch() ; exit(0) ; }
printf ( "\n The Roman letter Representation is : " ) ; if ( num >= 1000 ) { a = num / 1000 ; for ( i = 0 ; i < a ; i++ ) printf ( "M" ) ; num = num - ( a* 1000 ) ; }
if ( num>=500 ) { if ( num>=900 ) { printf( "CM" ) ; num = num -900 ; } else { printf ("D" ) ; num = num - 500 ; } }
if ( num >= 100 ) { a = num / 100 ; for ( i = 0 ; i < a ; i++ ) printf ( "C" ) ; num = num - ( a* 100 ) ; }
if ( num >= 50 ) { if ( num >= 90 ) { printf ( "XC" ) ; num = num - 90 ; }
else { printf ( "L" ) ; num = num - 50 ; } }
if ( num >= 10 ) { a = num / 10 ; for ( i = 0 ; i < a ; i++ ) printf ( "X" ); num = num - (a*10) ; }
if ( num >= 5 ) { if ( num >= 9 ) { printf ( "IX" ) ; num = num - 9 ; }
else { printf ( "V" ) ; num = num - 5 ; } }
if ( num >= 1 ) { a = num / 1 ; for ( i = 0 ; i < a ; i++ ) printf ( "I" ); num = num - (a*1) ; }
getch() ; }
First of all, I think it's d.i.c. policy to not write do other people's work for them. Also, if I'm not mistaken, the question is Roman system --> decimal system, not the other way around.
// M =1000, D = 500 , C = 100 , L = 50 , X = 10 , V = 5 , I = 1 ;
}
First of all, I think it's d.i.c. policy to not write do other people's work for them. Also, if I'm not mistaken, the question is Roman system --> decimal system, not the other way around.
OK give me some time let me try to write the ROMAN System --> decimal
This post has been edited by selloorhari: 7 Feb, 2008 - 06:58 AM
First of all, I think it's d.i.c. policy to not write do other people's work for them. Also, if I'm not mistaken, the question is Roman system --> decimal system, not the other way around.
OK give me some time let me try to write the ROMAN System --> decimal
Didn't you read the first remark?? You're NOT SUPPOSED TO GIVE THEM THE CODE. It's against d.i.c. policy!
First of all, I think it's d.i.c. policy to not write do other people's work for them. Also, if I'm not mistaken, the question is Roman system --> decimal system, not the other way around.
OK give me some time let me try to write the ROMAN System --> decimal
Didn't you read the first remark?? You're NOT SUPPOSED TO GIVE THEM THE CODE. It's against d.i.c. policy!
OK SORRY FOR THAT.. HERE AFTER I WON'T thanks for reminding my mistake...
Thanks for the help so far. At this point I have gotten most of it writtend but I am jammed. I am able to read the roman numeral and input it as a string9which is required for the assignment). After that I took the appropriate values and added them to a parallel array(also required for the assignment. I.E. if the value of str[0] is V then ary[0] is 5. My problem is the conversion. I cannot get it to properly convert all the time. The code I have below will convert correctly unless there is two or more consecutive numbers that are the same and the number following them is larger. For example IV converts to 4 but IIV converts to V instead of 3. let me know if anyone can help with this part.
int main (void) { char str[20]; int ary[20]; int check; int count; int decimal;
while (true) { count = 0; decimal = 0; for (int i = 0; i < 20; i++) { str[i] = NULL; ary[i] = 0; }
printf ("Please enter a Roman Numeral of up to 20 characters to be converted\n"); printf ("to an Integer value.\n"); fgets(str, sizeof (str), stdin); check = strspn(str, "IVXLCDM"); count = strlen (str);
if (check != count - 1) { printf("Your input is not a Roman Numeral.\n"); printf("Roman Numerals only contain the following case sensitive characters:\n"); printf("I,V,X,L,C,D,M\n"); printf("All other characters are invalid including the lower case equivilants.\n"); printf("Please try again by entering a properly formatted Roman Numeral\n\n\n\n"); } else {
for (int i = 0; i < count - 1; i++) { if (str[i] == 'I') ary[i] = 1; if (str[i] == 'V') ary[i] = 5; if (str[i] == 'X') ary[i] = 10; if (str[i] == 'L') ary[i] = 50; if (str[i] == 'C') ary[i] = 100; if (str[i] == 'D') ary[i] = 500; if (str[i] == 'M') ary[i] = 1000; }
for (int i = 0; i < count - 1; i++) {
if (ary[i] >= ary[i + 1]) decimal += ary[i]; if (ary[i] < ary[i + 1]) decimal -= ary[i];
} printf("The value of your Roman Numeral is %d", decimal); printf("%\n\n"); } } }
Thanks for the help so far. At this point I have gotten most of it writtend but I am jammed. I am able to read the roman numeral and input it as a string9which is required for the assignment). After that I took the appropriate values and added them to a parallel array(also required for the assignment. I.E. if the value of str[0] is V then ary[0] is 5. My problem is the conversion. I cannot get it to properly convert all the time. The code I have below will convert correctly unless there is two or more consecutive numbers that are the same and the number following them is larger. For example IV converts to 4 but IIV converts to V instead of 3. let me know if anyone can help with this part.
int main (void) { char str[20]; int ary[20]; int check; int count; int decimal;
while (true) { count = 0; decimal = 0; for (int i = 0; i < 20; i++) { str[i] = NULL; ary[i] = 0; }
printf ("Please enter a Roman Numeral of up to 20 characters to be converted\n"); printf ("to an Integer value.\n"); fgets(str, sizeof (str), stdin); check = strspn(str, "IVXLCDM"); count = strlen (str);
if (check != count - 1) { printf("Your input is not a Roman Numeral.\n"); printf("Roman Numerals only contain the following case sensitive characters:\n"); printf("I,V,X,L,C,D,M\n"); printf("All other characters are invalid including the lower case equivilants.\n"); printf("Please try again by entering a properly formatted Roman Numeral\n\n\n\n"); } else {
for (int i = 0; i < count - 1; i++) { if (str[i] == 'I') ary[i] = 1; if (str[i] == 'V') ary[i] = 5; if (str[i] == 'X') ary[i] = 10; if (str[i] == 'L') ary[i] = 50; if (str[i] == 'C') ary[i] = 100; if (str[i] == 'D') ary[i] = 500; if (str[i] == 'M') ary[i] = 1000; }
for (int i = 0; i < count - 1; i++) {
if (ary[i] >= ary[i + 1]) decimal += ary[i]; if (ary[i] < ary[i + 1]) decimal -= ary[i];
} printf("The value of your Roman Numeral is %d", decimal); printf("%\n\n"); } } }