Code skips data entry of char input 1st loop of do while

This is a discussion on Code skips data entry of char input 1st loop of do while within the C Programming forums, part of the General Programming Boards category; I thought I would use a do-while because it always loops once anyway After a numberic value is entered I ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    Code skips data entry of char input 1st loop of do while

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

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    FAQ.

    The newline is still in the standard input buffer from when you pressed enter to send the input.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The issue here is that you are scanning for the %lf value on the lines before and you hitting enter leaves the '\n' in the buffer, which the next scanf call picks up as an entry.

    So you'll need to clear the input buffer.

    You can use a while loop to do it in-between like this:

    Code:
    while( getchar() != '\n' );

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    Do I insert this before the do statement or inside at the top of the do while?

    Thanks
    Jack

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    Thanks for the help! I place it above the do statment it worked fine.

    I am new to c programming and I am taking my first intro to C class
    this was not covered in any of my materials yet.

    I appreciate the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21