I thought I would use a do-while because it always loops once anyway
After a numberic value is entered I thought it would prompt for the char entry.
But it is falling all the the way thru; failing and asking a second time giving the correct results.
I would like it to stop on the first go round and get the char input.
What am I doing wrong on this? Am I not understanding the do while process properly?
Code:/* * Convert Metric Prefix to numeric value */ #include <stdio.h> #include <math.h> int main(void) { double value, result, exitvalue; // stores numerical value exitvalue=0, value=0, result=0; // intitialize variables char prefix; // stores letter value of prefix // ask for and get numeric value printf("Please enter numeric value to convert:"); scanf("%lf", &value ); //test print out of value and power printf("the value entered is:%f \n\n", value); result = value*pow(10,9); printf("the result is:%f \n\n", result); do { // ask for prefix printf("Please enter the prefix in the following manner G,M,k,m,u,n,p: \n"); printf("Please enter the prefix you wish to convert:\n "); scanf("%c", &prefix); // evaluate prefix with switch switch (prefix) { case 'G' : result = value*pow(10,9); printf("The conversion is %f\n", result); exitvalue=1; break; case 'M' : result = value*pow(10,6); printf("The conversion is %f\n", result); exitvalue=1; break; case 'k' : result = value*pow(10,3); printf("The conversion is %f\n", result); exitvalue=1; break; case 'm' : result = value*pow(10,-3); printf("The conversion is %f\n", result); exitvalue=1; break; case 'u' : result = value*pow(10,-6); printf("The conversion is %f\n", result); exitvalue=1; break; case 'n' : result = value*pow(10,-9); printf("The conversion is %f", result); exitvalue=1; break; case 'p' : result = value*pow(10,-12); printf("The conversion is %f\n", result); exitvalue=1; break; default : exitvalue=0; printf("Please rerun, Invalid entry.\n"); } } while(exitvalue==0); return (0); }



LinkBack URL
About LinkBacks


