Choices using the if statements

This is a discussion on Choices using the if statements within the C Programming forums, part of the General Programming Boards category; What this program should do is to enter radius then pick choices.... but it doesn't seem to work the problem ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    19

    Choices using the if statements

    What this program should do is to enter radius then pick choices....
    but it doesn't seem to work
    the problem lies with the choice part in which when you type A or a, it will output the radius... when you type B or b circumference... and for C and c --- the area...

    Code:
    #include <stdio.h>
    #include <conio.h>
    #define PI 3.14159
    
    int main(void)
    {
      float r,circ,area;
      char c;
    
      clrscr();
    
      printf ("Input Radius:");
      scanf ("%f",&r);
    
      printf ("Enter Your Choice\n");
      printf ("\n[a] Radius");
      printf ("\n[b] circumference");
      printf ("\n[c] Area of Circle\n");
      scanf ("%f",&c);
      c = getche();
    
    
      if (c == 'a' || c == 'A') {
    	gotoxy (38,12);
    	printf ("The radius of the circle is %f", r);
    	}
      else if (c == 'b' || c == 'B') {
    	 gotoxy (38,12);
    	 circ = 2 * PI * r;
    	 printf ("Circumference: %f", circ);
    	 }
      else if ( c== 'c' || c== 'C') {
    	gotoxy (38,12);
    	area = PI * r *r;
    	printf ("Area: %f", area);
    	}
      else
      {
      gotoxy (38,12);
      printf ("Invalid Choice");
      }
      getch();
      }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    To read in a single character, use
    Code:
    scanf (" %c", &c);
    Note the leading space, that tells scanf to skip all leading whitespace.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    but it doesn't seem to work
    How does it not seem to work? What do you type in for input? What do you get for output? Are the results of the calculations wrong?

    Code:
    char c;
    
    ...
    
    scanf ("%f",&c);
    c = getche();
    Why are you using %f to read in a character? That should be a %c. Also, why would you then immediately throw away the scanned value by overwriting it with the return result from getche? That's probably the problem, you are overwriting the value you scanned earlier in the previous statement.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    19
    thx for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 11:42 AM
  3. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 12:36 PM
  4. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 09:18 AM
  5. Class Function Members and If Statements
    By Team Shadow V2 in forum C++ Programming
    Replies: 10
    Last Post: 02-03-2005, 03:00 PM

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