Thread: Code trouble

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    9

    Code trouble

    I need to have the program calculate the area of a circle from the inputs, converting degrees to radians, and checking for correct inputs such as letters instead of numbers or negative lengths. Here is the code I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define PI 3.1415926
    int main()
    {
      double base;
      double angleD;
      double angleR;
      double area;
      int in;
      char buffer[128];
    
    while (1)
      {
        printf("Would you like to continue?(Y/N)  ");
        if((in = getchar()) == 'Y' || (in = getchar()) == 'y')
          while (1)
          {
            printf("Enter Base Length and Angle in degrees(Less than 90):   ");
            gets(buffer);
            int x = sscanf(buffer, "%lf %lf", &base, &angleD);
            if(x == 2)
              {
                if(base > 0 && angleD > 0)
                {
                  angleR = angleD * PI / 180;
                  area = base * base * tan(angleR) / 2;
                  printf("Area is %7.4f\n", area);
    	      break;
                }
                else
                {
                  printf("No Negative Numbers Please\n");
                }
              }
            else
              {	
                printf("Please enter numbers only.\n");
              }
          }
        else
          {
            printf("Thank you!\n");
            break;
          }
      }
    }
    The problem is I get this when I try to run it as well as not accepting the lower case 'y':

    Would you like to continue?(Y/N) Y
    Enter Base Length and Angle in degrees(Less than 90): Please enter numbers only.
    Enter Base Length and Angle in degrees(Less than 90):

    I know it's a mess but I'm out of ideas and so are the other people I know who have any C experience. If you need anything cleared up let me know

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Well, I suggest converting the input to lowercase to analyze it. Also looks like you are getting your parentheses messed up.

    Code:
    #include <ctype.h>
    
    in = tolower(getchar());
    if (in == 'y')
        ...
    else
        ...

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    9
    Thanks, that fixed the upper and lower 'y' issue.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Also, use fgets() in place of gets(), see the FAQ for why.
    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
    Feb 2009
    Posts
    9
    Thanks for the help. It's not pretty but it will be a grade in the book.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM