scanf is not storing inputed numbers

Hello!

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  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); }


}
Can we see the function calling getColor() ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void calculate(){

    system("CLS");
    system("COLOr 2F");

    printf("------------------CALCULATING RESISTANCE------------------\n");
    printf("\nPlease enter the colors of the 4 bands: ");
        help2();



    getColor(); //Get color values of the 4 bands from user.


    //Assesing the numerical values of color1,color2,color3,color4 and assigining
    //approrptaite values to the relevant variables declared above
       while(counter<=12){


   //Determing Digit 1 and 2 Values
        if(determineColor(color1,counter) == true)
            digit1=counter-1;

            if(determineColor(color2,counter) == true)
                digit2=counter-1;

   //Determinig 10^Power , Power = ?
        if(determineColor(color3,counter)==true && counter < 11)
            powervalue = counter - 1 ;

            if(determineColor(color3,counter)==true && counter == 11)
                powervalue = -1 ;

                if(determineColor(color3,counter)==true && counter == 12)
                    powervalue = -2 ;

    //Determinig Tolerance
        if(determineColor(color4,counter)==true && counter == 11)
                tolerance = 5 ;

            if(determineColor(color4,counter)==true && counter == 12)
                tolerance = 10 ;


            counter++;
       }

        //Outputting Values
        resOutput(digit1,digit2,powervalue,tolerance,0);
I did this and there was nothing wrong with scanf
1
2
3
4
5
6
int main()
{
  int i;
  for(i = 0; i < 3; i++)
     getColor();
}


Hence the issue is in the function calling getColor()
I dont see any thing wrong with your codes.

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.
Can i have your complete code?
Hi,

Just some small matters of style:

When using scanf, always check the value it returns - to see if it worked properly.

With this:

if(determineColor(color1,counter) == true)

the test for true is unnecessary, because conditionals always evaluate to true or false :-

if(determineColor(color1,counter) )

Hope this helps a little. got to go - motor racing to watch !!
Topic archived. No new replies allowed.