Thread: Hello guys

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    196

    Hello guys

    I'm having a slight problem with my program. It keeps showing two answers and it's only supposed to show one.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
        float math(float x, float y);
        float math2(float x, float y);
        float x;
        float y;
        printf("Please enter a x value: ");
        scanf("%d", &x);
        printf("Please enter a y value: ");
        scanf("%d", &y);
        if(0 <= x <10)
        {
        printf("The result is %f.\n", math(x,y));
        }
        else if(x >=10)
        {
        printf("The result is %f.\n", math2(x,y));
        }
        if( y ==0)
        {
        printf("Error: Division by 0!");
        }
        return(0);
    }
    
    
    float math(float x, float y)
    {
        return  (x+1)/y;
    }
    
    
    
    
    float math2(float x, float y)
    {
        return (x-9)/y;
    }
    This is what I get on command prompt after i enter values
    Hello guys-sad-png

    The 1.5 is correct and the answer is correct. I just can't seem to stop the first one from appearing

    These are the instructions
    Hello guys-png

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This expression likely does not do what you intend: (0 <= x <10). Rather, you should break it up and use && to connect the two parts.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    %d in scanf is for integer variables not floats, read formats definitions for scanf

    division by zero should be checked before you are actually trying to divide. Otherwise - it has no point, your program has already crashed.

    Generally - function declarations should be put before main.

    And you should fix your indentation
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Thank you guys so much for your help. I will start working on it once I'm home!

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Ok guys, I got this to run and it now knows which function to use. The problem is that it doesn't show the complete correct answer and im sure it's because of the %d being used.

    For example if the answer is 1.5 it just shows 1 or if it's 0.5 it just shows 0. If i change everything to float though it works.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main()
    {
        int x;
        int y;
        int math(int x, int y);
        int math2(int x, int y);
        printf("Please enter a x value: ");
        scanf("%d", &x);
        printf("Please enter a y value: ");
        scanf("%d", &y);
        if( y ==0)
        {
            printf("Error: Division by 0!");
        }
        if(x>=0 && x<10)
        {
            printf("The result is %d.\n", math(x,y));
        }
        else if(x >= 10)
        {
            printf("The result is %d.\n", math2(x,y));
        }
        return(0);
    }
    
    
    int math(int x, int y)
    {
        return(x+1)/y;
    }
    
    
    
    
    int math2(int x, int y)
    {
        return(x-9)/y;
    }

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    line 19 should be "else if"
    otherwise you still try to do the math when y is zero
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by vart View Post
    line 19 should be "else if"
    otherwise you still try to do the math when y is zero
    Thank you pal.
    Code:
    #include <stdio.h>#include <math.h>
     
     
    int main()
    {
        int x;
        int y;
        int math(int x, int y);
        int math2(int x, int y);
        printf("Please enter a x value: ");
        scanf("%d", &x);
        printf("Please enter a y value: ");
        scanf("%d", &y);
        if( y ==0)
        {
            printf("Error: Division by 0!");
        }
        else if(x>=0 && x<10)
        {
            printf("The result is %i.\n", math(x,y));
        }
        else if(x >= 10)
        {
            printf("The result is %i.\n", math2(x,y));
        }
        return(0);
    }
     
     
    int math(int x, int y)
    {
        return(x+1)/y;
    }
     
     
     
     
    int math2(int x, int y)
    {
        return(x-9)/y;
    }
    I'm still having problems with the answer portion though

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Cdd101 View Post
    I'm still having problems with the answer portion though
    You said you have correct solution when all vars are floats?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Yes pal. If i do 12 and 2 it uses the second formula.

    (12-9)/2=1.5

    By using int it just shows 1.
    If I use float it shows 1.500000.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is to be expected due to integer division.
    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

  11. #11
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by laserlight View Post
    That is to be expected due to integer division.
    Expected this to be the reason why but my teacher wants %d to be used for scanf.

    Would using %i on result be better?

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can use %d in scanf and store the user input in integer variables - but make calculations using floats
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Cdd101
    Expected this to be the reason why but my teacher wants %d to be used for scanf.
    Probably because your teacher expected you to read into integers. You can get floating point division instead of integer division by casting one of the operands to a floating point type.

    Quote Originally Posted by Cdd101
    Would using %i on result be better?
    No, you're still reading into an integer.
    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

  14. #14
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by laserlight View Post
    That is to be expected due to integer division.
    Quote Originally Posted by laserlight View Post
    Probably because your teacher expected you to read into integers. You can get floating point division instead of integer division by casting one of the operands to a floating point type.


    No, you're still reading into an integer.
    Gah, I really don't know how to do this I'm like right at the finish like but can't finish. Don't want to change everything to float and then get a zero.

    Should I just include a remainder solution? By using modulus?

    Or could you guys lead me in the right direction with this? I've included the instructions on the OP.

  15. #15
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Well guys I just ended up turning it in. Thanks for the help, you guys helped me make a few more changes.

    Just one more question, can you multiply by char? The answer is no more than likely. Just asking because the following assignment is asking me to multiply when a digit is used
    Code:
    int main(){
        char a;
             printf("Enter a character: ");
             scanf("%c",&a);
        if(a>='a'&& a<='z')
             printf("%c is a lowercased alphabet letter.",a);
        else if (a>='A' && a<='Z')
             printf("%c is an uppercased alphabet letter.",a);
        else if ( a >= '0' && a<= '9')
             printf("%c is a digit.",a);
        else
            printf("It's a special character.");
        return 0;
    }
    I have got it to here. Now just need to make it multiply.
    Last edited by Cdd101; 09-21-2013 at 09:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help guys..........
    By oldnavyboy06 in forum C Programming
    Replies: 4
    Last Post: 04-09-2009, 02:43 PM
  2. Replies: 5
    Last Post: 12-05-2008, 10:04 PM
  3. Thanks Guys...
    By frodonet in forum C Programming
    Replies: 0
    Last Post: 11-03-2007, 08:16 PM
  4. Hello guys =)
    By carol.prime in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-22-2006, 10:25 AM
  5. please help guys!
    By Owain1602 in forum C Programming
    Replies: 7
    Last Post: 03-02-2004, 04:06 PM