You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言温度统计程序scanf/printf异常求助:显示内存值而非有效数据

Fixing Your C Program's Input/Output Issues

Hey there! I see exactly what's causing those weird memory values in your program—let's break down the problems and fix them one by one.

Key Issues in Your Code

  • Incorrect format specifier for double in scanf:
    You're using %g to read a double value with scanf, but that's wrong. In scanf, the format specifier for double is %lf (the l stands for "long", indicating it's a double-precision float). The %g specifier is valid for printing doubles with printf, but not for reading them. Using the wrong specifier here leads to undefined behavior—like reading garbage memory values instead of your input temperature.

  • Undefined behavior with fflush(stdin):
    The C standard doesn't allow calling fflush on an input stream like stdin. This operation is undefined and can cause unexpected behavior across different compilers. Instead of using fflush(stdin) to clear leftover newline characters from the input buffer, you can handle it properly by either:

    • Adding a space before the %s specifier in scanf (e.g., scanf(" %s", rilevazioni[i].nome)), which tells scanf to skip any leading whitespace (including newlines).
    • Using a loop to read and discard remaining characters until you hit a newline.

Corrected Code

Here's your fixed program with explanations of the changes:

#include <stdio.h>
#include <stdlib.h>

typedef struct { 
    char nome[30]; 
    double temp; 
} meteo;

int main(int argc, char* argv[]) {
    meteo rilevazioni[20];
    meteo min, max;
    double media = 0;
    int i = 0;
    int n;

    printf("quante citta' vuole inserire? (max 20 citta')\n");
    scanf("%d", &n);
    // Clear the newline left by scanf("%d") using getchar()
    getchar();

    printf("\n Inseriamo le rilevazioni\n");
    for (i = 0; i < n; i++) {
        printf("\n Inserisci il nome : ");
        // Added a space before %s to skip leading whitespace (like newlines)
        scanf(" %s", rilevazioni[i].nome);

        printf("\n Inserisci la temperatura : ");
        // Changed %g to %lf for reading double with scanf
        scanf("%lf", &rilevazioni[i].temp);

        if (i == 0) 
            min = max = rilevazioni[i];
        else if (min.temp > rilevazioni[i].temp) 
            min = rilevazioni[i];
        else if (max.temp < rilevazioni[i].temp) 
            max = rilevazioni[i];

        media += rilevazioni[i].temp;
    }

    media = media / n;
    printf("\n La citta' con temperatura minima e': %s con una temperatura di: %g gradi \n", min.nome, min.temp);
    printf("\n La citta' con temperatura massima e': %s con una temperatura di: %g gradi \n", max.nome, max.temp);
    printf("\n Le citta' con temperatura maggiore rispetto alla media (%f) sono:\n", media);
    
    for (i = 0; i < n; i++) {
        if (rilevazioni[i].temp > media) 
            printf(" - La citta' : %s ha una temperatura di: %g gradi \n", rilevazioni[i].nome, rilevazioni[i].temp);
    }

    return 0;
}

What Changed?

  1. Replaced scanf("%g", &rilevazioni[i].temp) with scanf("%lf", &rilevazioni[i].temp): This correctly reads the double-precision temperature value from input, eliminating the garbage memory values.
  2. Removed fflush(stdin): Instead, we use getchar() after the initial scanf("%d") to consume the leftover newline, and added a space before %s in the name input to skip any leading whitespace.
  3. Added minor formatting tweaks: Like newlines in printf statements for cleaner output, and bullet points for the final list of cities.

Why This Works

  • Using the correct %lf specifier ensures scanf writes the input value to the double variable properly.
  • Avoiding fflush(stdin) eliminates undefined behavior, making the program behave consistently across different compilers and systems.

内容的提问来源于stack exchange,提问作者Salvatore Ferlante

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:49:28