Thread: Program returning negative numbers

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    33

    Program returning negative numbers

    Hey everyone! The results of printing 'circle', switch case #3, comes out as a long negative number, although I'm sure the result should be positive. Am I overflowing? I've tried typecasting as double, long, etc, I can't seem to figure it out. Thanks!

    Code:
    int main(void)
    {
            int choice, x=0, square=0, cube=0, circle=0;
            printf("Welcome to the Area Calculation Program.\n");
            printf("(1)Square\n(2)Cubee\n(3)Circle\n");
            printf("Please make a selection.\n");
            scanf("%d", &choice);
            while(choice<1 || choice>3)
            {
                    printf("Incorrect choice.  Please choose from the 3 options. \n");
                    scanf("%d", &choice);
            }
            switch(choice)
            {
                    case 1:
                            printf("Please enter a positive number. \n");
                            scanf("%d", &x);
                            while(x<=0)
                            {
                                    printf("Please enter a positive number.\n");
                                    scanf("%d", &x);
                            }
                            square=x*x;
                            printf("The side length of the square is %d.\n", x);
                            printf("The area of the square is %d.\n", square);
                            break;
                    case 2:
                            printf("Please enter a positive number. \n");
                            scanf("%d", &x);
                            while(x<=0)
                            {
                                    printf("Please enter a positive number.\n");
                                    scanf("%d", &x);
                            }
                            cube=6*x*x;
                            printf("The side length of the cube is %d. \n", x);
                            printf("The area of the cube is %d. \n", cube);
                            break;
                    case 3:
                            printf("Please enter a positive number. \n");
                            scanf("%d", &x);
                            while(x<=0)
                            {
                                    printf("Please enter a positive number. \n");
                                    scanf("%d", &x);
                            }
                            circle=3.14159*x*x;
                            printf("The radius is %d\n", x);
                            printf("The area of the circle is %lf\n", circle);
                            break;
                    default:
                            printf("Please choose from the 3 options. \n");
                            scanf("%d", &choice);
                            break;
            }
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you declared circle to be an int. You should have declared it to be a double.
    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
    Aug 2015
    Posts
    33
    Thanks! So simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a program that wont allow negative numbers.
    By cathyruth in forum C Programming
    Replies: 1
    Last Post: 09-02-2013, 08:37 AM
  2. Question: Program returning large negative value as output
    By Chilifrybandit in forum C Programming
    Replies: 1
    Last Post: 02-23-2013, 07:40 PM
  3. Negative numbers in C
    By BlaX in forum C Programming
    Replies: 18
    Last Post: 06-29-2009, 06:30 PM
  4. Negative Numbers
    By cgod in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2005, 08:57 AM
  5. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 12:48 AM