Thread: Complete Newbie. Code won't build in CodeBlocks. What am I screwing up?

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    3

    Complete Newbie. Code won't build in CodeBlocks. What am I screwing up?

    It's a beginning assignment to create a program that calculates two different times. It's my first dip into programming and I just feel completely lost. Any tips would be greatly appreciated.

    Code:
    #include <stdio.h>
    struct time
    {
    int hour;
    int minutes;
    int seconds;
    };
    int main (void)
    {
    struct time elapsed_time (struct time t1, struct time t2);
    struct time t1 = {0, 0, 0}, t2 = { 0, 0, 0};
    struct time timediff;
    printf("This program will calculate the difference between two time values\n");
    printf("Enter a space between hours, minutes, and seconds\n");
    printf("For example 02 30 15, for 2:30:15\n\n");
    printf("Enter beginning time: ");
    scanf("%d%d%d", &t1.hour,&t1.minutes,&t1.seconds);
    printf("Enter Ending Time: ");
    scanf("%d%d%d",&t2.hour,&t2.minutes,&t2.seconds);
    timediff = elapsed_time(t1, t2);
    printf ("\nThe difference between the two times %.2i:%2i:%.2i and %.2i:%.2i:%.2i\n,
    t1.hour, t1.minutes, t1.seconds, t2.hour, t2.minutes, t2.seconds");
    printf("\n is %.2i hours %.2i minutes %2i seconds\n", timediff.hours, timediff.minutes timediff.seconds);
    return 0;
    }
    struct time elapsed_time(struct time t1, struct time t2)
    {
    int temp1, temp2; //Used to store hours before and after 12 (am/pm)
    struct time timediff = {0, 0, 0};
    
    timediff.seconds = t2.seconds - t1.seconds; //Subtract seconds
    
    if (timediff.seconds < 0) //Add to minutes if seconds < 0
    {
    timediff.seconds += 60;
    --t2.minutes;
    }
    timediff.minutes = t2.minutes - t1.minutes; //Subtract minutes
    
    if (timediff.minutes < 0) //Add to hours if minutes < 0
    {
    timediff.minutes += 60;
    --t2.hour;
    }
    
    timediff.hour = t2.hour - t1.hour; //Subtract hours
    
    //if hour < 0, need to borrow one day (crossed midnight)
    
    if (t1.hour > t2.hour) //subtract hours. Calculate hours before
    {                      //and after 12 if time crosses. Add together
    temp1 = 12 - t1.hour;
    temp2 = temp1 + t2.hour;
    timediff.hour=temp2;
    return timediff;
    }
    return timediff;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Welcome to the forum.

    First, you should post all errors and warnings from the compiler.

    Next, make sure the code you post is neatly formatted and indented. It should look something like this:

    Code:
    #include <stdio.h>
    
    struct time
    {
        int hour;
        int minutes;
        int seconds;
    };
    
    int main (void)
    {
        struct time elapsed_time (struct time t1, struct time t2);
        struct time t1 = {0, 0, 0}, t2 = { 0, 0, 0};
        struct time timediff;
    
        printf("This program will calculate the difference between two time values\n");
        printf("Enter a space between hours, minutes, and seconds\n");
        printf("For example 02 30 15, for 2:30:15\n\n");
    
        printf("Enter beginning time: ");
        scanf("%d%d%d", &t1.hour,&t1.minutes,&t1.seconds);
    
        printf("Enter Ending Time: ");
        scanf("%d%d%d",&t2.hour,&t2.minutes,&t2.seconds);
    
        timediff = elapsed_time(t1, t2);
    
        printf ("\nThe difference between the two times %.2i:%2i:%.2i and %.2i:%.2i:%.2i\n,
                t1.hour, t1.minutes, t1.seconds, t2.hour, t2.minutes, t2.seconds");
        printf("\n is %.2i hours %.2i minutes %2i seconds\n", timediff.hours, timediff.minutes timediff.seconds);
    
        return 0;
    }
    
    struct time elapsed_time(struct time t1, struct time t2)
    {
        int temp1, temp2; //Used to store hours before and after 12 (am/pm)
        struct time timediff = {0, 0, 0};
    
        timediff.seconds = t2.seconds - t1.seconds; //Subtract seconds
    
        if (timediff.seconds < 0) //Add to minutes if seconds < 0
        {
            timediff.seconds += 60;
            --t2.minutes;
        }
    
        timediff.minutes = t2.minutes - t1.minutes; //Subtract minutes
    
        if (timediff.minutes < 0) //Add to hours if minutes < 0
        {
            timediff.minutes += 60;
            --t2.hour;
        }
    
        timediff.hour = t2.hour - t1.hour; //Subtract hours
    
        //if hour < 0, need to borrow one day (crossed midnight)
        if (t1.hour > t2.hour) //subtract hours. Calculate hours before
        {                      //and after 12 if time crosses. Add together
            temp1 = 12 - t1.hour;
            temp2 = temp1 + t2.hour;
            timediff.hour=temp2;
            return timediff;
        }
    
        return timediff;
    }
    I received a couple of warnings and errors:

    Code:
    /*
    main.c|28|warning: missing terminating " character
    main.c||In function 'main':
    main.c|28|error: missing terminating " character
    main.c|29|warning: missing terminating " character
    main.c|29|error: missing terminating " character
    main.c|30|error: expected ')' before 'printf'
    main.c|33|warning: passing argument 1 of 'printf' makes pointer from integer without a cast
    \codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdio.h|252|note: expected 'const char *' but argument is of type 'int'
    main.c|33|error: expected ';' before '}' token
    ||=== Build finished: 4 errors, 3 warnings ===|
    */
    This might look like a lot, but it's likely most of these are caused by the same problem. Let's look at the first warning:

    >> main.c|28|warning: missing terminating " character

    And let's look at the corresponding line(s):

    Code:
    printf ("\nThe difference between the two times %.2i:%2i:%.2i and %.2i:%.2i:%.2i\n,
            t1.hour, t1.minutes, t1.seconds, t2.hour, t2.minutes, t2.seconds");
    Ah, see? You put the closing quote in the wrong spot. Let's fix that.

    Code:
    printf ("\nThe difference between the two times %.2i:%2i:%.2i and %.2i:%.2i:%.2i\n",
            t1.hour, t1.minutes, t1.seconds, t2.hour, t2.minutes, t2.seconds);
    Now we'll compile and see where we're at:

    Code:
    /*
    main.c||In function 'main':
    main.c|30|error: 'struct time' has no member named 'hours'
    main.c|30|error: expected ')' before 'timediff'
    */
    ||=== Build finished: 2 errors, 0 warnings ===|
    Notice how that one fix removed most of the warnings/errors. Now we just two errors left. The first should be obvious ... your "time" struct has a member called "hour", but you're trying to refer to it as "hours" in your print statement. Let's remove that 's' and see what's left:

    Code:
    /*
    main.c||In function 'main':
    main.c|30|error: expected ')' before 'timediff'
    ||=== Build finished: 1 errors, 0 warnings ===|
    */
    It says it's expecting a closing parenthesis. But if we carefully analyze that line, we see that all the parenthesis seem to line up:

    Code:
    printf("\n is %.2i hours %.2i minutes %2i seconds\n", timediff.hour, timediff.minutes timediff.seconds);
    But wait, during our careful analysis, we notice that something else is missing, which was causing the compiler to complain:

    Code:
    printf("\n is %.2i hours %.2i minutes %2i seconds\n", timediff.hour, timediff.minutes, timediff.seconds);
    Most compilers do a pretty good job of describing the cause of the error, but it's up to the programmer to identify the exact cause of the problem.

    Now the code should compile cleanly.



    Note that I did not go into analyzing your code, I just wanted to help you get it to compile. You should fix it up and start testing it yourself. If something else goes wrong, and you can't figure it out, feel free to ask about it.

    It's good practice to compile and test as you go, instead of writing most of your code all at once. For an example in building up a program incrementally, testing as you go, see here: A development process

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    3
    Thank you so much. You're already a better help than the instructor who emailed me back with "Just watch my tutorial video." I'll play around with it.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    You're welcome.

    Just to emphasize, the "development process" link I posted above describes one of the most powerful techniques for writing good, working programs, especially at the beginner level. It's a simple concept, but is often overlooked. If you just take one thing away with you today, let it be that.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    3
    I've gone through and made the changes but when I compile and build it in Code Blocks all I get is Linking stage skipped (build target has no object files to link)

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    It sounds like your file might not be part of a project. Make sure you have a console project open, and make sure your source file is added to the project.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    3,903
    Quote Originally Posted by Matticus View Post
    It sounds like your file might not be part of a project. Make sure you have a console project open, and make sure your source file is added to the project.
    If that does not fix it; make sure the file with the c or cpp extension has the link property checked.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Complete newbie is lost
    By Boltrig in forum C Programming
    Replies: 1
    Last Post: 11-25-2007, 07:23 PM
  2. complete newbie help
    By terracota in forum Windows Programming
    Replies: 4
    Last Post: 11-11-2004, 05:44 PM
  3. Complete Open GL Newbie
    By Krak in forum Game Programming
    Replies: 21
    Last Post: 05-01-2003, 08:19 AM
  4. Complete programming newbie - C or C++
    By Blobby in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 02-17-2003, 10:14 AM
  5. Replies: 4
    Last Post: 12-07-2002, 04:24 PM

Tags for this Thread