Thread: Help please

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Help please

    Hi, I've been trying to get this program to work for class. I have been having trouble with functions and decided to make the program more simple, but I am still getting errors. Any help would be appreciated.

    This is my program:

    Code:
    #include<stdio.h>
    void readdata (float*, float*, float*, float*, float*);
    float area (float, float);
    float weight (float, float, float, float);
    void printresult (float);
    
    float main(void)
    {
    float q1, t1, d1, id1, od1, ida1, oda1, rim1, w1;
    readdata(&q1, &t1, &d1, &id1, &od1);
    rim1=area(id1, od1);
    w1=weight(q1, t1, d1, rim1);
    printresult(w1);
    return 0;
    }
    
    void readdata (float *q1, float *t1, float *d1, float *id1, float *od1)
    {
    
    printf("Enter the Quantity, Thickness, Density, Inner Diameter, and Outer 
    Diameter for Washer A:\n");
    scanf("%f %f %f %f %f", &q1, &t1, &d1, &id1, &od1);
    }
    
    
    float area (float id1, float od1);
    {
    float ida1, oda1, rim1;
    ida1= ((id1/2)*(id1/2))*3.142
    oda1= ((od1/2)*(od1/2))*3.142
    rim1= oda1-ida1
    }
    
    
    float weight (float q1, float t1, float d1, float rim1)
    {
    w1= q1*t1*d1*rim1
    }
    
    void printresult (float w1);
    {
    printf("The weight of Washer A is: %f\n", w1);
    }

    These are the errors I am getting:

    "benice.c", line 20: newline in string literal
    "benice.c", line 21: syntax error before or at: Diameter
    "benice.c", line 21: invalid source character: '\'
    "benice.c", line 21: newline in string literal
    "benice.c", line 28: syntax error before or at: float
    "benice.c", line 37: undefined symbol: w1
    "benice.c", line 38: syntax error before or at: }
    "benice.c", line 42: syntax error before or at: printf
    cc: acomp failed for benice.c

    Again, any help is appreciated. Thanks a lot.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    when you write out strings " ... " , you cant press [enter] in the middle of writing a string.

    put it all on 1 line

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, mytrademark!



    Simplicity and clarity are good secondary goals for a program - accuracy being foremost.

    Code:
    #include<stdio.h>
    
    void readdata (float*, float*, float*, float*, float*);
    float area (float, float);
    float weight (float, float, float, float);
    void printresult (float);
    
    /* In C, main() returns an int, not a float */
    
    ??float main(void) 
    {
    
    float q1, t1, d1, id1, od1, ida1, oda1, rim1, w1;
    
    /* what's the difference between id1 and ida1?
    
    readdata(&q1, &t1, &d1, &id1, &od1);
    rim1=area(id1, od1);
    w1=weight(q1, t1, d1, rim1);
    printresult(w1);
    return 0;
    }
    
    void readdata (float *q1, float *t1, float *d1, float *id1, float *od1)
    {
    
    printf("Enter the Quantity, Thickness, Density, Inner Diameter, and Outer 
    Diameter for Washer A:\n");
    scanf("%f %f %f %f %f", &q1, &t1, &d1, &id1, &od1);
    }
    
    
    float area (float id1, float od1);
    {
    float ida1, oda1, rim1;
    ida1= ((id1/2)*(id1/2))*3.142
    oda1= ((od1/2)*(od1/2))*3.142
    rim1= oda1-ida1
    /*  add return rim1;      */
    }
    
    
    float weight (float q1, float t1, float d1, float rim1)
    {
    /* declare w1: float w1;  */
    w1= q1*t1*d1*rim1
    /*  add    return w1;   */
    }
    
    void printresult (float w1);
    {
    printf("The weight of Washer A is: %f\n", w1);
    }
    make those changes and re-compile. With some compilers, you need to add include math.h, to get the floating point "package" of the compiler, to link in, but try it with and without, and see what you get.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rodrigorules View Post
    when you write out strings " ... " , you cant press [enter] in the middle of writing a string.

    put it all on 1 line
    Of course you can....
    Try this...
    Code:
    char fred[] = "My name \n is fred \n and I will be \n your friend";
    puts(fred);
    
    printf("My name \n is fred \n and I will be \n your friend");
    
    puts(" \n My name \n is fred \n and I will be \n your friend");

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    [QUOTE=Adak;979208Simplicity and clarity are good secondary goals for a program - accuracy being foremost.[/QUOTE]

    I keep looking at this line:
    Code:
    printf("Enter the Quantity, Thickness, Density, Inner Diameter, and Outer 
    Diameter for Washer A:\n");
    Which appears to be the one it's complaining aobut... Do you think it's tripping over the "A:\" in there? I don't see anything else wrong with it....

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    I keep looking at this line:
    Code:
    printf("Enter the Quantity, Thickness, Density, Inner Diameter, and Outer 
    Diameter for Washer A:\n");
    Which appears to be the one it's complaining aobut... Do you think it's tripping over the "A:\" in there? I don't see anything else wrong with it....
    No, that is fine because '\n' is a valid escape sequence. Ironically, you don't see anything wrong with it because you wrongly denied rodrigorules' point in post #2. rodrigorules is absolutely correct. The problem is that the string literal is incorrectly split across two physical source lines.

    One solution is to make use of the fact that two adjacent string literals will be automatically concatenated:
    Code:
    printf("Enter the Quantity, Thickness, Density, Inner Diameter, and Outer "
        "Diameter for Washer A:\n");
    Another solution is to escape the end of line:
    Code:
    printf("Enter the Quantity, Thickness, Density, Inner Diameter, and Outer \
    Diameter for Washer A:\n");
    Of course, yet another solution is to simply use rodrigorules' suggestion, but for aesthetic reasons that is not always feasible.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No, that is fine because '\n' is a valid escape sequence. Ironically, you don't see anything wrong with it because you wrongly denied rodrigorules' point in post #2. rodrigorules is absolutely correct. The problem is that the string literal is incorrectly split across two physical source lines.
    What's this? Egg on my face? Of course... how did I miss that?

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Thanks for all the help, there are just a few syntax errors left that I can't figure out.

    Code:
    #include<stdio.h>
    void readdata (float*, float*, float*, float*, float*);
    float area (float, float);
    float weight (float, float, float, float);
    void printresult (float);
    
    int main(void)
    {
    float q1, t1, d1, id1, od1, ida1, oda1, rim1, w1;
    readdata(&q1, &t1, &d1, &id1, &od1);
    rim1=area(id1, od1);
    w1=weight(q1, t1, d1, rim1);
    printresult(w1);
    return 0;
    }
    
    void readdata (float *q1, float *t1, float *d1, float *id1, float *od1)
    {
    
    printf("Enter the Quantity, Thickness, Density, Inner Diameter, and Outer" 
    "Diameter for Washer A:\n");
    scanf("%f %f %f %f %f", &q1, &t1, &d1, &id1, &od1);
    }
    
    
    float area (float id1, float od1);
    {
    float ida1, oda1, rim1;
    ida1= ((id1/2)*(id1/2))*3.142
    oda1= ((od1/2)*(od1/2))*3.142
    rim1= oda1-ida1
    return rim1;
    }
    
    
    float weight (float q1, float t1, float d1, float rim1)
    {
    float w1;
    w1= q1*t1*d1*rim1;
    return w1;
    }
    
    void printresult (float w1);
    {
    printf("The weight of Washer A is: %f\n", w1);
    }
    "benice.c", line 28: syntax error before or at: float
    "benice.c", line 45: syntax error before or at: printf
    cc: acomp failed for benice.c


    Again, thanks in advance for all the help.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    float area (float id1, float od1);
    ...
    void printresult (float w1);
    Lose the ;s

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    ida1= ((id1/2)*(id1/2))*3.142
    oda1= ((od1/2)*(od1/2))*3.142
    rim1= oda1-ida1
    What's missing from these lines?

    Forgive my directness, but these are simple proof reading errors you should be catching without our help...

Popular pages Recent additions subscribe to a feed