繁体   English   中英

C:在main中调用struct函数时出错

[英]C: error in calling struct function in main

您是否知道为什么alegereStudent中的scanf无法正常工作? 控制台让我用空行写随机的东西,并在printf之后显示随机数。 我正在尝试做一个初学者项目,但是我不知道为什么这个结构不能让我按照自己的意愿进行scanf。

码:

#include<stdio.h>

struct student
{
    char numeStudent[20];
    char prenumeStudent[20];
    int idStudent;
    float medieAdmitere;
};

struct profesor
{
    char *numeProfesor[20];
    char *prenumeProfesor[20];
    char *domeniu[20];
};

void alegereStudent( struct student stud){
    printf("Introduceti Numele: %s", stud.numeStudent);
    scanf("%s\n",&stud.numeStudent);
    printf("Introduceti Prenumele:%s",stud.prenumeStudent);
    scanf("%s\n",&stud.prenumeStudent);
    printf("Introduceti ID-ul studentului:%d",stud.idStudent);
    scanf("%d\n",&stud.idStudent);
    printf("Introduceti Media de admitere:%f",stud.medieAdmitere);
    scanf("%f\n",&stud.medieAdmitere);
};

int main(void)
{
    int intrDate=0,cautDate=0,listDate=0,iesire=0,desprStudenti=0,desprProfesori=0,inapoi=0;
    struct student stud;
    printf("Meniu principal:\n\n");
    printf("1.Introducere date. %d\n",intrDate);
    printf("2.Cautare date.%d\n",cautDate);
    printf("3.Listare date.%d\n",listDate);
    printf("0.Iesire Aplicatie.%d\n",iesire);
    printf("Alegeti o optiune:");
    scanf("%d,%d,%d,%d",&intrDate,&cautDate,&listDate,&iesire);
    if(intrDate==1){
        printf("1. Despre studenti %d\n",desprStudenti);
        printf("2. Despre profesori\n");
        printf("3. Revenire la meniul principal\n");
        printf("Alegeti o optiune:");
        scanf("%d",&desprStudenti);
    }
    if(desprStudenti==1)
        alegereStudent(stud);

    return 0;
}

Scanf错误:您需要在alegereStudent函数中的scanf中删除&才能读取stud.numeStudentstud.prenumeStudent您需要了解数组和指针。 另外,您需要包括<stdio.h>并放入; return 0

错误原因:数组会衰减为指针,而scanf需要一个指针才能将用户输入读入变量,因此无需放置&

建议: http : //sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html

alegereStudent()的代码修改了局部变量stud ,但这只是在main()创建的未初始化结构的一个副本—它不会影响main()的结构。

要使函数在调用函数中修改结构,必须将指针传递给该结构。 对结构成员的引用然后使用arrow- ->运算符而不是点. 运营商。 您不想打印出旧的(未初始化的)值; 您要提示输入新值。 而且,您不想传递char (*)[20] (指向20个字符的数组的指针),而scanf()函数需要一个简单的char * —从字符串中删除&

一个主要问题是您不希望在scanf()格式的string中使用尾随换行符 这意味着用户必须键入非空格字符才能停止输入,而且人们也不擅长预测下一步需要做什么。 您正在使用的所有转换都会跳过前导空格,包括换行符,因此您不必担心遗留换行符(或者,如果您要担心它,则不必使用\\nscanf()格式字符串中)。

void alegereStudent(struct student *stud)
{
    printf("Introduceti Numele: ");
    scanf("%19s", stud->numeStudent);
    printf("Introduceti Prenumele: ");
    scanf("%19s", stud.prenumeStudent);
    printf("Introduceti ID-ul studentului: ");
    scanf("%d", &stud.idStudent);
    printf("Introduceti Media de admitere: ");
    scanf("%f", &stud.medieAdmitere);
}

这些格式将字符串限制为19个字符加一个空字节,因此您不会溢出,但是它们不能确保您读取(并丢弃?)任何多余的字节。 切记: %s最多读取下一个空格字符。 如果用户在第一行上键入Robin Hood ,则第一个scanf()将读取Robin和下一个Hood (无需等待用户键入的新数据。

另请注意,该功能后不需要分号。 它标志着函数主体之后的空(变量?)声明的结尾。

当然,您需要在main()函数中更改调用:

if (desprStudenti == 1)
    alegereStudent(&stud);

最好不要将七个变量定义填入一行。 实际上,通常最好每个变量使用一行。 并使用更多空间。

 int intrDate=0,cautDate=0,listDate=0,iesire=0,desprStudenti=0,desprProfesori=0,inapoi=0; 

int intrDate = 0;
int cautDate = 0;
int listDate = 0;
int iesire = 0;
int desprStudenti = 0;
int desprProfesori = 0;
int inapoi = 0;

创建MCVE( 最小,完整,可验证的示例 )时,最好删除未使用的变量和类型定义,并消除菜单(只需调用需要执行的代码),等等。

另请参阅远离scanf()新手指南 scanf()函数是最难正确使用的函数之一。 在简单的情况下,它使工作变得轻松,但是当事情开始变得棘手时, scanf()可能是错误的工作工具。

暂无
暂无

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

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