Very nice! Thanks for the contribution! I had completely forgotten about SWITCH Statements... but then again, I just started learning C and I haven't experimented with them.
It could be simplified a little bit more utilizing one of your methods by simplifying another one of the If Statements:
I didn't test this, but it should work. I just took out like 3 lines only though. >_<Code:#include <stdio.h> #include <conio.h> #include <math.h> #define PI 3.14159 int main() { char buffer[32]; char* info[3] = {"area", "diameter", "radus"}; float area = 0, diameter = 0, radius = 0, value = 0; int selection = 0, valid = 0; clrscr(); printf("Calculate the Area, Radius, or Diameter of a circle\nWhat is the information you have?\n1. Area\n2. Radius\n3. Diameter\nEnter a number 1-3: "); if (fgets(buffer, sizeof buffer, stdin) != NULL && sscanf(buffer, "%d", &selection) && selection > 0 && selection < 4) { printf("What is the value for the %s? ", info[selection - 1]); if (fgets(buffer, sizeof buffer, stdin) != NULL && sscanf(buffer, "%f", &value) == 1) { switch (selection) { case 1 : area = value; radius = sqrt(area / PI); diameter = 2 * radius; break; case 2 : radius = value; area = PI * pow(radius, 2); diameter = 2 * radius; break; default : diameter = value; radius = diameter / 2; area = PI * pow(radius, 2); } printf("Area = %.2f\nRadius = %.2f\nDiameter = %.2f\n", area, radius, diameter); valid = 1; } } else printf("Invalid number!\n"); } else printf("No such option!"); getch(); if (!valid) main(); return 0; }
Results 1 to 15 of 15
Threaded View
- 12 Nov. 2009 02:13pm #11