Thread: Is there something wrong here?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Is there something wrong here?

    Hey guys, I'm new to programming and I think I'm having some fun...

    .. we'll see. I cannot for the life of me figure out what's wrong with my program. It takes a cartesian point as input, and is supposed to describe whether that point on an axis or in some quadrant 1-4. I think all my statements follow syntax, the thing compiles just fine, but it only ever gives me an output of "Your point lies in quadrant I". What's so different about that statement that make that always trumps the conditions I set? Please let me know if you see something wrong here.

    Thanks!

    -Tyler

    Code:
    //This program finds if a cartesian input point lies on an axis or two, and what quadrant the point is in.
    
    #include<stdlib.h>
    #include<stdio.h>
    #include<math.h>
    
    int main(void){
    
    double x, y, IQ;
    
    printf("Enter the 'x' coordinate\n");
        scanf("%.2fl", x);
    IQ = 0;
        fflush(stdin);
    
    printf("Enter the 'y' coordinate\n");
        scanf("%.2fl", y);
    
    if(y == 0 && x == 0 ) {
        printf("Your point lies on both the x and y axis, and exists within no quadrants");
    }
    else if ( y == 0 && x == !0 )
        printf("Your point lies on the x axis");
    else if ( y == !0 && x == 0 )
        printf("Your point lies on the y axis");
    else if ( y > 0 && x > 0 )
        printf("Your point lies in quadrant I");
    else if ( y > 0 && x < 0 )
        printf("Your point lies in quadrant II");
    else if ( y < 0 && x < 0 )
        printf("Your point lies in quadrant III" );
    else if ( y < 0 && x > 0 )
        printf("Your point lies in quadrant IV");
    
    return (0);
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    scanf("%.2fl", x);
    You should be passing a pointer to x to read into x using %lf not %fl, e.g.,
    Code:
    scanf("%.2lf", &x);
    Same goes when reading into y.

    Also, this is wrong as fflush is not defined for input streams, or update streams for which the last operation was input:
    Code:
    fflush(stdin);
    By the way, I can see that you made effort to indent your code, which is good, but your indentation look inconsistent. I would have indented your code as:
    Code:
    //This program finds if a cartesian input point lies on an axis or two, and what quadrant the point is in.
    
    #include<stdlib.h>
    #include<stdio.h>
    #include<math.h>
    
    int main(void){
    
        double x, y, IQ;
    
        printf("Enter the 'x' coordinate\n");
        scanf("%.2fl", x);
        IQ = 0;
        fflush(stdin);
    
        printf("Enter the 'y' coordinate\n");
        scanf("%.2fl", y);
    
        if(y == 0 && x == 0 ) {
            printf("Your point lies on both the x and y axis, and exists within no quadrants");
        }
        else if ( y == 0 && x == !0 )
            printf("Your point lies on the x axis");
        else if ( y == !0 && x == 0 )
            printf("Your point lies on the y axis");
        else if ( y > 0 && x > 0 )
            printf("Your point lies in quadrant I");
        else if ( y > 0 && x < 0 )
            printf("Your point lies in quadrant II");
        else if ( y < 0 && x < 0 )
            printf("Your point lies in quadrant III" );
        else if ( y < 0 && x > 0 )
            printf("Your point lies in quadrant IV");
    
        return (0);
    
    }
    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
    Sep 2006
    Posts
    8,868
    fflush(stdin);

    is obsolete. It works only on outgoing streams, not incoming ones.

    Imagine your plumbing as in or out streams. Flushing works on OUT streams. You can't flush the kitchen faucet, but you can flush the drain pipe on the kitchen sink.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Thanks so much guys! I definitely goofed on the placeholders there didn't I.. thank you for the indentation tip. Its better to bump things over to the right another 'tab'?

    Question: If fflush(stdin); is so obsolete and not recognized, why does my program not work without it? The buffer gets clogged and the program skips over the 'if' function and just outputs garbage. You never get the chance to enter the second variable.

    Thanks again for your help! I really appreciate the helpfulness on this board.

    Is there a better way around this?

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The problem is that there is a '\n' still on the buffer

    You can get scanf to read '\n' and do nothing with it by adding this to your scanf:

    Code:
    scanf("%.2fl%*c", x);

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Meatball View Post
    Question: If fflush(stdin); is so obsolete and not recognized, why does my program not work without it?
    You're probably using an out-of-date compiler which just happens to behave such that that particular command happens to do what you wanted.
    Generally though it is an error, albeit a common one, to write that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  2. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  3. Is their something wrong here?
    By Joe123 in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 07:16 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM