该程序应该执行以下操作:
添加主要详细信息
添加大学详细信息
为大学添加专业
更新主要成本
搜索专业
在完成主要功能之前,我面临一个问题:
当我在函数中使用 scanf 而不是 get 时,它会问 2 个问题,而不会让我填写前一个问题。例如:输入大学名称:输入大学地址:当我使用gets时,我遇到了同样的问题。
如果还有其他错误请提示我
提前致谢!
#include <stdio.h>
#include <string.h>
//Define Structures
typedef struct {
char name[30];
char department[30];
int nb_students;
int credits;
float cost;
char adv_name[15];
}Major;
typedef struct {
char name[50];
char address[30];
Major uni_majors[50];
int nb_majors;
}University;
// Define Functions Prototypes
Major majors_function();
University university_function();
void AddMajor(Major *major,University *university);
University UpdateMajor(char nameMajor[], University U, float newCost);
void SearchMajor(Major major,University university);
int main(int argc, const char * argv[])
{
Major new_major;
University new_university;
new_major = majors_function();
new_university = university_function();
return 0;
}
// Fills The Major Details Function
Major majors_function() {
Major major;
printf("Enter Major name: ");
gets(major.name);
printf("Enter Department name: ");
gets(major.department);
printf("Enter number of students: ");
scanf("%d",&major.nb_students);
printf("Enter number of credits: ");
scanf("%d",&major.credits);
printf("Enter the cost of credit: ");
scanf("%f",&major.cost);
printf("Enter the Advisor's Last Name: ");
scanf("%s",major.adv_name);
return major;
}
// Fills the university details Function
University university_function() {
University university;
printf("Enter university's name: ");
gets(university.name);
printf("Enter university's address: ");
gets(university.address);
printf("Enter number of majors in this univeristy: ");
scanf("%d",&university.nb_majors);
return university;
}
// Adds Major to a university
void AddMajor(Major *major,University *university) {
university->nb_majors = 0;
if(university->nb_majors <50) {
university->uni_majors[university->nb_majors] = *major;
}
else
printf("No Available space");
university->nb_majors++;
}
// Update Major's Cost
University UpdateMajor(char nameMajor[], University U, float newCost) {
if(strcmp(nameMajor,U.uni_majors->name)) {
U.uni_majors->cost = newCost;
}
return U;
}
// Searches for a major in a university
void SearchMajor(Major major,University university) {
if(strcmp(university.uni_majors->name,major.name))
printf("The total cost of this major is %.2f",(major.cost*major.credits));
else
printf("There is no such major!");
}