The_Bandit
The_Bandit

Reputation: 73

for Loop Not Running - Temperature Problem

I'm having some trouble with this simple for loop using user inputs question. The problem wants me to create a table that converts fahrenheit to celcius, taking user input values for the starting, ending, and increment values for the table using scanf. I've only been coding for 2 weeks, and I just started loops, but this seems like it should work. Thanks! Here is the code I have:

#include <stdio.h>



int main (void) 
{
    int f, c, f_min, f_max, i;

    printf("Enter the minimum (starting) temperature value: ");
    scanf("%d", &f_min);

    printf("Enter the maximum (ending) temperature value: ");
    scanf("%d", &f_max);

    printf("Enter the table increment value: ");
    scanf("%d", &i);

    for (f = scanf("%d", &f_min); f <= scanf("%d", &f_max); f = f + scanf("%d", &i))
    {
        c = ((f - 32.0) * (5.0 / 9.0));
        // printf("Degrees in C is: %d");
    }

    return 0;
} 

Upvotes: 0

Views: 117

Answers (4)

Sakshama Ghoslya
Sakshama Ghoslya

Reputation: 88

As mentioned in one of the answer scanf returns number of iteams it reads.
For example A = scanf("%d, %d", &f1, &f2);. In this case A will be 2 since the scanf read two values.

In your code:

for (f = scanf("%d", &f_min); f <= scanf("%d", &f_max); f = f + scanf("%d", &i))

Here:
f = scanf("%d", &f_min) will be evaluated as 1 and value of f will also become 1

& f <= scanf("%d", &f_max) will be evaluated as f <= 1. Since value of f is 1 this statement will be true.

& f = f + scanf("%d", &i) will be evaluated as f = f + 1.

In all the above cases I assumed as you have provided inputs to scanf and since it is reading single integer it will return 1
So your for will become:

for (f = 1; f <= 1; f = f + 1)

Which will be evaluated exactly once.

What happens when you don't provide any input to the scanf? it will be:

    for (f = 0; f <= 0; f = f + 0)
    {
    }

Upvotes: 0

John Bode
John Bode

Reputation: 123468

scanf returns the number of input items successfully converted and assigned, or EOF if it sees an end of file or error indication on the input stream. It does not return the entered value.

You don't need to read a new value for each f - you just set it to f_min and then increment it until it's greater than f_max:

for ( f = f_min; f <= f_max; f += i )
  ...

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83527

    for (f = scanf("%d", &f_min); f <= scanf("%d", &f_max); f = f + scanf("%d", &i))

Each time you call scanf() it will wait for input from you. This is why the for loop appears to not run. In addition, scanf() returns a code that indicates a success or error of the operation. I think this isn't what you want. You have already read the min, max, and increment values into variables, so you don't need scanf() here at all. Just use the variables:

for (f = f_min; f <= f_max; f += i)

Upvotes: 3

Govind Parmar
Govind Parmar

Reputation: 21542

You are calling scanf inside your loop initializer, loop guard, and loop increment count, which means that the program is waiting for input from you at the beginning of the loop, at the beginning of each loop iteration, then at the end of each loop iteration. You also aren't comparing the values f_max and i but the return value of scanf, which is the number of format specifiers it successfully populated from the input string, not the values read.

You already have the values you want, f_min, f_max and i, just use those in the loop:

for(f = f_min; f <= f_max; f+=i)

Upvotes: 5

Related Questions