Thread: I need help with function codes...............

  1. #1
    Registered User
    Join Date
    Apr 2015
    Location
    Fortaleza, Brazil
    Posts
    8

    I need help with function codes...............

    Ex 961: Create a function that asks the name of a student to the user and display the information of the student.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    struct student{
        char name[50];
        int stid;
        int age;
    };
    
    
    void func(struct student *stu);
    void func(struct student *s1);
    void func(struct student s2);
    
    
    int main(){
        struct student s1;
        printf("Enter student's name: ");
        scanf("%s",&s1.nome);
        printf("Digite a matricula do estudante: ");
        scanf("%d",&s1.matricula);
         printf("Digite a idade do estudante: ");
        scanf("%d",&s1.idade);
        return 0;
    }
    
    
    void func(struct student s2){
       printf(""Enter student's name thay you wanna have information: ");
       scanf("%s",&s2.name);
       if (s1.name=s2.name) //I wanna compare if they are the same
       func(&stu);//I wanna show the informations 
    }
    
    
    // this funcion is to show the information that are restered before
    void func(struct student *stu) 
    {
      printf("Output\nName: %s",stu.name);
      printf("\nStudent Id: %d",stu.stid);
      printf("\nAge: %d",stu.age);
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Did you have a question?

    Code:
    //I wanna compare if they are the same
    First of all, one = is the assignment operator, and two == is the equality operator.

    But to compare strings, you need to use the "strcmp()" function from "string.h".

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    kikunha

    You declare the function, "func()" three times,
    Code:
    void func(struct student *stu);
    void func(struct student *s1);
    void func(struct student s2);
    and define it twice, but never call either one in main().

    You should name each function with a different name, and only declare and define each function once.

    This program should never compile, at least correctly.

  4. #4
    Registered User
    Join Date
    Apr 2015
    Location
    Fortaleza, Brazil
    Posts
    8

    it doesn't work..........

    Code:
    
    
    Code:
    #include<stdlib.h>
    #include<string.h>
    
    
                struct aluno{
                   char name [40];
                   const char grade[40];
                   const char name2[40];
                   int  student_id;
    
    
                };
    
    
    void setAluno (struct aluno x[]);
    void showAluno (struct aluno x[]);
    
    
    
    
    main(){
    
    
        struct aluno sujeito[2];
    
    
        setAluno(sujeito);
        showAluno(sujeito);
    
    
    }
    
    
    void setAluno (struct aluno x[]){
        int i;
    
    
        for (i = 0; i < 2; i++){
            printf("\nEnter with Student ID %student: ", i+1);
            scanf("%d", &x[i].student_id);
    
    
            printf("Enter with the name %student: ", i+1);
            scanf("%s%*c", &x[i].name);
    
    
            printf("Enter with the grade: ", i+1);
            scanf("%s%*c", &x[i].grade);
        }
    }
    
    
    void showAluno (struct aluno x[]){
        int i;
        char name2;
        printf("\nEnter with the grade: ");
        scanf("%s%*c",name2);
    
    
        for (i = 0; i < 2; i++){
                if(strcmp(name2,x[i].name)==0){
    
    
                printf("Id: %d  Name: %s\nGrade: %s\n" , x[i].student_id, x[i].name, x[i].grade);
                }else{
                printf("dosen't have students in this grade \n\n");
                }
                break;
                }
    
    
        system ("pause");
    }


    it doesn't work..........i wanna show the students that are scribed in one grade

    for example
    id1 : 11
    name 1 : wesley
    grade: Math

    id2: 12
    name2: Robert
    grade: English

    Enter with the grade:
    Math

    id: 11 name: wesley grade:math

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Compile with full warnings.

    Code:
    /*
    main.c|21|warning: return type defaults to 'int'|
    main.c||In function 'setAluno':|
    main.c|39|warning: implicit declaration of function 'printf'|
    main.c|39|warning: incompatible implicit declaration of built-in function 'printf'|
    main.c|39|warning: format '%s' expects type 'char *', but argument 2 has type 'int'|
    main.c|40|warning: implicit declaration of function 'scanf'|
    main.c|40|warning: incompatible implicit declaration of built-in function 'scanf'|
    main.c|43|warning: format '%s' expects type 'char *', but argument 2 has type 'int'|
    main.c|44|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[40]'|
    main.c|47|warning: too many arguments for format|
    main.c|48|warning: format '%s' expects type 'char *', but argument 2 has type 'const char (*)[40]'|
    main.c||In function 'showAluno':|
    main.c|56|warning: incompatible implicit declaration of built-in function 'printf'|
    main.c|57|warning: incompatible implicit declaration of built-in function 'scanf'|
    main.c|57|warning: format '%s' expects type 'char *', but argument 2 has type 'int'|
    main.c|61|warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast|
    \include\string.h|43|note: expected 'const char *' but argument is of type 'char'|
    main.c|57|warning: 'name2' is used uninitialized in this function|
    ||=== Build finished: 0 errors, 15 warnings ===|
    */
    Some of these are from not including "stdio.h".

    Right off the bat, line 55 declares a single char variable, but you're trying to use it as a string.

  6. #6
    Registered User
    Join Date
    Apr 2015
    Location
    Fortaleza, Brazil
    Posts
    8
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    
    
                struct aluno{
    	   	    char nome [40];
    		   	char disciplina[40];
    		   	char discipliana2[40];
    		   	int  matricula;
    		   	
                };
    
    
    void setAluno (struct aluno x[]);
    void showAluno (struct aluno x[]);
    
    
    
    
    main(){
    
    
        struct aluno sujeito[2];
    
    
        setAluno(sujeito);
        showAluno(sujeito);
    
    
    }
    
    
    void setAluno (struct aluno x[]){
        int i;
    
    
        for (i = 0; i < 2; i++){
            printf("\nDigite a matricula do %do aluno: ", i+1);
            scanf("%d", &x[i].matricula);
    
    
            printf("Digite o nome do %do aluno: ", i+1);
            scanf("%s%*c", &x[i].nome);
    
    
            printf("Entre com o nome da disciplina: ", i+1);
            scanf("%s%*c", &x[i].disciplina);
        }
    }
    
    
    void showAluno (struct aluno x[]){
        int i;
        char disciplina2[40];
        printf("\nDigite a disciplina do aluno: ");
        gets(disciplina2);
    
    
        for (i = 0; i < 2; i++){
    			if (strcmp(disciplina2,x[i].matricula)==0){
                printf("Matricula: %d  Nome: %s\nDisciplina: %s\n" , x[i].matricula, x[i].nome, x[i].disciplina);
                }else{
                printf("NAO matriculado\n\n");
                }
    			break;
    			}
    
    
        system ("pause");
    }


    it's almost work.......the second doesn't just the first.........
    matricula=student_id
    disciplina=grade

    i'm compare grades now.............



  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You still have warnings that need to be addressed.

    Code:
    /*
    main.c|21|warning: return type defaults to 'int'|
    main.c||In function 'setAluno':|
    main.c|44|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[40]'|
    main.c|48|warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[40]'|
    main.c||In function 'showAluno':|
    main.c|61|warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast|
    \include\string.h|43|note: expected 'const char *' but argument is of type 'int'|
    ||=== Build finished: 0 errors, 4 warnings ===|
    */
    If you're not seeing these, figure out how to increase the warnings generated by your compiler.

    Look at the very last warning I posted ... it mentions something important about types.

  8. #8
    Registered User
    Join Date
    Apr 2015
    Location
    Fortaleza, Brazil
    Posts
    8
    [code]

    scanf("%s%*c", &x[i].disciplina);

    [\code]

    my problem is here...........so how i can keep 2 names and call them using for..............gets?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Don't use "gets()"!!!
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    Code:
    /*
    string.h|43|note: expected 'const char *' but argument is of type 'int'|
    */
    Code:
    if (strcmp(disciplina2,x[i].matricula)==0)
    What type is "disciplina2"?
    What type is ".matricula"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validate area codes within a list of area codes
    By Staja24 in forum C Programming
    Replies: 9
    Last Post: 05-06-2015, 09:28 PM
  2. codes
    By IfYouSaySo in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 11-18-2005, 03:09 PM
  3. Need some C++ codes...
    By blah569 in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2005, 02:21 PM
  4. Error codes when compiling, using template function
    By LurPak in forum C++ Programming
    Replies: 2
    Last Post: 09-03-2003, 04:12 AM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM

Tags for this Thread