All pastes #876903 Raw Edit

C exercise

public text v1 · immutable
#876903 ·published 2008-01-29 04:31 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
		digit[i] = strtol(&c, &pEnd, 0);
		//strcpy(digit[i], &c);		
		i++;
	
	}
	// count how many actual digits were entered into digit
	i = 0;
	n = 0;
	while((int)digit[i] != 0 ){  // && digit[i] != NULL
		printf("This char is %c and digit is %d\n", digit[i], digit[i]);
		n++; // N will contain the number of digits entered
		i++;
	}	
	
	// do arithmatic to compile the number needed.
	//c = NULL;
	printf("Number of digits is %d.\n", n);

	c = 0;
	i = 1;
	factor = 1;
	for(; n != 0; --n){
		c += (digit[(n - 1)] * (int)factor);
		printf("DEBUG: c is now (int) %d and (char) %c.\n", c, c);
		factor = factor * 10;
		i++;

	}
	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);
}