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