I wrote a looping program that will calculate resistance based on color input. Lets say I run it the first time and get a resistance value calculated, then when my code loops it again, and this function is called to determine the users choice of colors, scanf is not working properly and the values of color1,color2,color3 and color4 are not changed from the first run.
Why is that? Thank you in advance.
void getColor(){
//Getting a proper value of color 1
printf("\n\nFirst Color Numerical Value(1-10): "); scanf("%f",&color1);
while(color1 < 1 || color1 > 10){
char c;
c = getchar();
printf("\nPleae Enter a proper value (1-10): "); scanf("%f",&color1);
}
//Getting a proper value of color2
printf("\nSecond Color Numerical Value (1-10): ");scanf("%f",&color2);
while(color2<1 || color2>10){
char c;
c = getchar();
printf("\nPleae Enter a proper value (1-10): ");scanf("%f",&color2);
}
//Gettig a proper value of color3
printf("\nThird Color Numerical Value(1-12): "); scanf("%f",&color3);
while(color3 <1 || color3>12){
char c;
c = getchar();
printf("\nPlease Enter a proper value (1-12): "); scanf("%f",&color3);}
//Getting a proper cvalue for color 4
printf("\nFourth Color Numerical Value(11-12): ");scanf("%f",&color4);
while(color4 <11 || color3 >12){
char c;
c = getchar();
printf("\nPlease Enter a proper value (11-12): ");scanf("%f",&color4); }
}
scanf is not working properly and the values of color1,color2,color3 and color4 are not changed from the first run.
Can u explain the behavior during the second run?
Is it that it (scanf) does not allow you to input,or inputs but enters the while loop when it was not supposed to, or what exactly happens? And what is the data type for color?
For example I run it one time and enter 5,6,8,11 as my numbers for color1,color2,color3 and color4. Then when the program loops again, and getColor() is called, after the user enters the new values, still 5,6,8,11 are the assigned values to color1,color2,color3 and color 4.