我不明白為什么運行該程式的用戶在選擇選單上的 #4 后沒有輸入學生全名的選項...它提示輸入結構的所有其他變數,但它跳過了第一。
我只是在嘗試隨機的東西,所以如果這段代碼看起來很亂,我很抱歉,我自己現在幾乎無法閱讀它。我們不允許在這個專案中使用陣列,這就是為什么我用指標做所有這些奇怪的事情。
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
// Ok, first we must make a struct that holds these things:
// StudentID
// Class Name
// ClassID (like INET3101)
// StudentFullName
struct record {
int studentID;
char *class_name;
char *classID;
char *full_name;
};
int num_rec = 0;
void add_record(struct record);
void remove_record(struct record);
void print_record(struct record);
struct record *database;
void add_record(struct record addTemp){
num_rec ;
if (num_rec ==1) {
database = malloc(sizeof(struct record));
memcpy(database, &addTemp, sizeof(struct record));
}
else {
struct record *tempMem = malloc(sizeof(struct record) * num_rec);
struct record *mover = database;
struct record *head = tempMem;
int i;
for (i=0; i< num_rec; i ){
memcpy(tempMem , mover , sizeof(struct record));
}
memcpy(--tempMem, &addTemp, sizeof(struct record));
free(database);
database = head;
}
}
void remove_record(struct record removeTemp){
if (num_rec == 0){
printf("There is nothing to delete here!");
return;
}
else{
struct record *tempMem= malloc(sizeof(struct record) * num_rec-1);
struct record *mover = database;
struct record *head = tempMem;
int i;
for (i = 0; i < num_rec-1; i ){
memcpy(tempMem, &removeTemp, sizeof(struct record));
}
memcpy(--tempMem, &removeTemp, sizeof(struct record));
free(database);
database = head;
num_rec--;
}
}
void printRecords(){
int i;
for(i = 0; i < num_rec; i ) {
int k = i 1;
printf("Student #%d\n", k);
fprintf(stderr, "Student full name: %s\n", (database i)->full_name);
fprintf(stderr, "Student ID: %d\n", (database i)->studentID);
fprintf(stderr, "Class name: %s\n", (database i)->class_name);
fprintf(stderr, "Class ID: %s\n", (database)->classID);
printf("\n");
}
}
// }
// 1. print all records
// 2. print number of records
// 3. print size of database
// 4. add record
// 5. delete record
// 6. print number of accesses to a database
// 7. exit
int main(){
int BUFSIZE = 100;
struct record ultimate;
static int accesses=0;
int choice=0;
while (true) {
printf("1. print all records \n");
printf("2. print number of records \n");
printf("3. print size of database \n");
printf("4. add record \n");
printf("5. delete record \n");
printf("6. print number of accesses to a database \n");
printf("7. exit \n");
scanf("%d", &choice);
switch (choice){
case 1:
;
printf("This is our current database of records: ");
if (num_rec == 0){
printf("Oh... It looks like the data base is currently empty.\n");
accesses ;
break;
}
else{
printRecords();
accesses ;
break;
}
case 2:
;
printf("The current number of student records is: %d records\n", num_rec);
accesses ;
// 3. print size of database
case 3:
;
int size_database;
int size_struct = sizeof(struct record);
size_database = size_struct * num_rec;
printf("The size of our database is %i\n", size_database);
break;
// 4. add record
case 4:
;
char *buf1 = (char*) malloc(BUFSIZE);
char *buf2 = (char*) malloc(BUFSIZE);
char *buf3 = (char*) malloc(BUFSIZE);
char *buf4 = (char*) malloc(BUFSIZE);
struct record ultimat;
printf("Please enter this students Full Name: \n");
fgets(buf1, BUFSIZE, stdin);
if ((strlen(buf1) > 0) && (buf1[strlen (buf1) - 1] == '\n'))
buf1[strlen (buf1) - 1] = '\0';
ultimat.full_name = malloc(strlen(buf1) 1);
strcpy(ultimat.full_name, buf1);
int id;
printf("Please enter this students Student ID: \n");
fgets(buf2, BUFSIZE, stdin);
if ((strlen(buf2) > 0) && (buf2[strlen (buf2) - 1] == '\n'))
buf2[strlen (buf2) - 1] = '\0';
ultimat.studentID = id;
printf("Please enter the Class Name: \n");
fgets(buf3, BUFSIZE, stdin);
if ((strlen(buf3) > 0) && (buf3[strlen (buf3) - 1] == '\n'))
buf3[strlen (buf3) - 1] = '\0';
ultimat.class_name = malloc(strlen(buf3) 1);
strcpy(ultimat.class_name, buf3);
printf("Please enter this students Class ID: \n");
fgets(buf4, BUFSIZE, stdin);
if ((strlen(buf4) > 0) && (buf4[strlen (buf4) - 1] == '\n'))
buf4[strlen (buf4) - 1] = '\0';
ultimat.classID = malloc(strlen(buf4) 1);
strcpy(ultimat.classID, buf4);
add_record(ultimat);
printf("Alright, I added this student to the database! \n");
free(buf1);
free(buf2);
free(buf3);
free(buf4);
accesses ;
break;
// 5. delete record
case 5:
;
remove_record(ultimate);
printf("Alright! I got the student at the top of the list deleted! \n");
break;
// 6. print number of accesses to a database
case 6:
;
printf("The current number of data base accesses is: %d acesses\n", accesses);
break;
case 7:
exit(0);
}
}return 0;
}
uj5u.com熱心網友回復:
在這個電話之后scanf
scanf("%d", &choice);
'\n'輸入緩沖區包含在第一次呼叫之前需要洗掉的換行符fgets
fgets(buf1, BUFSIZE, stdin);
否則將輸入一個空字串。
例如,您可以通過以下方式進行操作
while (getchar() != '\n');
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516677.html
上一篇:這段代碼如何不會導致緩沖區溢位?
