Thread: Starting with arrays and getting error!! Please get me help

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    28

    Starting with strucutres and getting error!! Please get me help

    Here is my header file code (structure.h)


    Code:
    struct user {
    
    
        int userId;
        char firstname[20];
        char lastname[20];
        int age;
        float weight;
    
    
    };
    And here is my main program code

    Code:
    //Programming in C (Structural)
    
    
    /*
     *
     * @author Dushyant Kaushik
     * 
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "strucutre.h"
    
    
    char name[25];
    void addusr_Wizard(char name[25]);
    int main(void)
    
    
    {
        system("clear");
        char c;
        
        printf("Want to add a user? (Y OR N)");
        
        scanf("%c", &c);
        
        if(c == 'Y') 
        {
            printf("\nEnter name of the user: ");
            scanf("%s", &name);
            addusr_Wizard(name);
        } else 
        {
            printf("OK! As you wish!");
            break;
        }
        
        getchar();
        return 0;
    }
    
    
    void addusr_Wizard(char name[25])
    {
        struct user name;
        
        name.firstname = name;
        printf("Enter last name of the user: ");
        scanf("%s", &name.lastname);
        printf("Enter age of user: ");
        scanf("%d", &name.age);
        printf("Enter user id: ");
        scanf("%d", &name.userId);
        printf("Enter weight of the user: ");
        scanf("%f", &name.weight);
        
        exit(0);
    }


    I am getting these arrays in NetBeans


    "/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
    make[1]: Entering directory `/home/iexploit/NetBeansProjects/CProgramming'
    "/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/cprogramming
    make[2]: Entering directory `/home/iexploit/NetBeansProjects/CProgramming'
    mkdir -p build/Debug/GNU-Linux-x86
    rm -f "build/Debug/GNU-Linux-x86/newmain.o.d"
    gcc -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/newmain.o.d" -o build/Debug/GNU-Linux-x86/newmain.o newmain.c
    newmain.c: In function ‘main’:
    newmain.c:28:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[25]’ [-Wformat=]
    scanf("%s", &name);
    ^
    newmain.c: In function ‘addusr_Wizard’:
    newmain.c:43:17: error: ‘name’ redeclared as different kind of symbol
    struct user name;
    ^
    newmain.c:41:25: note: previous definition of ‘name’ was here
    void addusr_Wizard(char name[25])
    ^
    newmain.c:45:20: error: incompatible types when assigning to type ‘char[20]’ from type ‘struct user’
    name.firstname = name;
    ^
    newmain.c:47:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[20]’ [-Wformat=]
    scanf("%s", &name.lastname);
    ^
    make[2]: *** [build/Debug/GNU-Linux-x86/newmain.o] Error 1
    make[2]: Leaving directory `/home/iexploit/NetBeansProjects/CProgramming'
    make[1]: *** [.build-conf] Error 2
    make[1]: Leaving directory `/home/iexploit/NetBeansProjects/CProgramming'
    make: *** [.build-impl] Error 2


    BUILD FAILED (exit value 2, total time: 260ms)
    Last edited by kdushyant297; 09-19-2017 at 09:03 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,294
    1. You have too many things called name.
    2. You don't need & on your variables when using %s to read into a char array.
    3. Put your global variables in main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct user {
        int userId;
        char firstname[20];
        char lastname[20];
        int age;
        float weight;
    };
    
    struct user infomation;
    
    void addusr_Wizard(struct user infomation, char n[])
    {
        strcpy(infomation.firstname, n);
        printf(" name is %s\n", infomation.firstname);
    
        /*
        printf("Enter last name of the user: ");
        scanf("%s", &name.lastname);
        printf("Enter age of user: ");
        scanf("%d", &name.age);
        printf("Enter user id: ");
        scanf("%d", &name.userId);
        printf("Enter weight of the user: ");
        scanf("%f", &name.weight);
        * **/
    }
    
    int main (int argc, char **argv )
    {
    char c[10];
    char n[20];
    
        system("clear");
        printf("Want to add a user? (Y OR N)");
        scanf("%s", c);
    
        if(strcmp( "y" , c) == 0)
        {
            printf("\nEnter name of the user: ");
            scanf("%s", n);
            addusr_Wizard( infomation, n );
        }
         else
        {
            printf("OK! As you wish!\n");
        }
    return 0;
    }

    Code:
    Want to add a user? (Y OR N)y
    
    Enter name of the user: bob
     name is bob
    how's that?
    You'll still need to figure out how to get the rest of your members filled.
    Last edited by userxbw; 09-21-2017 at 12:03 PM. Reason: corrected code

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,294
    > addusr_Wizard( scanf("%s", &n) );
    > .....
    > void addusr_Wizard(char n[25])
    Well that explains your other thread, if you thought this
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Salem View Post
    > addusr_Wizard( scanf("%s", &n) );
    > .....
    > void addusr_Wizard(char n[25])
    Well that explains your other thread, if you thought this
    that is old, removed.. go NOW and look again and see what I did to make it work. not bad for someone that has no idea what he is doing.

    no help from you btw.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    92
    Btw, you intend to use structure.h but in your C-source you wrote
    #include "strucutre.h"

    typing error.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: ISO C++ forbids assignment of arrays
    By JonathanS in forum C Programming
    Replies: 5
    Last Post: 09-19-2011, 10:26 AM
  2. Indexes of arrays in C starting with 1
    By RafaelMarch in forum C Programming
    Replies: 12
    Last Post: 10-30-2009, 01:44 PM
  3. Error with Dynamic Arrays
    By Catalyst8487 in forum C++ Programming
    Replies: 7
    Last Post: 11-27-2008, 05:48 AM
  4. strcpy error with arrays
    By trang in forum C Programming
    Replies: 4
    Last Post: 01-10-2004, 10:13 PM
  5. Arrays/calculation error... *need Help*
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 12:45 AM

Tags for this Thread