简体   繁体   English

如何在c中打印特定数据?

[英]How to print specific data in c?

I have a problem that when the users type same id,they need to type name and id again. 我有一个问题,当用户键入相同的id时,他们需要再次键入name和id。 How can i only ask the users to input the id rather than both name and id,when the users type same id number.Please give me some hints how to do that.Thanks for your help! 当用户键入相同的ID号时,我该如何仅要求用户输入ID而不是名称和ID。请给我一些提示,以帮助您。谢谢您的帮助!

struct student
{
    char student_name[30];          
    char student_id[10];            
    int student_course_num[20]; 
    int student_course[10];
};

int main()
{
int student_num;

printf("Enter the number of students:");
scanf("%d",&student_num);
fflush(stdin);
struct student S[student_num];
char TestForId[student_num][10];
int i,j,student_code=1;
for(i=0;i<student_num;i++)
{
    printf("Enter the name of student:");
    fgets(S[i].student_name,30,stdin);

    printf("Enter the Student ID (8 digits):");
    fgets(S[i].student_id,10,stdin);

    strcpy(TestForId[i],S[i].student_id);
    for(j=0;j<i;j++)
    {
    if(strcmp(TestForId[j],S[i].student_id)==0)
    {
        printf("The student id has already exit\n");
    }
    }
    student_code++;
}   

your problem is: you have one struct S which is supposed to hold all values. 您的问题是:您有一个应该保留所有值的结构S。 But inside S you have only one Array for the name, instead of an array of char-arrays for all names. 但是在S内部,您只有一个名称数组,而不是所有名称的char-array数组。

Maybe what you want is 也许你想要的是

struct student {
   char name[50];
   int id;
}

struct student sarray[30];

Try this: 尝试这个:

for(i=0;i<StuNum;i++)
{
    printf("Number of Students %d:\n",student_code);
printf("Enter the name of student:");
scanf("%s",&S.student_name[i]);
fflush(stdin);
printf("Enter the Student ID :");
scanf("%d",&S.student_id[i]);
fflush(stdin);
for(j=0;j<i;j++)
{
    if(S.student_id[j]==S.student_id[i])  //whether the id is same or not
    {
        printf("<ID NUMBER HAS ALREADY EXITED>\nEnter the ID again: ");
       scanf("%d",&S.student_id[i]);
        break;
    }
}
student_code++; }   
  1. Create an array of struct student , not just a single entry. 创建一个struct student数组,而不只是一个条目。 struct student S[Student_N];

  2. Create a count of used records. 创建使用记录的计数。 size_t Student_Count = 0;

  3. Ask the student ID first before the student name. 首先在学生姓名之前询问学生ID。

  4. Read the student ID ( and later student name) into a local struct student local; 将学生ID(和后来的学生姓名)读取到本地struct student local; . scanf("%29s", local.student_id); Use a width of sizeof(local.student_id) - 1 . 使用sizeof(local.student_id) - 1的宽度。

  5. Before asking the student name, search your list 0 up to Student_Count for a matching entry. 在询问学生姓名之前,搜索列表0直至Student_Count以找到匹配的条目。 If found, fill in the rest of local with the matching data, skip next 2 steps. 如果找到,请使用匹配的数据填充其余的local,然后跳过接下来的2个步骤。

  6. Read the student name into a local struct student local; 将学生姓名读入本地struct student local; . scanf(" %49[^\\n]", local.student_name); . Use a format specifier that scans in spaces between first & last name. 使用格式说明符,在名字和姓氏之间的空格中进行扫描。

  7. Copy local to student_id[Student_Count++] . local复制到student_id[Student_Count++]

  8. Not sure you need the student_num field. 不确定您是否需要student_num字段。 The index of S[Student_N] is the student_num . S[Student_N]的索引是student_num

  9. Check the results if scanf() as in if (scanf("%29s") != 1) Handle_Error(); 检查结果是否为scanf()if (scanf("%29s") != 1) Handle_Error(); .

  10. Delete fflush(stdin); 删除fflush(stdin);

You have to use strcmp instead of if(S.student_id[j]==S.student_id[i]) . 您必须使用strcmp而不是if(S.student_id[j]==S.student_id[i])

Something like : 就像是 :

if(0 == strcmp(S.student_id[j],S.student_id[i]))

More info about strcmp : Here 有关strcmp的更多信息: 此处

Also id should not be an int array but a char array (string right ?) 另外,id不应是int数组,而应是char数组(字符串对吗?)

I don't really like your int StuNum=S.student_num; 我真的不喜欢你的int StuNum=S.student_num; it should be at the top of the main. 它应该在主体的顶部。

I would create a struct array where each element is a struct containing ID and name. 我将创建一个结构数组,其中每个元素都是包含ID和名称的结构。 Then I would keep the array IDs sorted, so that it would really easy to check if the ID exists. 然后,我将对数组ID进行排序,以便确实很容易检查ID是否存在。

eg: 例如:

  • new ID=10 新ID = 10

  • the biggest ID in the array is 7 (this information is known because 阵列中最大的ID为7(此信息是已知的,因为
    the array is sorted) therefore no need to check further, just add a 数组已排序),因此无需进一步检查,只需添加一个
    new ID 新ID

In order to do so , you could use qsort and bsearch 为此,您可以使用qsortbsearch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM