scanf trouble

This is a discussion on scanf trouble within the C++ Programming forums, part of the General Programming Boards category; I'm trying to read a character in from the keyboard, however, the first time I try to read data of ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    scanf trouble

    I'm trying to read a character in from the keyboard, however, the first time I try to read data of type "char" it ignores the comand. There are other scanf's previous to the first char-attempt for inputting doubles. Here is some code.

    Code:
    #include <stdio.h>
    
    //prototypes
    double ends(char, double*, double*);
    
    int main(void){
    	//this will be used to define the endpoints of the calculations
    	char ch='p';
    	double a;
    	double rightend, leftend, step;
    
    
    	//defining the inputs
    	printf("Enter the lattice constant, a, then hit enter: ");
    	scanf("%lf", &a); //this step reads the value input from the keyboard
    	printf("\n\n");
    
    	//ends returns the stepsize and also defines the left and right endpoints.
    	step = ends(ch, &leftend, &rightend);
    	printf("%lf, %lf, %lf\n\n", leftend, rightend, step);
    
    	return 0;
    }
    
    double ends(char yorn, double *left, double *right){
    	double step, leftend, rightend;
    	
    	//this section defines the range of r over which the energy is calculated
    	while(1){
    		if(yorn!='y'&&yorn!='n'){
    			printf("Do you want to use the range \n[0.35 <= a2 <= 2.0] \nwith a step size of 0.01 (y or n) ?: "); //( recall: r = a2 * a) 
    	        scanf("%c", &yorn);
    			printf("\n\nthe characeter 'ch' is %c.\n\n", yorn);
    		}
    		else if(yorn == 'y'){
    			leftend = 0.35;
    			rightend = 2.0;
    			step = 0.01;
    			break;
    		}
    		else{
    			printf("enter the left endpoint: ");
    			scanf("%lf", &leftend);
    			printf("enter the right endpoint: ");
    			scanf("%lf", &rightend);
    			printf("enter the step size: ");
    			scanf("%lf", &step);
    			break;}
    	}
    
    	*left = leftend;
    	*right = rightend;
    
    	return step;
    }
    this is only part of the code, but after running, this is what appears on the screen:

    Enter the lattice constant, a, then hit enter: 1


    Do you want to use the range
    [0.35 <= a2 <= 2.0]
    with a step size of 0.01 (y or n) ?:

    the characeter 'ch' is
    .

    Do you want to use the range
    [0.35 <= a2 <= 2.0]
    with a step size of 0.01 (y or n) ?: y


    the characeter 'ch' is y.

    0.350000, 2.000000, 0.010000

    Press any key to continue


    It seems as though the fist scanf is ignored, but the second one actually works. Please help me get rid of this nuisance. Thanks in advance,

    Josh

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Everything about this code tells me it should have been posted in the C forum not the C++ forum.

    The problem you're having is because you have a character left after you input the float. Flush the input buffer after the input.

    Code:
    scanf("%lf", &a);
    getchar();
    Last edited by SlyMaelstrom; 11-16-2005 at 10:38 AM.
    Sent from my iPadŽ

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Actually, the buffer should be flushed like so:
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF);
    That is definitely a C program.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    What is scanf?
    Woop?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Yeah I knew there was a better way to flush the buffer.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Replies: 7
    Last Post: 05-25-2006, 01:51 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 08:28 AM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 05:34 AM
  5. Having trouble with scanf function
    By cw3od in forum C Programming
    Replies: 2
    Last Post: 11-28-2001, 03:58 PM

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