mercredi 13 janvier 2021

Brackets are Clearly in the Wrong Place, but WheređŸ€”

Working within "C" at the moment and am struggling with this code. I feel the sequence of what I need with the Switch Statements are there. But when the user input is entered it just alternates between the first two possible answersđŸ’”đŸ˜©

I know it has something to do with where the brackets are placed but I'm struggling to find where. I've moved them around and looked at a bunch of references online for the flow of how switch statements work, but am absolutely struggling.

Would appreciate any help or advice in tackling this problem and understanding the theory of where I'm going wrong.

Attached is the code below:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main () {
    int choice, radius, diameter, choice1a, radiuschoice1, choice2a, choice2b, radiuschoice2;
    int choice1b, diameterchoice1;
    char a, c;
    int lower1 = 5.0, upper1 =15.0; 
    int lower2 = 15.0, upper2 =30.0; 
    float area;
    float circumference=1, convertedradius;
    srand(time(NULL)); 
    radius = (rand() %(upper1-lower1+1))+ lower1; 
    diameter = (rand() %(upper2-lower2+1))+ lower2; 

 
    printf("Do you want to use radius or diameter for the calculation? \n");
    printf("Enter 1 for Radius \n");
    printf("Enter 2 for Diameter \n");
    printf("Enter -1 to Exit \n");
    scanf("%d", &choice);
    while (choice != -1){
        
    /*Entered 1 For Radius > */
    if (choice == 1)  { 
    printf("You selected Radius. Would you like the unit measured in CM or M? \n");
    printf("Enter 1 for CM \n");
    printf("Enter 2 for M \n");
    scanf("%d", &choice1a);
    
   
        switch (radiuschoice1) {
        case 1:
        /*Entered 1 For Radius > Entered 1 For CM > */ 
        printf("You selected CM. \n");
        printf("The Radius is %dcm \n", radius);
        printf("Would you like the Area or Circumference calculation? \n");
        printf("Enter 1 for Area \n");
        printf("Enter 2 for Circumference \n");
        scanf("%d", &choice2a);
        break;

        case 2:
        /*Entered 1 For Radius > Entered 2 For M > */
        printf("You selected M. \n");
        printf("The Radius is %dm \n", radius);
        printf("Would you like the Area or Circumference calculation? \n");
        printf("Enter 3 for Area \n");
        printf("Enter 4 for Circumference \n");
        scanf("%d", &choice2b);
        break;
        }

            switch (radiuschoice2) {
            case 1:
            /*Entered 1 For Radius > Entered 3 For CM > Entered 1 for Area*/
            area=3.14159*radius*radius;
            printf("You selected A for Area.\n");
            printf("The Area is %.2f.cm \n", area);
            break;

            case 2:
            /*Entered 1 For Radius > Entered 3 For CM > Entered 2 for Circumference*/
            circumference=(2*22*radius)/(7);
            printf("You selected C for Circumference.\n");
            printf("The Circumference is %.2f.cm \n", circumference);
            break;

            case 3:
            /*Entered 1 For Radius > Entered 4 For M > Entered 3 for Area*/
            area=3.14159*radius*radius;
            printf("You selected A for Area.\n");
            printf("The Area is %.2f.m \n", area);
            break;
    
            case 4:
            /*Entered 1 For Radius > Entered 4 For M > Entered 4 for Circumference.*/
            circumference=(2*22*radius)/(7);
            printf("You selected C for Circumference.\n");
            printf("The Circumference is %.2f.m \n", circumference);
            break;

        }
        }
    else (choice == 2); {
    printf("You selected Diameter. Would you like the unit measured in CM or M? \n");
    printf("Enter 1 for CM \n");
    printf("Enter 2 for M \n");
    scanf("%d", &choice1b);    

    switch(diameterchoice1){
    case 1:
    /*Entered 2 For Diameter > Entered 1 For CM */
    printf("You selected CM.  \n");
    printf("The Diameter is %dcm \n", diameter);
    convertedradius = diameter / 2;
    printf("Diameter to Radius is %.2f \n",convertedradius);
    area=3.14159*convertedradius*convertedradius;
    circumference=(2*22*convertedradius)/(7);
    printf("Area is %.2f.cm Circumference is %.2f.cm \n", area, circumference);
    break;
    
    case 2:
    /*Entered 2 For Diameter > Entered 2 For M*/
    printf("You selected M.  \n");
    printf("The Diameter is %dm \n", diameter);
    convertedradius = diameter / 2;
    printf("Diameter to Radius is %.2f \n",convertedradius);
    area = 3.14159*convertedradius*convertedradius;
    circumference=(2*22*convertedradius)/(7);
    printf("Area is %.2f.m Circumference is %.2f.m \n", area, circumference);
    break;
}
}
}
return 0; 
}

Aucun commentaire:

Enregistrer un commentaire