Thread: Another program (try)

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    24

    Another program (try)

    Hi,

    I would like to write a program which takes a double as parameter and has the following functionality:
    If the passed value is <= 0 | 0 is returned
    If the passed value is > 0 | the passed value is returned unchanged

    I have written this code:

    Code:
    #include <stdio.h>
    
    double rel(double n1) {
         {
            if (n1 <= 0)
                n1 = 0;
            else if
                (n1 > 0)
                n1 = n1;
        }
        return n1;
    }
    
    double read_data() {
        double i;
        scanf("%lf", &i);
        return i;
    }
    
    
    double main() {
    double i = read_data;
    printf("%.2f", double i, rel(double i));
    }
    But somwhere is a problem because i can not compile the code.

    Thanks for your help

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    Quote Originally Posted by jasmin89
    But somwhere is a problem because i can not compile the code.
    What is the compile error? It would come with a line number that tells you where the error was detected. This is not always where the mistake was made, but it can give you a starting point. In this case, the errors do lie where the error message line numbers say, e.g., in main you forgot to call a function, and then you placed what looks like declarations where it looks like you wanted to provide arguments to a function call.

    Note that you can simplify your rel function, e.g.,
    Code:
    double rel(double n1) {
        if (n1 < 0)
            n1 = 0;
        return n1;
    }
    or even:
    Code:
    double rel(double n1) {
        return (n1 < 0) ? 0 : n1;
    }
    You don't need the extra braces and assigning n1 to itself is rather pointless.

    Also, the return type of the main function is int, not double.

    After you have fixed these problems, you may still find another. That's alright: just keep fixing.
    Last edited by laserlight; 11-05-2020 at 03:25 PM.
    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

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    Ok, i got this compile error at line 22:
    error: incompatible types when initializing type 'double' using type 'double' (*)()'
    line23: error: expected expression before 'double'

    Do you mean that here in the main function is int:?

    Code repaired
    Code:
    int main() {
    double i = read_data;
    printf("%.2f", double i, rel(double i));
    }


    I used double because i have a double as parameter


    Thanks for your help

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,620
    You don't mention the types when you call a function.
    printf("%.2f = %.2f", i, rel(i));
    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.

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    72
    Code:
    printf("%.2f", double i, rel(double i));
    When passing an argument to a function you don't pass the type, only the name. Also when you pass 2 parameters to printf you need 2 format specifiers.
    Code:
    printf("i=%.2f\trel=%.2f", i, rel(doublei));

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    Ok i used this repaired code:

    Code:
    #include <stdio.h>
    
    
    
    double rel(double n1) {
         {
        if (n1 < 0)
            n1 = 0;
        return n1;
        }
    }
    
    double read_data() {
        double i;
        scanf("%lf", &i);
        return i;
    }
    
    
    int main() {
    int i = read_data;
    
    printf("%.2f = %.2f", i, rel(i));
    }
    For main i used int. But i have no compiling error but the program doesn't work.
    And i don't know why.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,620
    1. Make i a double.
    2. Notice the use of () on every other function you call.
    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.

  8. #8
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    But when i make i to a double i get this error in line21:


    Code:
    int main() {
    double i = read_data;
    
    printf("%.2f = %.2f", i, rel(i));
    }
    Error: incompatible types when initializing type 'double' using type 'double' (*)()'
    Last edited by jasmin89; 11-06-2020 at 05:54 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    I'm a little surprised that the compiler didn't complain about the conversion from function (pointer) type to int when it correctly complained about the conversion to double. Might be a relic of the old days: are you compiling with compile warnings turned on to a high level?

    Anyway, the problem is that you're not calling the function. Call it:
    Code:
    double i = read_data();
    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

  10. #10
    Registered User
    Join Date
    Nov 2020
    Posts
    24
    Ok, fine.

    When i type in now -0.05 i got this result: -0.05= 0.00

    How can i write this output
    Code:
    printf("%.2f = %.2f", i, rel(i));
    to get this result: 0
    I only want the output as result...

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    Well, that means you have no clue what the printf is about. You need to read up about it so you can understand it and change it yourself. This is so simple that if we have to hand hold you to fix it, you might as well just quit programming.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-25-2020, 05:06 PM
  2. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  3. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  4. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  5. Replies: 5
    Last Post: 08-16-2007, 11:43 PM

Tags for this Thread