Daniel Pawłowski
Daniel Pawłowski

Reputation: 43

C : Infinite loop when using scanf

I am using following structure:

struct Transistor {
   char  type_number[50];
   char  supplier[50];
   int   maxVce;
   float   maxIc;
   int   max_power;
   int   max_freq;
   int   max_gain;
   float   price;
};

Running code:

while(1==1){
        printf("Insert parameters: \n");
        char input[100];
        scanf ("%[^\n]", &input);
        char *token = strtok(input, " ");
        if(input[0]=='q'){
            return 0;
        }
        parameters[0]=atoi(token);
        token = strtok(NULL, " ");
        ic=atof(token);
        token = strtok(NULL, " ");
        parameters[1]=atoi(token);
        token = strtok(NULL, " ");
        parameters[2]=atoi (token);
        token = strtok(NULL, " ");
        parameters[3]=atoi(token);


        printf ("\n Parameters: \nVCe:%d,\nPow:%d,\nFreq:%d,\nGain:%d \nIce:%.3f\n",parameters[0],parameters[1],parameters[2],parameters[3],ic);
        printf("Transistor that matches your requirements: \n");
        for(a=0; a<15; a++){
            i=0;
            if(parameters[0]<=all_Transistors[a].maxVce){
                i++;
            }
            if(parameters[1]<=all_Transistors[a].max_power){
                i++;
            }
            if(parameters[2]<=all_Transistors[a].max_freq){
                i++;
            }
            if(parameters[3]<=all_Transistors[a].max_gain){
                i++;
            }
            if(ic<=all_Transistors[a].maxIc) {
                i++;
            }
            if(i==5){
                printf("\n\nTransistor name: %s\n",all_Transistors[a].type_number);
                printf("Supplier: %s\n",all_Transistors[a].supplier);
                printf("Maximum Vce: %d\n",all_Transistors[a].maxVce);
                printf("Maximum Ic: %.3f\n",all_Transistors[a].maxIc);
                printf("Maximum power: %d\n",all_Transistors[a].max_power);
                printf("Maximum freq: %d\n",all_Transistors[a].max_freq);
                printf("Maximum gain: %d\n",all_Transistors[a].max_gain);
                printf("Price: %.3f\n",all_Transistors[a].price);
            }
        }
    }

Thing is: I am entering the first set of parameters like:

1 1.1 2 3 4

It works fine loop runs again but scanf is not, it should work as long as user enters q as a first character of input.

Why is scanf not running every loop?

Upvotes: 1

Views: 75

Answers (1)

SiggiSv
SiggiSv

Reputation: 1229

Your main problem with this line:

scanf ("%[^\n]", &input);

was that it reads everything from the stdin buffer up to '\n' and leaves '\n' in the buffer. So the next time you get back to that line to read from the buffer, the first char in the buffer is still '\n' resulting in nothing being read and the buffer not being changed.

(Another problem with that line is that there is the risk of buffer overflow if the line being read is longer than the input array).

There is more than one way to fix this but the simplest one (that also should protect you from buffer overflow) would be the one that has been mentioned in the comments, by using fgets(input, sizeof input, stdin);.

Upvotes: 1

Related Questions