我是編程和學習 C 語言的新手,并且正在練習結構,并試圖通過使用結構來制作基本的學校管理系統,但是我遇到了太多錯誤(通過在結構中使用指標)。所以,你能告訴我有什么錯誤,我怎樣才能避免這些型別的錯誤。因為,根據我的理解,這應該是有效的。
這是我的代碼:
#include <stdio.h>
struct student {
int rollno;
char name[20];
float marks;
};
int main() {
again:;
while (1 == 1) {
struct student array[60];
char input;
int roll;
printf("What you would like to do:\n");
printf("Enter 'R' to fill the details again of all the student\n Enter 'U' to edit the
details of a single student\n");
printf("Enter 'S' to see the details of a particular student\n Enter 'A' to see the
details of every student\n");
scanf("%c", &input);
if (input == 'A' || 'a') {
for (int i = 0; i < 60; i ) {
printf("Roll no. : %d\n Name : %s\n Marks : %f\n", array[i].rollno, array[i].name,
array[i].marks);
}
}
else if (input == 'R' || 'r') {
for (int i = 0; i < 60; i ) {
printf("Roll no. os student is : %d\n", (i 1));
printf("Enter students name :\n");
gets(array[i].name);
printf("Enter students marks :\n");
scanf("%f", &array[i].marks);
}
}
else if (input == 'S' || 's') {
printf("Enter the roll no. of the student of whom you want to see the details :\n");
scanf("%d", &roll);
printf("Roll no. :\n %d\n", roll);
printf("Name : \n");
puts(array[(roll - 1)].name);
printf("Marks : \n %f \n", (*(array (roll - 1))->marks));
}
else if (input == 'U' || 'u') {
printf("Enter the roll no. of the student of whom you want to see the details :\n");
scanf("%d", &roll);
printf("Enter the name :\n");
gets((*(array (roll - 1))->name));
printf("Enter the marks :\n");
scanf("%f", &(*(array (roll 1))->marks));
} else {
printf("Error Occured!!! \n");
printf("Re-enter your input\n");
goto again;
}
}
return 0;
}
uj5u.com熱心網友回復:
你的代碼有很多問題:
不需要 a
goto again;,你已經有一個無限回圈while (1 == 1),通常是用for (;;)C 撰寫的。不要使用goto.定義
struct student array[60];應該移到回圈體的范圍之外。如所發布的,該陣列在每次迭代后被丟棄,其內容變得不確定。array應該初始化以避免在從用戶讀取內容之前列印內容時出現未定義的行為。字串文字不能跨越 C 源檔案中的多行。您可以通過這種方式拆分它們以提高可讀性:
printf("What you would like to do:\n" "Enter 'R' to fill the details again of all the student\n" "Enter 'U' to edit the details of a single student\n" "Enter 'S' to see the details of a particular student\n" "Enter 'A' to see the details of every student\n");你應該有一個選單選項來退出程式
讀取單個字符
scanf("%c", &input)很棘手:scanf()將從先前的呼叫中讀取掛起的換行符。您應該scanf(" %c", &input)在空間將導致待處理的空白被讀取和丟棄的地方使用。if (input == 'A' || 'a')不測驗Aora,它比較input,'A'如果不同比較'a'為零,因此測驗總是正確的。你應該寫:if (input == 'A' || input == 'a')gets(array[i].name);是一個NO NO。不要使用gets(),扔掉告訴你使用它的書。為了與其他輸入保持一致,您可以使用scanf("[^\n]", array[i].name);
請注意,它19告訴空終止符之前scanf()要存盤的最大字符數array[i].name,防止更長的用戶輸入出現未定義的行為,并[^\n]導致scanf()讀取和存盤字符直到換行符,但不包括換行符。s將停在任何空白處,防止輸入多個單詞,例如James Bond.
scanf("%f", &(*(array (roll 1))->marks));是一種非常扭曲的寫法:scanf("%f", &array[roll 1].marks);您應該檢查回傳值
scanf()以檢測無效輸入并在錯誤時重繪 待處理的輸入。
這是修改后的版本:
#include <stdio.h>
struct student {
int rollno;
char name[20];
float marks;
};
int main() {
struct student array[60] = { 0 };
int len = sizeof(array) / sizeof(*array);
int c, i, roll;
char input;
for (;;) {
printf("What you would like to do:\n"
"Enter 'R' to fill the details of all the student\n"
"Enter 'U' to edit the details of a single student\n"
"Enter 'S' to see the details of a particular student\n"
"Enter 'A' to see the details of every student\n"
"Enter 'Q' to quit the program\n");
if (scanf(" %c", &input) != 1)
break;
if (input == 'A' || input == 'a') {
for (i = 0; i < len; i ) {
printf("Roll no.: %d\nName: %s\nMarks: %f\n",
array[i].rollno, array[i].name, array[i].marks);
}
} else
if (input == 'R' || input == 'r') {
for (i = 0; i < len; i ) {
printf("Roll no. os student is: %d\n", i 1);
printf("Enter student's name:\n");
if (scanf("[^\n]", array[i].name) != 1)
break;
printf("Enter students marks:\n");
if (scanf("%f", &array[i].marks) != 1)
break;
}
if (i < len) {
printf("Invalid input\n");
}
} else
if (input == 'S' || input == 's') {
printf("Enter the roll no. of the student of whom you want to see the details:\n");
if (scanf("%d", &roll) == 1 && roll >= 1 && roll <= len) {
printf("Roll no.: %d\nName: %s\nMarks: %f\n",
roll, array[roll - 1].name, array[roll - 1].marks);
} else {
printf("Invalid Roll no\n");
}
} else
if (input == 'U' || input == 'u') {
printf("Enter the roll no. of the student of whom you want to see the details:\n");
if (scanf("%d", &roll) == 1 && roll >= 1 && roll <= len) {
printf("Enter the name:\n");
scanf("[^\n]", array[roll - 1].name);
printf("Enter the marks:\n");
scanf("%f", &array[roll - 1].marks);
} else {
printf("Invalid Roll no\n");
}
} else
if (input == 'Q' || input == 'q') {
return 0;
} else {
printf("Invalid entry\n");
printf("Re-enter your input\n");
}
/* read and discard the rest of the input line */
while ((c = getchar()) != EOF && c != '\n')
continue;
}
printf("Premature end of file\n");
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/469133.html
