Like Tree2Likes
  • 1 Post By memcpy
  • 1 Post By stahta01

Code is outputting a random integer, changes every time I run it

This is a discussion on Code is outputting a random integer, changes every time I run it within the C Programming forums, part of the General Programming Boards category; Heya. I have no idea why it's doing this, I've been tweaking and changing the code for the past hour ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    13

    Code is outputting a random integer, changes every time I run it

    Heya. I have no idea why it's doing this, I've been tweaking and changing the code for the past hour and it still won't work.

    Code:
    /* A program to change headings in degrees into bearings */
    
    
    #include <stdio.h>
    
    
    int input(); //gets degrees from user
    char directionmake(); //calculates direction
    int calc(); //calculates degrees to turn after direction
    
    
    int main()
    {
        int degrees;
        degrees = input();
        char direction;
        direction = directionmake(degrees);
        degrees = calc(degrees);
        printf("\n\n%c, \n\n", direction);
        printf("%d", degrees);
        return 0;
    } // program execution
    
    
    int input()
    {
        int degrees;
        printf("Please state the heading in degrees: ");
        scanf("%d", &degrees);
        return degrees;
    }
    
    
    char directionmake()
    {
        char direction;
        int degrees;
        if (degrees < 0)
            printf("Bad input");
        else if (degrees < 90)
            direction = 'N';
        else if (degrees < 270)
            direction = 'S';
        else if (degrees <= 360)
            direction = 'N';
        return direction;
    }
    
    
    int calc()
    {
        int degrees;
        if (degrees > 90 && degrees < 180)
            degrees = degrees - 90;
        else if (degrees > 180 && degrees < 270)
            degrees = degrees - 180;
        else if (degrees > 270 && degrees <180)
            degrees = degrees - 270;
    
    
        return degrees;
    }
    I feel like there's something blatantly wrong, but I can't see it...

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,679
    Learn to pass parameter to functions!

    Try reading this link info http://www.cprogramming.com/tutorial/c/lesson4.html

    Code:
    int calc()
    {
        int degrees; /* local variable that could have random value */
        if (degrees > 90 && degrees < 180)
            degrees = degrees - 90;
        else if (degrees > 180 && degrees < 270)
            degrees = degrees - 180;
        else if (degrees > 270 && degrees <180)
            degrees = degrees - 270;
     
     
        return degrees;
    }
    Last edited by stahta01; 06-28-2012 at 08:20 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Like this?
    Code:
    #include <stdio.h>
    
    
    int input(); //gets degrees from user
    char directionmake(); //calculates direction
    int calc(int degrees); //calculates degrees to turn after direction
    
    
    int main()
    {
        int degrees;
        degrees = input();
        char direction;
        direction = directionmake(degrees);
        degrees = calc(degrees);
        printf("\n\n%c, \n\n", direction);
        printf("%d", degrees);
        return 0;
    } // program execution
    
    
    int input()
    {
        int degrees;
        printf("Please state the heading in degrees: ");
        scanf("%d", &degrees);
        return degrees;
    }
    
    
    char directionmake()
    {
        char direction;
        int degrees;
        if (degrees < 0)
            printf("Bad input");
        else if (degrees < 90)
            direction = 'N';
        else if (degrees < 270)
            direction = 'S';
        else if (degrees <= 360)
            direction = 'N';
        return direction;
    }
    
    
    int calc(int degrees)
    {
        if (degrees > 90 && degrees < 180)
            degrees = degrees - 90;
        else if (degrees > 180 && degrees < 270)
            degrees = degrees - 180;
        else if (degrees > 270 && degrees < 360)
            degrees = degrees - 270;
    
    
        return degrees;
    }
    Edit: Now it passes degrees to it, but it doesn't subtract the numbers.

    Edit: Somehow I fixed it, I have no idea how though.

    This is the working code:
    Code:
    /* Program 4 - A program to change headings in degrees into bearings */
    
    
    #include <stdio.h>
    
    
    int input(); //gets degrees from user
    char directionmake(); //calculates direction
    int calc(int degrees); //calculates degrees to turn after direction
    
    
    int main()
    {
        int degrees;
        degrees = input();
        char direction;
        direction = directionmake(degrees);
        degrees = calc(degrees);
        printf("\n\n%c, \n\n", direction);
        printf("%d", degrees);
        return 0;
    } // program execution
    
    
    int input()
    {
        int degrees;
        printf("Please state the heading in degrees: ");
        scanf("%d", &degrees);
        return degrees;
    }
    
    
    char directionmake()
    {
        char direction;
        int degrees;
        if (degrees < 0)
            printf("Bad input");
        else if (degrees < 90)
            direction = 'N';
        else if (degrees < 270)
            direction = 'S';
        else if (degrees <= 360)
            direction = 'N';
        return direction;
    }
    
    
    int calc(int degrees)
    {
        if (degrees > 90 && degrees < 180)
            degrees = degrees - 90;
        else if (degrees > 180 && degrees < 270)
            degrees = degrees - 180;
        else if (degrees > 270 && degrees < 360)
            degrees = degrees - 270;
    
    
        return degrees;
    }
    Last edited by CornedBee; 02-19-2013 at 04:00 PM. Reason: Remove real name

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > Somehow I fixed it, I have no idea how though.
    That's usually the result of undefined behavior, which your program certainly relies on. You should make the same change you did to "calc" to "directionmake", passing in the integer instead of making a new variable.
    stahta01 likes this.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    13
    Quote Originally Posted by memcpy View Post
    > Somehow I fixed it, I have no idea how though.
    That's usually the result of undefined behavior, which your program certainly relies on
    I can't tell if that's an insult or not D:. Anyways, thanks, I forgot to do the same to directionmake.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,563
    I'm sure it's not an insult, just a statement of fact. We're blunt, not mean.

    As a side note, your calc function is simply a "remainder" or "modulo" function. You want to take degrees and turn it into a value from 0-90, so just do
    Code:
    return degrees % 90;

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,679
    Code:
    return degrees % 90;
    Correction that makes the value 0 to 89; I have no idea what the OP wants if the value is 90.

    Tim S.

    Quote Originally Posted by anduril462 View Post
    I'm sure it's not an insult, just a statement of fact. We're blunt, not mean.

    As a side note, your calc function is simply a "remainder" or "modulo" function. You want to take degrees and turn it into a value from 0-90, so just do
    Code:
    return degrees % 90;
    anduril462 likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Outputting Current Time in C++
    By SeeForever in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2010, 06:29 PM
  2. outputting an integer
    By SniperSAS in forum Windows Programming
    Replies: 4
    Last Post: 12-16-2005, 04:46 PM
  3. outputting characters one at a time
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 08-11-2002, 09:26 AM
  4. Outputting random array entires
    By Syme in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2001, 11:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21