Thread: Head First C geo2jason problem

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

    Question Head First C geo2jason problem

    OK, I really starting to feel stupid. I have been trying to post a bit of code for the last 15 minutes while getting the "Please wrap your code in code tags" warnings at every Preview. I first typed the code tags in manually with the word code between a "[" and a "]". I then edited the font size, asked for a preview and was pointed to the thread describing the use of code tags. They looked exactly like mine so I decided the problem was that I had edited the font size and the board s/w was being confused.

    Next, I pasted in the code with the "Paste as plain text" function. Still between my manually created code tags (which can't be shown as the board s/w wants to make them into actual tags... Same warning...

    Next I tried simply pasting in the code and using the "Wrap your code in code tags" function. Same warning...

    BTW, If the tags here are case-sensitive, it will be the first board I've that is. Thus [code] and [CODE] should be equivalent.

    So, what is the deep dark secret hand-shake for posting code?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    There is a beginning code tag and an closing code tag. Also if you use the Advanced editor there is a icon for inserting code tags.
    [code] Here is the code [/code]

    Jim

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    6

    One more try...

    OK, I think I've found the "code is in this post" trigger. It's having any curly braces in it. Below this code is an example of what the output should be. That output uses those at each end of the text string. If I removed those, the board s/w would accept my post. Or I can just wrap the example in code tags... Oh well...


    I'm beginning to think I need to start a new search for something I can understand! Found this forum when I discovered an example in this book needed a little help to even work. I'm now wondering if I should NOT be using Xcode to work on the examples.


    If someone could paste the following code into a "Command Line Tool" project and make the Type as C, then run the code and let me know if there is correct output in the Console, I would be forever indebted. Well, certainly until the next time I need help, anyway...
    Code:
    #include <stdio.h>
    
    int main()
    {
        float latitude;
        float longitude;
        char info[80];
        int started = 0;
    
        puts("data=[ ");
        while (scanf("%f, %f, %79[ ^\n]", &latitude, &longitude, info) == 3)
        {
            if (started)
            {
                printf(",\n");
            } else {    /* These braces are Not in original code, but I
                 don't think that is the cause of the problem.
                 I have the same issues with or without them. */
                started = 1;
            }
            printf("{latitude: %f, longitude: %f, info: '%s'}", latitude, longitude, info);
        }
        puts("\n]");
    
        return 0;
    }
    The input expects numbers (type float) for the Lat and Long: 42.363400 and -73.296301, for example. The last input is a short string like Speed = 21. The output for the example input would be:
    Code:
    {latitude: 42.363400, longitude: -73.296301, info: 'Speed = 21'}
    for each entry of the three items.


    Thanks for any help or suggestions.
    Last edited by Jim Chaffin; 05-11-2012 at 03:06 PM.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    6
    I made some additional print statements to verify what I thought was happening; that the value of 'started' was never being changed to "1". Here are the inserted sprint statements:
    Code:
    if (started)        {
                printf(",\n");
    printf("started = %d @ line 24\n", started);
            } else {
                started = 1;
            }
    printf("started = %d @ line 28\n", started);
    printf("{latitude: %f, longitude: %f, info: '%s'}", latitude, longitude, info);
        }
    puts("\n]");
    printf("started = %d @ line 31\n", started);
    Any output I get always has 'started' still with a value of zero. Thus the if condition is always false. Perhaps I should use "else if (!started)"? DOH! Nope.
    Last edited by Jim Chaffin; 05-11-2012 at 03:23 PM.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're main problem is an extra space in this format specifier %79[ ^\n]. There can't be a space before the ^ symbol.
    Also (but you probably know this) your input must be separated with commas.

    And your code is still not posting properly! It probably has extra formatting in it. Try pasting it into notepad (or whatever). If it looks good there, recopy it and paste that.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    6
    I've been editing and changing the code as you've been answering. Hopefully the code looks ok to you by now, it appears OK, on my end in Safari.

    However, even after your first response, I inexcusably failed to thank you for reading my post! I apologize. I have posted on some programming forums and been severely castigated for asking stupid questions or for not seeing the error staring me in the face. Needless to say, I don't visit those places often and never ask for help, anymore. So I am sensitive to forum etiquette and have no excuse for not thanking you earlier.

    Regardless of the code formatting, you saw what I could not detect in the PDF version of the book! For some reason, there is extra space at the end of both square braces! It looks like it is Courier. Just never noticed it before! I'm having cataract surgery in June, maybe I'll see stuff like this after that! But, thank you, thank you, thank you for your help!!! I started studying Objective-C and a couple suggested I take a brief course in plain-jane C first. Then I run into something like this! Oh well...frustration can be a learning experience...I guess.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Although the code you posted in post#4 looks "ok", it does not look correct as compared to other code on the site (look in another post to see what it should look like). I suspect extra formatting. Copy it from Xcode and paste it into a plain text editor (whatever there is available on apple). See if there is extra formatting. If there is, then you've solved the problem. If there isn't, then it's still possible it was "removed" before being pasted into the plain text editor. So try copying the text from there and posting that to the board.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    6
    Reformatted the code to be sure it was plain text, then wrapped it in the cod tags. The code now works, thanks to jimblumberg spotting the extra space!
    Code:
    #include <stdio.h>
    
    int main()
    {
        float latitude;
        float longitude;
        char info[80];
        int started = 0;
        
        puts("data=[");
        while (scanf("%f, %f, %79[^\n]", &latitude, &longitude, info) == 3)
        {
            if (started)
            {
                printf(",\n");
            } else {
                started = 1;
            }
        printf("{latitude: %f, longitude: %f, info: '%s'}", latitude, longitude, info);
        }
        puts("\n]");
        
        return 0;
    }
    Thanks again for the help. Have I finally got the code looking correctly?
    Last edited by Jim Chaffin; 05-11-2012 at 04:28 PM. Reason: I see the difference! Even has the line numbers!

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes, your post #8 is looking great!
    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.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    6
    Glad the final CODE listing is working. Takes a little more effort here but the result is better looking.

    I'll next try the image insertion function to show a small screen shot of the code in the book I'm working with. As it is a rather old book, there is no longer a forum at the publisher nor any downloadable files, if there ever were any. My point, if the image is inserted, will show the extra space after both a left and right square brace character. The space is so large that it appeared to be an actual 'space' character. That has not been a problem until it was in the %79[ ^\n] " area.
    Head First C geo2jason problem-deadspace_braces-jpg
    Thanks again for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doing my head in...
    By Matty_Alan in forum C Programming
    Replies: 3
    Last Post: 04-21-2010, 05:16 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. X11 head to i2c bus
    By maes in forum Linux Programming
    Replies: 0
    Last Post: 07-25-2007, 05:28 AM
  4. this is doing my head in!!
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2005, 05:45 AM
  5. Head Nod...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-16-2002, 12:58 PM