从c中定义的与结构相关的函数中打印错误的数字



我有一项作业,输入未知数量的学生信息,然后打印出来。我编写的代码适用于一个学生,但当我输入其他学生时,只有电话号码和ID的输出是错误的,但没有任何错误消息。这是我的代码的较短版本。我希望有人能帮我找出错误。感谢您提前提供帮助!!

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRSIZE 75
struct student GetData();
int print_info (struct student *STU, int i);
struct company
{
char Co_Intern[STRSIZE];
long int Co_Tel; ///this variable has wrong output
};
struct student
{
long int ID; ///this variable has wrong output
char name[STRSIZE];
long int Tel; ///this variable has wrong output
struct company COE;
int Tot_days;
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
};
int main()
{
struct student *STU;
int i, n;
printf("Enter the number of students: ");
scanf("%d", &n);
STU = (struct student*) malloc(n * sizeof(struct student));
for(i = 0; i < n; ++i)
{
STU[i]= GetData(); //calling a struct function to scan data
}
printf("nDisplaying Information:n==============================nn");
for(i = 0; i < n; ++i)
{
print_info (STU, i);  //calling a function to print data
}
return 0;
}
struct student GetData() ///struct function to scan data
{
struct student info_s;
printf("nEnter student's information:n");
printf("Student ID:t");
scanf("%li", &info_s.ID);
printf("Student's name:t");
getchar();
gets(info_s.name);
printf("Student Telephone number:t");
scanf("%li", &info_s.Tel); 
printf("nEnter student's internship company date and information:n");
printf("Student's Company of internship:t");
scanf("%s", info_s.COE.Co_Intern);
printf("Company's Telephone:t");
scanf("%li", &info_s.COE.Co_Tel); 
printf("Beginning date: dd-mm-yyyy:t");
scanf("%d%d%d", &info_s.doj.dd, &info_s.doj.mm, &info_s.doj.yyyy);
printf("Total days of internship:t");
scanf("%d", &info_s.Tot_days);
return(info_s);
}
int print_info (struct student *STU, int i)
{
printf("nStudent-%d:tttttt",i+1);
printf("nnStudent ID:ttttt%li", STU[i].ID);
printf("nnStudent's name:ttttt%s", STU[i].name);
printf("nnStudent Telephone number:ttt%li", STU[i].Tel);
//here is the problem
printf("nnStudent's Company of internship:tt%s", STU[i].COE.Co_Intern);
printf("nnCompany's Telephone:tttt%li", STU[i].COE.Co_Tel);
//here is the problem
printf("nnBeginning date: dd-mm-yyyy:ttt%d%d%d", STU[i].doj.dd, STU[i].doj.mm, STU[i].doj.yyyy);
printf("nnTotal days of internship:ttt%d", STU[i].Tot_days);
printf("nn==============================nn");
}

这是长数字错误的输出:

Enter the number of students: 2
Enter student's information:
Student ID:     1234567890
Student's name: xxxx xxxx
Student Telephone number:       987654321
Enter student's internship company date and information:
Student's Company of internship:        aaaa
Company's Telephone:    5554443330
Beginning date: dd-mm-yyyy:     22-10-2020
Total days of internship:       365
Enter student's information:
Student ID:     1112223330
Student's name: nnnn ssss
Student Telephone number:       9998887770
Enter student's internship company date and information:
Student's Company of internship:        name
Company's Telephone:    3333344444
Beginning date: dd-mm-yyyy:     20-10-2020
Total days of internship:       365
Displaying Information:
==============================

Student-1:
Student ID:                                     1234567890
Student's name:                                 xxxx xxxx
Student Telephone number:                       987654321
Student's Company of internship:                aaaa
Company's Telephone:                            1259476034
Beginning date: dd-mm-yyyy:                     22-10-2020
Total days of internship:                       365
==============================

Student-2:
Student ID:                                     1112223330
Student's name:                                 nnnn ssss
Student Telephone number:                       1408953178
Student's Company of internship:                name
Company's Telephone:                            -961622852
Beginning date: dd-mm-yyyy:                     20-10-2020
Total days of internship:                       365
==============================

正如你所看到的,下面的几行有问题,只有Id是正确的,但有时随机地,第二个Id也是错误的,并且有错误的值,但我在代码中的日期和持续时间是正确的。我还尝试了其他人询问的其他方法,比如将它们单独扫描为int,然后将它们分配给相关的结构,但效果不佳。

Company's Telephone: -961622852

Student Telephone number: 1408953178

Company's Telephone: 1259476034

可能还有其他问题,但输入字符串"%d%d%d"与给定日期的输入不匹配。因此,scanf将这些值视为负数。为了避免这个错误,也许可以使用不同的输入格式。一般来说,为了避免这种类型的错误,每次调用scanf时,必须始终检查scanf返回的值。例如,可能将dd/mm/yy与:一起使用

if( 3 != scanf("%d/%d/%d", &info_s.doj.dd, &info_s.doj.mm, &info_s.doj.yyyy) ){
fprintf(stderr, "Invalid inputn");
exit(EXIT_FAILURE);
}

如果您想提供一个更友好的交互界面(最好使用C!以外的语言(,您可能不应该使用scanf来读取输入。相反,使用fgets,然后在读取数据后解析数据(可能使用sscanf,但实际上最好避免使用整个scanf系列(。尝试使用scanf从意外输入中恢复通常比实际情况更困难。

请参阅:http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html

最新更新