Changing integer input into string, wrong value

This is a discussion on Changing integer input into string, wrong value within the C Programming forums, part of the General Programming Boards category; I'm creating a code to transform integers into roman numerals. I thought about attempting to use getchar() to read each ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    Changing integer input into string, wrong value

    I'm creating a code to transform integers into roman numerals. I thought about attempting to use getchar() to read each number but then decided to try to use scanf and use algebraic means to break the number into components for a string. Below is the code, which seems to work fine except for this error. I commented in the code several values that return wrong.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define MAXNUM 4
    
    int main() {
    
    	double input, fraction, integer;
    	int i, j;
    	int b10_num[MAXNUM];
    	char *N_1000[] = {"", "M", "MM"};
    	char *N_100[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
    	char *N_10[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
    	char *N_1[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
    	
    	while(1) {
    		printf("\nEnter a number (between 1 and 3999): ");
    		scanf("%lf", &input);
    		if (input < 4000 && input > 0 && modf(input, &integer) == 0) 
    			break;
    		else {
    			printf("Error: number is invalid");
    			continue;
    		}	
    	}
    	//transforming into string
    	/* ERROR LIST
    	  1551 returns 1550
    	  1552 returns 1551
    	  1556 returns 1555
    	  1557 returns 1556
    	  1560 returns 1550
    	*/
    	for(i = 0; i < MAXNUM; i++) {
    		input = input / 10;
    		printf("input: %g\t", input);
    		fraction = modf(input, &integer) * 10;
    		b10_num[i] =  (int)fraction;
    		printf("fraction: %lf\t b10_num[%d]: %d\n", fraction, i, b10_num[i]);
    	}
    	input *= pow(10,MAXNUM);
    	//result
    	printf("Digits of the number: ");
    		for(j = 3; j >= 0; j--) {
    			printf("%d,", b10_num[j]);
    		}
    	printf("\nThe number %g as Roman numeral: %s%s%s%s\n", input, 
    		N_1000[b10_num[3]], N_100[b10_num[2]], N_10[b10_num[1]], N_1[b10_num[0]]);
    
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you mixing floating point math with your integers? All you need to do here (going from what you look to be doing) is keep dividing by 10, and use the integer mod operator: %
    Code:
    x = y % 10; /* put the remainder of y / 10 into x */

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    Thank you for the help. It didn't occur to me to use the remainder operator. My first thought was modf. I ended up reducing the transformation part to:
    Code:
    for(i = 0; i < MAXNUM; i++) {
    	b10_num[i] =  (input % (int)pow(10, i + 1)) / (int)pow(10, i);
    }

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by Ace2187 View Post
    Thank you for the help. It didn't occur to me to use the remainder operator. My first thought was modf. I ended up reducing the transformation part to:
    Code:
    for(i = 0; i < MAXNUM; i++) {
    	b10_num[i] =  (input % (int)pow(10, i + 1)) / (int)pow(10, i);
    }
    Oh gosh. As bad as using modf for this was, using two calls to pow is even worse (in terms of efficiency)! What quzah suggested was the best way to do it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21