[英]Program skipping over user input (Large modular Library Info System in C)
I'm currently in the process of programming the largest C program I have ever written, for a final assignment for one of my university modules. 我目前正在编程我曾经写过的最大的C程序,以完成我大学模块之一的最终作业。 The program is modular, containing files such as main.c, librarian.c, students.c, and books.c.
该程序是模块化的,包含main.c,librarian.c,students.c和books.c等文件。 Each has their corresponding header file and I also have a file, structs.h, containing two structures: one for books and one for students.
每个文件都有对应的头文件,我还有一个文件structs.h,其中包含两个结构:一个用于书籍,一个用于学生。
I'm currently facing a problem, where when the librarian tries to add a new student membership to the library, certain inputs are completely skipped over and I don't understand why. 我当前遇到一个问题,当图书馆管理员尝试向图书馆添加新的学生会员资格时,某些输入内容被完全跳过了,我不明白为什么。
I'll try and include as little code as possible but this may be a challenge due to the size of my program. 我将尝试包含尽可能少的代码,但是由于程序的大小,这可能是一个挑战。
First, here's my student structure as it may be relevant: 首先,这是我的学生结构,可能与之相关:
struct STUDENT //Structure variable for student
{
int id;
char *name[20];
char *pass[20];
int mobile;
float fee;
int age;
char *cat;
};
struct STUDENT student;
Now, the function that is causing me problems is this: 现在,导致我出现问题的函数是这样的:
int student_data(int answer){ //Function to add data of student to LIS
student_confirm();
int x = 15, x1 = 30;
int student_ID;
gotoxy(x,7);printf("Enter the Information Bellow");
gotoxy(x,10);printf("Student Name:"); gotoxy(x1,10);scanf(" %d",&student_ID);
if(student.id==student_ID){
gotoxy(x,11);printf("Id is already Exits");
getch(); add_student();
}
student.id=student_ID;
gotoxy(x,11);printf("User Name:");gotoxy(x1,11);scanf("%s",&student.name);
gotoxy(x,12);printf("Password:");gotoxy(x1,12);scanf("%s",&student.pass);
gotoxy(x,13);printf("Mobile:");gotoxy(x1,13);scanf("%d",&student.mobile);
gotoxy(x,14);printf("Fee:");gotoxy(x1,14);scanf("%f",&student.fee);
gotoxy(x,15);printf("Age:");gotoxy(x1,15);scanf("%d",&student.age);
return 1;
}
The student_confirm() function simply asks the user if they are sure they want to add a new student to the library. student_confirm()函数只是询问用户是否确定要向库中添加新学生。 Now, when I run the program, I sign in as the librarian, go to add a student and am greeted with this in the console
现在,当我运行程序时,我以图书管理员的身份登录,去添加一个学生,并在控制台中对此表示欢迎。
*****Library Information System*****
***Brotherton Library***
*1975*
____________________________________________
Confirm you would like to add a new student: (Y/N)
Enter the Information Bellow
Student Name:
User Name:
Password:
It skips straight to Password, not allowing me to enter the prior two fields. 它直接跳到了密码,不允许我输入前两个字段。 I'm completely stumped by this.
我对此深感困惑。 There's similar parts of code in my program (such as for adding books) that follow the same logic and they work just fine.
我的程序中有相似的代码部分(例如用于添加书籍)遵循相同的逻辑,并且工作正常。 I wasn't able to find anything via a search on here either.
我也无法通过搜索找到任何东西。
Now, I've only been programming in C for about 6 months so I'm definitely a beginner. 现在,我只用C编程大约六个月,所以我绝对是一个初学者。 I'm becoming somewhat proficient but there's so much more to learn, and so much that I don't know about regarding the language.
我已经精通某种语言,但是还有很多东西要学习,甚至还有很多我对语言一无所知。
Warnings I'm getting are: 我收到的警告是:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char * (*)[20]' [-Wformat=]|
for both the fields that are skipped. 对于两个被跳过的字段。 Is this because arrays are technically pointers?
这是因为数组在技术上是指针吗? I tried changing a few things around, but like I said my books struct uses exactly the same logic as the students one, and I have an add_books function which is more or less the same as the students one provided which works without any hiccups.
我试图改变一些事情,但是就像我说我的书结构使用与学生完全相同的逻辑一样,我有一个add_books函数,该函数与学生提供的add_books函数大致相同,没有任何打ic。
Any help would be greatly appreciated and hopefully this question can help others in the future. 任何帮助将不胜感激,希望这个问题将来可以帮助其他人。 Thanks guys and gals.
谢谢大家和加尔斯。
edited to add the student_confirm() function: 编辑添加了student_confirm()函数:
int student_confirm(){ //Function to confirm adding of student
int x = 10;
char answer;
system("cls");window();
printf("\n\n\n");
gotoxy(x,5);printf("Confirm you would like to add a new student: (Y/N)");
if(getch() == 'y' || answer == 'Y')
student_data(answer);
return 1;
} }
The compiler is explaining it to you 编译器正在向您解释
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char * (*)[20]'
char *name[20];
is an array of pointers to char
, instead, you want an array of char
's: 是一个指向
char
的指针数组,相反,您需要一个char
的数组:
char name[20];
or a pointer to char
: 或指向
char
的指针:
char *name;
...
student.name = malloc(20);
and then 接着
scanf("%s", student.name);
Notice that you don't need to use the address of operator ( &
) with scanf("%s", student.name)
because student.name
is already (or decays into) a pointer. 注意,您不需要将操作符(
&
)的地址与scanf("%s", student.name)
因为student.name
已经(或变成)指针了。
Same for pass
. pass
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.