Scan single character C -
/* program calculate trip , plan flights */ #define trip 6 #define dest 1 #include <stdio.h> int error_dest(int type_num, int cont_num, int dest_code, int check); int main(void) { int check, type_num, cont_num, index, i, dest_code, trip_num, row, col; int travelint[trip][dest], travelarea[trip]; char area_code, s, m, l, n, p, k, r, c, u, w, o; trip_num = 7; while (trip_num > trip) { printf("please enter number of trips:"); scanf("%d", &trip_num); if ( trip_num < trip) { printf("valid trip number. please proceed enter destination code.\n"); } else { printf("invalid trips. please enter no more 6 trips.\n"); } } /*********************************************************************************/ (i=0; < trip_num ; i++) /*destination code input*/ { printf("please enter destination code:"); scanf("%d", &dest_code); /*input of destination code*/ check = error_dest(type_num, cont_num, dest_code, check); if (check == 2) { travelint[i][0]=dest_code; } else { while (check == 1) { printf("please enter destination code:"); scanf("%d", &dest_code); /*input of destination code*/ check = error_dest(type_num, cont_num, dest_code, check); if (check == 2) { travelint[i][0]=dest_code; } } } printf("please select following best describes destination:\n"); printf("s small city - population under 50,000\n"); printf("m medium city - population between 50,000 , 500,000\n"); printf("l large city - pop. on 500,000\n"); printf("n natural formation mountain, lake, cave, geyser, fjord, canyon, etc.\n"); printf("p designated park or reserve such wildlife refuge, national park, bioreserve, or protected marine area\n"); printf("k man made landmark great wall of china, taj mahal, or stonehenge\n"); printf("r state or province or region of country\n"); printf("c whole country\n"); printf("u multiple countries traveling through europe\n"); printf("w ocean voyage\n"); printf("o other type of destination - such visiting sites of 7 wonders of world\n"); printf("please enter area letter code:"); scanf("%c", &area_code); } /*******************************************************************************/ /*print destination_code*/ (row = 0; row < trip_num; row++) { (col=0; col < dest; col++) printf("trip[%d] = %d\n", row+1, travelint[row][col]); } return 0; } error_dest(type_num, cont_num, dest_code, check) { cont_num = dest_code / 10000; /*math error check*/ type_num = dest_code/1000 - cont_num*10; if ( (cont_num <= 7) && (cont_num > 0) && (type_num <= 5) && (type_num >=0) ) { /* loop checking destination code*/ check = 2 ; return check; } else { printf("%d invalid code\n", dest_code); check = 1; return check; } }
for strange reason @ scanf("%c", &area_code); runs ahead , print dest_code array without letting me input character , i'm not sure doing wrong.
if you're looking grab 1 character, perhaps better use getchar() instead of scanf()?
Comments
Post a Comment