我正在嘗試撰寫一個程式作為練習,它從檔案中獲取一些值,將它們分類為兩個名為studentsPassedand 的變數studentsFailed,然后列印通過的學生人數和失敗的學生人數以及模塊代碼和學生人數。
這是 .txt 檔案:
101 20
65 72 23 59 80 75 55 88 92 77 44 57 73 31 48 59 71 48 66 59
101是模塊代碼,20是班級的學生人數。下面一行中的 20 個數字是這 20 個學生中每個人的分數(例如學生 1 的得分為 65,學生 2 的得分為 72,等等)
這是我的代碼:
#include <stdio.h>
FILE *fp;
int main(){
//Open the file and assign its address/disk location to file pointer
fp = fopen("marks.txt", "r");
//Variables
int moduleCode, numStudents;
const int size = 20;
int studentMark;
int studentsPassed;
int studentsFailed;
//Scan in the first line for the module code and the number of students
fscanf(fp, "%d %d", &moduleCode, &numStudents);
//Scan in the student marks and loop through them
for (int i = 0; i < size; i )
if (fscanf(fp, "%d", &studentMark) < 40){
int studentsFailed = studentsFailed 1;
}
else if (fscanf(fp, "%d", &studentMark) >= 40){
int studentsPassed = studentsPassed 1;
}
else{
printf("Error: Number of marks exceeds cap!");
return 0;
}
//Print the results
printf("%d %d\n", moduleCode, numStudents);
printf("Number of students passed: %d\n", studentsPassed);
printf("Number of students failed: %d\n", studentsFailed);
return 0;
}
What the program should do is read the module code and the number of students and print them on the first line (which is successfully does), then the program should loop through each of the 20 numbers and sort them into different variables based on if they are higher or lower than 40, this is the part that I am struggling on, as the program executes, but it will print out a random large number for studentsPassed, and "1" for studentsFailed every time I run it.
What am I doing wrong? I feel like I am missing something to do with looping through each of the numbers but I'm not sure how to correct it.
Note: This is what I initially tried (which also didn't work) before reading another answer on this website to get what my current code is.
//Scan in the student marks and loop through them
fscanf(fp, "%d", &studentMark);
for (int i = 0; i < size; i )
if (studentMark < 40){
int studentsFailed = studentsFailed 1;
}
else if (studentMark >= 40){
int studentsPassed = studentsPassed 1;
}
uj5u.com熱心網友回復:
你的第二個版本幾乎是正確的,只是呼叫fscanf()需要在回圈內。
此外,您不應在塊中重新宣告studentsFailed和。只需分配給之前宣告的變數。studentsPassedif
for (int i = 0; i < size; i )
fscanf(fp, "%d", &studentMark);
if (studentMark < 40){
studentsFailed ;
}
else{
studentsPassed ;
}
}
else if當第二個條件與第一個條件相反時,您也不應該使用。只需使用else.
uj5u.com熱心網友回復:
*scanf回傳的值是成功匹配的轉換說明符的數量。它與它從輸入流中讀取的值無關。
你應該改掉依賴輸入流來提供預期資料點數量的習慣;它很脆弱,通常沒有必要。相反,只需讀取所有資料,直到到達輸入流的末尾。在您的情況下,您可以執行以下操作:
#include <stdio.h>
int
main(int argc, char **argv)
{
int moduleCode, numStudents, studentsPassed = 0, studentsFailed = 0;
int actualNumStudents = 0;
int score;
FILE *fp = argc > 1 ? fopen(argv[1], "r") : stdin;
if( fp == NULL ){
perror(argv[1]);
return 1;
}
if( fscanf(fp, "%d%d", &moduleCode, &numStudents) != 2 ){
fprintf(stderr, "invalid input\n");
return 1;
}
while( fscanf(fp, "%d", &score) == 1 ){
actualNumStudents = 1;
*( score < 40 ? &studentsFailed : &studentsPassed ) = 1;
}
if( ! feof(fp) ){
fputs(ferror(fp) ? "read error\n" : "invalid input\n", stderr);
return 1;
}
if( actualNumStudents != numStudents ){
fprintf(stderr, "Warning: the declared number of students does "
"not match the actual number of students\n");
}
printf("%d %d\n", moduleCode, actualNumStudents);
printf("Number of students passed: %d\n", studentsPassed);
printf("Number of students failed: %d\n", studentsFailed);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359881.html
