I'm currently enrolled in a ANSI C course at my college and i'm having an issue with a particular portion of the assignment...
The Overview:
The user needs to be able to input a 'y', 'Y', 'n', 'N' as a response to 5 questions, but my program must also be able to accept Yes, NO, YUP, or NOPE, and similar examples, as long as the first character in their response is y or n, in their variations. I'm currently using:
scanf(" %c", &response) to get their response.
Problem is, my instructor wants us to use the getc(stdin) or getchar(c).
Since i am very new to C, the "advance" terms are beyond me, as well as we are not allowed to use them. Either way, i don't undertand the use of or how to use getc or getchar, the example programs are unclear at times and don't necessarily coincide with my use of them. here the same of what i have so far, i just need some direction on how to use getc or getchar inplace of my scanf, i'll copy my program here, it's not all that big. I'm a noob at this so please be nice
-------------------------------------------------------------------------Code:--------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> #include <stdlib.h> /* Variables * * Patient Number - patNum * Patient Number (Left to Right) * First Digit - d1 * Second Digit - d2 * Thrid Digit - d3 * Fourth Digit - d4 * Fifth Digit - d5 * Sixth Digit - d6 * * Modulous Digit - modDig * Patient# Temp - temp * */ int patNum = 0, temp, lpCtrl, symptoms; int fever, headache, soreThroat, cough, sniffles, sneezes; int fCount, hCount, stCount, cCount, sniCount, sneCount; int d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0; int odd_d1, odd_d3, odd_d5, sum,modSum, divSum, check, check_1, count; char fever_rep, headache_rep, soreThroat_rep, cough_rep, sniffles_rep, sneezes_rep; main() { printf("Clark Hospital Diagnositic Center \n\n"); for(;;) /***/{ printf("\n Enter the patient number (0 to exit): "); scanf(" %i", &patNum); if(patNum == 0) { printf("\n User has ended program \n" " Program will now terminate"); exit(1); } temp = patNum; /* Patient Number Extraction */ d6 = temp % 10; temp = temp / 10; printf(" %i", d6); d5 = temp % 10; temp = temp / 10; printf(" %i", d5); d4 = temp % 10; temp = temp / 10; printf(" %i", d4); d3 = temp % 10; temp = temp / 10; printf(" %i", d3); d2 =temp % 10; temp = temp / 10; printf(" %i", d2); d1 = temp % 10; temp = temp / 10; printf(" %i", d1); /* Patient Number Verificaton */ odd_d1 = d1 * 2; odd_d3 = d3 * 2; odd_d5 = d5 * 2; if(odd_d1 > 10) odd_d1 = (odd_d1 % 10) + 1; if(odd_d3 > 10) odd_d3 = (odd_d3 % 10) + 1; if(odd_d5 > 10) odd_d5 = (odd_d5 % 10) + 1; sum = odd_d5 + odd_d3 + odd_d1 + d4 + d2; modSum = sum % 10; divSum = sum / 10; check = (divSum + 1) * 10; check_1 = check - sum; if(check_1 == d6) break; printf("\n The Patient Number %i is invalid \n", patNum); /***/} printf("\nWhich of the following symptoms does the patient have (Y for yes, N for no)\n\n"); /* Fever Input Response */ while(fCount == 0) { printf(" Fever -------- "); scanf(" %c", &fever_rep); if(fever_rep == 'Y' || fever_rep == 'y') { symptoms = symptoms + 1; fever = 1; fCount = 1; break; } if(fever_rep == 'N' || fever_rep == 'n') { fever = 0; fCount = 1; break; } if((fever_rep != 'Y' || fever_rep != 'y') || (fever_rep != 'N' || fever_rep != 'n')) { printf("\n Invalid Entry\n"); continue; } } /* Headache Input Response */ while(hCount == 0) { printf(" Headache ----- "); scanf(" %c", &headache_rep); if(headache_rep == 'Y' || headache_rep == 'y') { symptoms = symptoms + 1; headache = 1; hCount = 1; break; } if(headache_rep == 'N' || headache_rep == 'n') { headache = 0; hCount = 1; break; } if((headache_rep != 'Y' || headache_rep != 'y') || (headache_rep != 'N' || headache_rep != 'n')) { printf("\n Invalid Entry\n"); continue; } } /* Sore Throat Input Response */ while(stCount == 0) { printf(" Sore Throat -- "); scanf(" %c", &soreThroat_rep); if(soreThroat_rep == 'Y' || soreThroat_rep == 'y') { symptoms = symptoms + 1; soreThroat = 1; stCount = 1; break; } if(soreThroat_rep == 'N' || soreThroat_rep == 'n') { soreThroat = 0; stCount = 1; break; } if((soreThroat_rep != 'Y' || soreThroat_rep != 'y') || (soreThroat_rep != 'N' || soreThroat_rep != 'n')) { printf(" Invalid Entry\n"); continue; } } /* Cough Input Response */ while(cCount == 0) { printf(" Cough -------- "); scanf(" %c", &cough_rep); if(cough_rep == 'Y' || cough_rep == 'y') { symptoms = symptoms + 1; cough = 1; cCount = 1; break; } if(cough_rep == 'N' || cough_rep == 'n') { cough = 0; cCount = 1; break; } if((cough_rep != 'Y' || cough_rep != 'y') || (cough_rep != 'N' || cough_rep != 'n')) { printf("\n Invalid Entry\n"); continue; } } /* Sniffles Input Response */ while(sniCount == 0) { printf(" Sniffles ----- "); scanf(" %c", &sniffles_rep); if(sniffles_rep == 'Y' || sniffles_rep == 'y') { symptoms = symptoms + 1; sniffles = 1; sniCount = 1; break; } if(sniffles_rep == 'N' || sniffles_rep == 'n') { sniffles = 0; sniCount = 1; break; } if((sniffles_rep != 'Y' || sniffles_rep != 'y') || (sniffles_rep != 'N' || sniffles_rep != 'n')) { printf("\n Invalid Entry\n"); continue; } } /* Sneezes Input Response */ while(sneCount == 0) { printf(" Sneezes ------ "); scanf(" %c", &sneezes_rep); if(sneezes_rep == 'Y' || sneezes_rep == 'y') { symptoms = symptoms + 1; sneezes = 1; sneCount = 1; break; } if(sneezes_rep == 'N' || sneezes_rep == 'n') { sneezes = 0; sneCount = 1; break; } if((sneezes_rep != 'Y' || sneezes_rep != 'y') || (sneezes_rep != 'N' || sneezes_rep != 'n')) { printf("\n Invalid Entry\n"); continue; } } }



LinkBack URL
About LinkBacks



