File Input/Output Function Question
Hello, I'm taking the liberty of asking questions to others first The program is to input students' information through the file input/output function and get total scores and averages I have a question about the program output being good, but the output value is weird. There's nothing wrong with creating a new file, saving and loading the contents However, it seems that the output of the average part comes up to the "-1" part, which should be finished. There is no -1 part in the notepad, but when you run the source code, you add up to the last 77 points I think it's because the contents of the buffer remain when I input it, so the result is the same even though I initialized the buffer. I want to know where I made a mistake. I'd appreciate it if you let me know
Results screen
![Image][1]
![Image][2]
Source Code
///* Practice) Writing a grade program */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void main() {
// File = variable
FILE* fp = NULL;
int number, count = 0;
char name[20], fname[30];
float score, total = 0.0 ;
// Get transcript file name
printf("Enter your grade file name >>");
scanf("%s", fname);
fflush(stdin); // buffer initialization
// Creating a Sexual File
if ((fp = fopen(fname, "w")) == NULL) {
fprintf(stderr, "Cannot open file %s", fname);
exit(1);
}
// sexual content output
while (1) {
printf("Enter your grade for the class name (exits when you enter a negative number) >> ");
scanf("%d", &number);
if (number < 0)
break;
scanf("%s %f", name, &score);
fflush(stdin);
// Formatted Output
fprintf(fp, "%d %s %f\n", number, name, score);
}
fclose(fp); // If the end function is not entered here, the mean is not obtained
// Failed to open file
if ((fp = fopen(fname, "r")) == NULL) {
fprintf (stderr, "%s file cannot be opened", fname);
exit(1);
}
// the sum of grades
while (!feof(fp)) {
fscanf(fp, "%d %s %f", &number, name, &score);
total += score;
count++;
}
printf("%d student's total score = %f\n", count, total);
printf("%d student average score = %f\n", count, total / count);
// File Shutdown
fclose(fp);
}
[1]: https://res.cloudinary.com/eightcruz/image/upload/v1606902192/hl12gbtiubxmxnzz4vm4.png [2][2]: https://res.cloudinary.com/eightcruz/image/upload/v1606902206/qvza79o8huyi1by8iwzm.png
visual-studio
Please refer to the code below.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void main() {
// File = variable
FILE* fp = NULL;
int number = 0, count = 0;
char name[80] = { 0, }, fname[80] = { 0, };
float score = 0.0, total = 0.0;
// Get transcript file name
printf("Enter your grade file name >>");
scanf("%s", fname);
// Creating a Sexual File
if ((fp = fopen(fname, "w")) == NULL) {
fprintf(stderr, "Cannot open file %s", fname);
exit(1);
}
// sexual content output
while (1) {
printf("Enter your grade for the class name (exits when you enter a negative number) >> ");
scanf("%d", &number);
if (number < 0)
break;
scanf("%s %f", name, &score);
fflush(stdin);
// Formatted Output
fprintf(fp, "%d %s %f\n", number, name, score);
}
fclose(fp); // If the end function is not entered here, the mean is not obtained
// Failed to open file
if ((fp = fopen(fname, "r")) == NULL) {
fprintf (stderr, "%s file cannot be opened", fname);
exit(1);
}
// the sum of grades
while (fscanf(fp, "%d %s %f", &number, name, &score) == 3)
{
total += score;
count++;
}
printf("%d student's total score = %f\n", count, total);
printf("%d student average score = %f\n", count, total / count);
// File Shutdown
fclose(fp);
return 0;
}
Thank you. It was helpful
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
582 PHP ssh2_scp_send fails to send files as intended
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
918 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.