Thread: Code problem or compiler problem....?

  1. #1
    Registered User
    Join Date
    Mar 2015
    Location
    Virginia
    Posts
    2

    Question Code problem or compiler problem....?

    Greetings. Looking for some help learning what the problem is here. I am using an OLD Programming in C book (25yrs old, in fact), and I dunno if anything may have changed. The compiler I have been using is CodingGround.

    My HW assignment is as follows:

    "Calculate area of a house (up to four rectangle rooms)".

    Seems simple enough, but I am failing left, right and center. Trying to set up an input to let user enter # of rooms, then ask L & W measurements for each room until entries = rooms and then add up each room's area and print it out.

    First attempt:
    Code:
    #include <stdio.h>
    main ()
    {
    int i, Rm;
    int T_area = 0;
    float Length, Width, Area;
    printf("How many rooms to include in calculation?");
    scanf("%d", &Rm);
    For(i = 1; i <= Rm; ++i)
    {
    printf("Enter Length for Room #%d:",i);
    scanf("%f", &Length);
    printf("Enter Width for Room #%d:",i);
    scanf("%f", &Width);
    Area = Length * Width;
    T_area = T_area + Area;
    }
    printf("Total area of rooms = %f", T_area);
    return 0;
    }
    Fail.


    Second attempt (commented out For loop, went all in on While):


    Code:
    #include <stdio.h>
    main ()
    {
    int i, Rm;
    int T_area = 0;
    float Length, Width, Area;
    printf("How many rooms to include in calculation?");
    scanf("%d", &Rm);
    /*For(i = 1; i <= Rm; ++i)*/
    i=1;
    While (i <= Rm)
    {
    printf("Enter Length for Room #%d:",i);
    scanf("%f", &Length);
    printf("Enter Width for Room #%d:",i);
    scanf("%f", &Width);
    Area = Length * Width;
    T_area = T_area + Area;
    ++i
    }
    printf("Total area of rooms = %f", T_area);
    return 0;
    }
    Double fail.

    Both attempts give same errors:
    Code:
    gcc -o main *.cgcc -o main *.c
        expected ; before { (line 12)
    While tweaking this around, i occasionally got some other random error...compiler seems a little...moody. If there is a better one i am all about switchin...as long as its free, yo.

    Thanks to anyone who can lend some help...i think i am a few pointers away from getting on top of this and start learning from successes, rather than drinking due to failures.

    Cheers.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, C has changed in the last 25 years. There was a C standard developed in 1989/1990, then another in 1999 and a third in 2011. One thing that hasn't changed, however, is that keywords (like "for" and "while") have always been entirely lower case :P.

    EDIT: Note that if you have a decent code editor (many free options, either stand alone or part of your IDE), it will have syntax highlighting. This would make your error obvious, as For and While would not be highlighted the same as other keywords, but rather like a function/variable/identifier; this would be a huge visual clue.

    When you find yourself asking "is it my code or is the compiler wrong", you should almost always assume it's your code (or maybe you have the wrong compiler flags, i.e. using a C99 feature without enabling C99 compiling). That's probably true even for "moody" compilers, or even for (the much despised) Turbo C compiler.

    I don't know much about this Coding Ground compilers, however there are a number of free compilers. Some recommendations: Clang and GCC are popular for Linux. MinGW is a popular GCC port for Windows; it is often used with the Code::Blocks IDE. Pelles C is a Windows compiler & IDE that is also popular, and has things like resource editors for making Windows apps. Even MS Visual C++ has a free version that compiles C, though I don't recommend it because MS refuses to keep up with C standards, they only support the 1989/1990 standard.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh, and definitely get an updated book. If the book you're referring to is Kernighan and Ritchie's "The C Programming Language", it's a great reference book for core C stuff, but you need to be aware that it's out of date, and should have a modern reference/textbook at hand as well.

    Also, whatever compiler you pick, make sure you always compile at the maximum warning level (e.g. -Wall option for gcc, else check your compiler documentation). That will help you find many problems that are legal C code, but are likely to cause your program to misbehave.

  4. #4
    Registered User
    Join Date
    Mar 2015
    Location
    Virginia
    Posts
    2
    Thank you very much anduril....lower case...oh man. I guess i am spoiled as a primary VBA coder where letter case isn't so unforgiving.

    It works now, does exactly what i wanted it to do. I am still getting this error: "gcc -o main *.c error", which doesn't google very well, so i think i may just drive on with it as is.

    As for my book, it is "Programming in C", by Kochan. Seems ok, was newest book i could find @ library (published in 1990).

    As for the code, it looks like this now:

    Code:
    #include <stdio.h>
    int main ()
    {
    int i, Rm;
    float T_area=0;
    float Length, Width, Area;
    printf("How many rooms to include in calculation?");
    scanf("%d", &Rm);
    for(i = 1; i <= Rm; ++i)
    {
    printf("Enter Length for Room in sq. ft #%d:",i);
    scanf("%f", &Length);
    printf("Enter Width for Room in sq. ft #%d:",i);
    scanf("%f", &Width);
    Area = Length * Width;
    T_area = T_area + Area;
    printf("Current area in sq. ft = %.2f\n", T_area);
    }
    printf("Total area of rooms in sq. ft = %.2f\n", T_area);
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I don't recognize that error, usually there is more information, like line number and specific error message. If you're still using Coding Ground, that's fine. Just copy-paste the output from the terminal after you click compile. I put in the code from your most recent post and saw no errors:
    Code:
    sh-4.3# gcc -std=c99 -o main *.c                                                                                                                                                                                                                       
    sh-4.3#
    You may want to also copy-paste your compiler settings for us (in the upper-right, click "Project" then "Compile options").

    Your code looks better, but it should be properly indented. So, for example
    Code:
    #include <stdio.h>
    int main ()
    {
        int i, Rm;
        float T_area=0;
        float Length, Width, Area;
        printf("How many rooms to include in calculation?");
        scanf("%d", &Rm);
        for(i = 1; i <= Rm; ++i)
        {
            printf("Enter Length for Room in sq. ft #%d:",i);
            scanf("%f", &Length);
            printf("Enter Width for Room in sq. ft #%d:",i);
            scanf("%f", &Width);
            Area = Length * Width;
            T_area = T_area + Area;
            printf("Current area in sq. ft = %.2f\n", T_area);
        }
        printf("Total area of rooms in sq. ft = %.2f\n", T_area);
        return 0;
    }
    You could use a little improvement on your variable names too. Make them more descriptive, so reading your code is like reading plain English. Never sacrifice clarity or readability to save a few keystrokes. Better names might be total_area instead of T_area, or number_of_rooms instead of Rm (here, num_rooms would be an acceptable abbreviation, it's very common and easily understood by the programming community).

    A few other notes:
    1. It's typical in C to start your loop at 0 and use < in your condition (instead of 1 and <=). This is largely because in C, array indexes start at 0 and (for an array of N elements) the last element is at index N-1.
    2. You can do total_area += area; that's a shortcut for total_area = total_area + area; Better yet, you don't even need the area variable, you can just do total_area += length * width;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird problem, compiler problem?
    By sharonch in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2014, 12:08 AM
  2. Calloc() problem on ARM code/compiler
    By ashok449 in forum C Programming
    Replies: 3
    Last Post: 02-15-2013, 09:42 AM
  3. problem with code::blocks compiler
    By iceddragon117 in forum C++ Programming
    Replies: 11
    Last Post: 04-23-2012, 12:06 PM
  4. Dev c++ compiler problem
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-17-2002, 08:26 AM
  5. Problem with compiler
    By knight543 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2002, 09:16 PM