Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
326 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...