Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...