Errors when compiling code.

This is a discussion on Errors when compiling code. within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am very new at C programming and I am following the book from O'Reilly called "Practical C" ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    6

    Errors when compiling code.

    Hello everyone,

    I am very new at C programming and I am following the book from O'Reilly called "Practical C" 3rd Edition.

    The exercise at the end of Chapter 1 ask to make a program to print your Name, Social Security number and Date of Birth. This is MY own code I came up with to do this:

    Code:
    /* This program is designed to produce a Name, Date of Birth and a SS number */
    
    
    #include <stdio.h>
    
    
    int dob;        /* Date of Birth */
    int ssn;        /* SS Number */
    int full_name;  /* Full Name */
    
    
    int main()
    
    
    {
    
    
            dob = 1981;
            ssn = 047237554;
            full_name = "John W Doe";
    
    
    }
    
    
    {
    
    
            printf("Hi, my name is %d.", full_name);
            printf("I was born on %d.", dob);
            printf("My SS number is %d.", ssn);
            return (0);
    
    
    }
    When I try to compile the program I get the following error:

    Code:
    Manuel-Velezs-MacBook-Pro:c_files Manolo$ cc -g -o exercise_1 exercise_1.c
    exercise_1.c:14:8: error: invalid digit "9" in octal constant
    exercise_1.c: In function ‘main’:
    exercise_1.c:15: warning: assignment makes integer from pointer without a cast
    exercise_1.c: At top level:
    exercise_1.c:19: error: expected identifier or ‘(’ before ‘{’ token
    I feel like I'm missing something really obvious and I'm gonna feel stupid once I can get an idea of what's going on but I love trying to learn this stuff! Thank you in advance for everyones help!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    When you start an integer constant with a 0 it is considered to be in octal (base-8) and therefore can only have the digits 0 to 7, inclusive. You must have changed something, though, as that error does not occur with the code you posted. Did you originally have a 0 before the 1981? like 04211981 or something like that? Also, you need to remove the leading zero in your ssn, or change its representation to a string.

    full_name is obviously not supposed to be an integer, since "John W Doe" is not an integer; it is a string. For your current purpose, try a char* (a character pointer). And in printing it out with the printf, use %s (for a string) rather than %d (for an integer).

    And you need to remove the close-brace, open-brace in the middle of your code. The close brace closes off your main, which you do not want to do at that point.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    6
    Ok so I scraped the other code and started over and I still can't get this. I'm sorry for all the obvious errors I might be making I'm just trying to go by the book but I'm not getting these parts that I am messing up. Here is the new code I created:

    Code:
    /* This program is designed to produce a Name, Date of Birth and a SS number */
    
    
    #include <stdio.h>
    
    
    /* Desfine Variables */
    
    
    char first_name;                /* First Name of User */
    char middle_name;       /* Middle Name of User */
    char last_name;         /* Last Name of User */
    int year_born;          /* Birth Date */
    int current_year;       /* Current Year */
    int age;                /* Age to Date */
    int ssn_1;              /* First three numbers of your SS */
    int ssn_2;              /* Middle two numbers of your SS */
    int ssn_3;              /* Last four numbers of your SS */
    
    
    int main()
    {
            year_born = 1983;
            current_year = 2012;
            age = current_year - year_born;
            first_name = John;
            middle_name = W;
            last_name = Doe;
            ssn_1 = 045;
            ssn_2 = 68;
            ssn_3 = 3257;
    
    
                    printf("Hello, my name is %s\n%s\n%s", first_name,middle_name,last_name);
                    printf("This year I'm going to %d years old", age);
                    printf("My SS number is %d\n%d\n%d\n", ssn_1,ssn_2,ssn_3);
                    return(0);
    }
    Now I get the following error:

    Code:
    Manuel-Velezs-MacBook-Pro:c_files Manolo$ cc -g -o exercise_1 exercise_1.c
    exercise_1.c: In function ‘main’:
    exercise_1.c:22: error: ‘John’ undeclared (first use in this function)
    exercise_1.c:22: error: (Each undeclared identifier is reported only once
    exercise_1.c:22: error: for each function it appears in.)
    exercise_1.c:23: error: ‘W’ undeclared (first use in this function)
    exercise_1.c:24: error: ‘Doe’ undeclared (first use in this function)
    exercise_1.c:29: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    exercise_1.c:29: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    exercise_1.c:29: warning: format ‘%s’ expects type ‘char *’, but argument 4 has type ‘int’
    exercise_1.c:29: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
    exercise_1.c:29: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    exercise_1.c:29: warning: format ‘%s’ expects type ‘char *’, but argument 4 has type ‘int’
    I thought by maybe changing the variables around it might work but I guess I'm using them wrong.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,552
    Read this link strncpy - C++ Reference

    Edited code to show one of the ways to do this; note, I only fixed the string assignment issue in one place.
    Code:
    /* This program is designed to produce a Name, Date of Birth and a SS number */
    
    
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_NAME_LENGTH 31
    
    
    /* Desfine Variables */
    
    
    char first_name[MAX_NAME_LENGTH + 1];  /* First Name of User */
    char middle_name[MAX_NAME_LENGTH + 1]; /* Middle Name of User */
    char last_name[MAX_NAME_LENGTH + 1];   /* Last Name of User */
    int year_born;          /* Birth Date */
    int current_year;       /* Current Year */
    int age;                /* Age to Date */
    int ssn_1;              /* First three numbers of your SS */
    int ssn_2;              /* Middle two numbers of your SS */
    int ssn_3;              /* Last four numbers of your SS */
    
    
    int main()
    {
            year_born = 1983;
            current_year = 2012;
            age = current_year - year_born;
            strncpy (first_name,"John",MAX_NAME_LENGTH);
    /*
            middle_name = W;
            last_name = Doe;
    */
            ssn_1 = 045;
            ssn_2 = 68;
            ssn_3 = 3257;
    
    
                    printf("Hello, my name is %s\n%s\n%s", first_name,middle_name,last_name);
                    printf("This year I'm going to %d years old", age);
                    printf("My SS number is %d\n%d\n%d\n", ssn_1,ssn_2,ssn_3);
                    return(0);
    }
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    6

    Reply::

    Thank you for your help! I re did it the way you said and make the changes and it works. I have to read up on what you did because in the book we did not get that far in using those things you used in the code but I will study them and make sure I learn them. Again, thank you so much for your help!

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    6

    Talking Reply 2

    Hey I got it to work a different way! I think I was trying to do too much too soon in the previous post I posted above! I did the following and it worked perfect with no errors:

    Code:
    /*
     *
     *Program created to print your Name, Date of Birth and SS Number
     *
     */
    
    
    #include <stdio.h>
    
    
    int ssn1;       /* First set of SSN */
    int ssn2;       /* Second set of SSN */
    int ssn3;       /* Third set of SSN */
    int year1;      /* Current Year */
    int year2;      /* Year Born */
    int age;        /* Age Turning in Current Year */
    
    
    int main()
    {
    
    
    /*
     * Set up of SSN veriables.
     */
    
    
            ssn1 = 145;
            ssn2 = 23;
            ssn3 = 3443;
    
    
    /*
     *Set up of Year veriables.
     */
    
    
            year1 = 2012;
            year2 = 1983;
    
    
    /*
     *Set up of Age veriable.
     */
    
    
            age = year1 - year2;
    
    
    /*
     *Set up of Print Statements.
     */
    
    
                    printf("Hello, my name is Manny I. Velez\n");
                    printf("I was born on November 2nd, %d\n", year2);
                    printf("This year, I'm going to be %d\n", age);
                    printf("My Social Security number is %d %d %d\n", ssn1,ssn2,ssn3);
    
    
    /*
     *End Program Statement
     */
                    return (0);
    }
    But thank you for your help!!! I'm reading on the features you stated above and know it a tiny bit better now in what you did. I know I'll run into this in the near future and at least will have a heads up on it! Thanks!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with errors in compiling
    By m-kosier in forum C Programming
    Replies: 5
    Last Post: 11-29-2011, 05:31 PM
  2. Need Help in Compiling errors
    By sick in forum C Programming
    Replies: 2
    Last Post: 01-21-2010, 02:26 AM
  3. Compiling errors
    By courtesan in forum C Programming
    Replies: 2
    Last Post: 08-18-2005, 10:52 AM
  4. Compiling using g++ and getting errors...
    By alvifarooq in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2004, 08:36 AM
  5. c++ compiling errors... very odd!
    By renderstream in forum C++ Programming
    Replies: 9
    Last Post: 03-07-2004, 04:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21