C语言温度统计程序scanf/printf异常求助:显示内存值而非有效数据
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
doubleinscanf:
You're using%gto read adoublevalue withscanf, but that's wrong. Inscanf, the format specifier fordoubleis%lf(thelstands for "long", indicating it's a double-precision float). The%gspecifier is valid for printing doubles withprintf, 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 callingfflushon an input stream likestdin. This operation is undefined and can cause unexpected behavior across different compilers. Instead of usingfflush(stdin)to clear leftover newline characters from the input buffer, you can handle it properly by either:- Adding a space before the
%sspecifier inscanf(e.g.,scanf(" %s", rilevazioni[i].nome)), which tellsscanfto skip any leading whitespace (including newlines). - Using a loop to read and discard remaining characters until you hit a newline.
- Adding a space before the
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?
- Replaced
scanf("%g", &rilevazioni[i].temp)withscanf("%lf", &rilevazioni[i].temp): This correctly reads the double-precision temperature value from input, eliminating the garbage memory values. - Removed
fflush(stdin): Instead, we usegetchar()after the initialscanf("%d")to consume the leftover newline, and added a space before%sin the name input to skip any leading whitespace. - Added minor formatting tweaks: Like newlines in
printfstatements for cleaner output, and bullet points for the final list of cities.
Why This Works
- Using the correct
%lfspecifier ensuresscanfwrites the input value to thedoublevariable properly. - Avoiding
fflush(stdin)eliminates undefined behavior, making the program behave consistently across different compilers and systems.
内容的提问来源于stack exchange,提问作者Salvatore Ferlante

