简体   繁体   中英

C: error in calling struct function in main

Do you have any idea why the scanf in alegereStudent doesn't work? The console let me write random things, in empty lines and is showing random numbers after the printf. I'm trying to do a beginner project but I don't know why this struct won't let me do the scanf as I want.

Code:

#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 ERROR: You need to remove & from scanf in alegereStudent function to read stud.numeStudent and stud.prenumeStudent - you need to learn about arrays and pointers. Plus you need to include <stdio.h> and put ; after return 0 .

Reason for error: an array decays into pointer and scanf needs a pointer to read user input into the variables, hence no need to put & .

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

The code in alegereStudent() modifies the local variable stud , but that's simply a copy of the uninitialized structure that is created in main() — it doesn't affect the structure that is in main() .

For a function to modify a structure in the calling function, you must pass a pointer to the structure. References to members of the structure then use the arrow -> operator instead of the dot . operator. You don't want to print out the old (uninitialized) values; you want to prompt for the new value. And you don't want to pass a char (*)[20] (pointer to an array of 20 characters) where the scanf() function expects a simple char * — drop the & from strings.

One major problem is that you do NOT want a trailing newline in your scanf() format strings . That means the user must type a non-white-space character to stop the input, and people aren't good at predicting what will be needed next. All the conversions you're using skip leading white space, including newlines, so you don't have to worry about the newline left behind (or, if you do want to worry about it, you don't do it with a \\n in the scanf() format string).

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);
}

These formats limit the strings to 19 characters plus a null byte, so you don't get overflows, but they don't ensure that you read (and discard?) any extra bytes. Remember: %s reads up to the next white space character. If the user types Robin Hood on the first line, the first scanf() will read Robin and the next Hood (without waiting for new data typed by the user.

Note also that the semicolon after the function is not required. It marks the end of an empty (variable?) declaration after the body of the function.

You'd need to change the call in the main() function, of course:

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

And it is best not to cram seven variable definitions onto a single line. Indeed, it is usually best to use one line per variable. And use more spaces.

 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;

When creating an MCVE ( Minimal, Complete, Verifiable Example ), it is best to remove unused variables and type definitions and to eliminate menus (simply call the code that needs to be executed), etc.

See also A Beginner's Guide Away From scanf() . The scanf() function is one of the hardest functions to use correctly. For easy cases, it makes life easy, but when things start getting tricky, scanf() is probably the wrong tool for the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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