All pastes #876907 Raw Edit

C exercise

public text v1 · immutable
#876907 ·published 2008-01-29 04:39 UTC
rendered paste body
/*
 *  Exercise1.3.c
 *  
 *
 *
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int getdig(void){
	char * pEnd;
	int i, c, n, factor,  digit[9] = { 0 };
	
	i = 0;
	
	printf("Enter Digits.\n");
	
	while((c = getchar()) != '\n' && i != 9){
		// c is now the int cast of the unsigned char that getchar returned
                if( c >= '0' && c <= '9') {
                        digit[i++] = c - '0';
                } else {
                        /* do something.. they entered something that wasn't a digit */
                }
	}
        n = i;
	c = 0;
	factor = 1;
	for(i=n-1; i!=0; i--){
		c += (digit[i] * (int)factor);
		printf("DEBUG: c is now (int) %d and (char) %c.\n", c, c);
		factor *= 10;
	}
	printf("Is your number %c, or %d?\n", c, c);
	printf("Number of digits is %d.\n", n);
	
	return 0;
}

int main(void){
	int got;
	got = getdig();
	
	printf("Your number is: %d\n",got);
}