/*--------------------------------------------------*/
/* Name: Student number:   */
/* Date: September 17,2014                          */
/* Program:TriangleCalculator                       */
/* This program takes 4 bands from the              */
/*user, through menus and calculates the            */
/*resistance value and tolerence. It also shows     */
/*the correct unit depending on how                 */
/*large the entered values are.                     */
/*--------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main()

{
     //Declare variables
     int option,terminate=0,counter=1,band1;
     int firstband,secondband,multiplier,tolerence;
     float fourthband,tolerencevalue,resistance,thirdbandunit;
     double thirdband;
     char ch,*unit1 = "Ohm",*unit2 = "Kilo-Ohm",*unit3 = "Mega-Ohm",*unit4 = "Giga-Ohm",*returnunit;


    //Display the menu options
    //This only needs to be dispalyed once, therefore no loop is neccessary
 start:
        printf("Please choose an option.\n");
        printf("1. Calculate resistance value.\n");
        printf("2. Help.\n");
        printf("3. Exit.\n");

          //Read selected option eneterd by the user
        scanf("%d", &option);
          switch(option)
        {//Main menu statment starts
          case 1:
                    while(counter<3) //While loop for displying menu in band1 and 2 as values are identical.

                {

                  //Display the option for all the bands.

                  mainstart:

                                printf("Please select the colours on the bands of your resistor, enter the bracketed letters for your choice\n");
                                 printf("(B)lack\n");
                                 printf("(b)rown\n");
                                 printf("(R)ed\n");
                                 printf("(O)range\n");
                                 printf("(Y)ellow\n");
                                 printf("(G)reen\n");
                                 printf("B(l)ue\n");
                                 printf("(V)iolet\n");
                                 printf("(g)ray\n");
                                 printf("(W)hite\n");


                                  do {//To avoid similair cases, use char inswtead of numerical options
                                            ch=getchar();
                                    } while(ch!='B' && ch!='b' && ch!='R' && ch!='O' && ch!='Y' && ch!='G' && ch!='l' && ch!='V' && ch!='g' && ch!='W');

                                    //Declaring all the case values that will be necessary later, this is done through a while assignment adn the getchar() function

                                 switch(ch)
                                 {
                                     case 'B':

                                    if (counter==1)
                                   {
                                       firstband=0;
                                   }
                                    else
                                    {
                                        secondband=0;
                                    }
                                    printf("You chose the color Black\n");
                                    counter++;

                                        break;

                                case 'b':

                                       if (counter==1)
                                   {
                                       firstband=10;
                                   }
                                    else
                                    {
                                        secondband=1;
                                    }
                                     printf("You chose the color Brown\n");
                                     counter++;

                                        break;

                                case 'R':

                                     if (counter==1)
                                   {
                                       firstband=20;
                                   }
                                    else
                                    {
                                        secondband=2;
                                    }
                                     printf("You chose the color Red\n");
                                     counter++;

                                        break;

                                case 'O':
                                     if (counter==1)
                                   {
                                       firstband=30;
                                   }
                                    else
                                    {
                                        secondband=3;
                                    }
                                     printf("You chose the color Orange\n");
                                     counter++;

                                        break;

                                case 'Y':
                                     if (counter==1)
                                   {
                                       firstband=40;
                                   }
                                    else
                                    {
                                        secondband=4;
                                    }
                                     printf("You chose the color Yellow\n");
                                     counter++;

                                     break;


                                case 'G':
                                     if (counter==1)
                                   {
                                       firstband=50;
                                   }
                                    else
                                    {
                                        secondband=5;
                                    }
                                     printf("You chose the color Black\n");
                                     counter++;

                                     break;

                                case 'l':
                                     if (counter==1)
                                   {
                                       firstband=60;
                                   }
                                    else
                                    {
                                        secondband=6;
                                    }
                                     printf("You chose the color Blue\n");
                                     counter++;

                                     break;

                                case 'V':
                                     if (counter==1)
                                   {
                                       firstband=70;
                                   }
                                    else
                                    {
                                        secondband=7;
                                    }
                                     printf("You chose the color Violet\n");
                                     counter++;

                                    break;


                                case 'g':
                                     if (counter==1)
                                   {
                                       firstband=80;
                                   }
                                    else
                                    {
                                        secondband=8;
                                    }
                                     printf("You chose the color Gray\n");
                                     counter++;

                                    break;

                                case 'W':
                                     if (counter==1)
                                   {
                                       firstband=90;
                                   }
                                    else
                                    {
                                        secondband=9;
                                    }
                                     printf("You chose the color White\n");
                                     counter++;

                                    break;

                                default:
                                    printf("You entered an illegal option.\n");
                                    goto mainstart;
                                    break;
                                 }//This ends the first two bands

                }//End of the while-loop

                printf("\n");//space to visually differentiate the next process

                start2: //restart menu if wrong input
                printf("Now to enter the multiplier band M.\n");

                printf("\n");
                                 printf("1.Black\n");
                                 printf("2.Brown\n");
                                 printf("3.Red\n");
                                 printf("4.Orange\n");
                                 printf("5.Yellow\n");
                                 printf("6.Green\n");
                                 printf("7.Blue\n");
                                 printf("8.Violet\n");
                                 printf("9.Gray\n");
                                 printf("10.White\n");
                                 printf("11.Gold\n");
                                 printf("12.Silver\n");
                                 scanf("%d", &multiplier);
          switch(multiplier)
          {
        case 1:
            thirdbandunit = pow(10,0);
            returnunit = unit1;
            break;
        case 2:
            thirdbandunit = pow(10,1);
            returnunit = unit1;
            break;
        case 3:
            thirdbandunit = pow(10,2);
            returnunit = unit1;
             break;
        case 4:
            thirdband = pow(10,3);
            thirdbandunit = thirdband/pow(10,3);
            returnunit = unit2;
             break;
        case 5:
            thirdband = pow(10,4);
            thirdbandunit = thirdband/pow(10,3);
            returnunit = unit2;
            break;
        case 6:
            thirdband = pow(10,5);
            thirdbandunit = thirdband/pow(10,3);
            returnunit = unit2;
            break;
        case 7:
            thirdband = pow(10,6);
            thirdbandunit = thirdband/pow(10,6);
            returnunit = unit3;
            break;
        case 8:
              thirdband = 7;
            thirdband = pow(10,7);
            thirdbandunit = thirdband/pow(10,6);
            returnunit = unit3;
            break;
        case 9:
            thirdband = pow(10,8);
            thirdbandunit = thirdband/pow(10,6);
            returnunit = unit3;
            break;
        case 10:
            thirdband = pow(10,9);
            thirdbandunit = thirdband/pow(10,9);
            returnunit = unit4;
            break;
        case 11:
            thirdbandunit = 0.1;
            returnunit = unit1;
            break;
        case 12:
            thirdbandunit = 0.01;
            returnunit = unit1;
            break;

          default:
                printf("Your selection is invalid. Try again.\n");
                goto start2;
                break;
          }
                    {
                            start3:
                            printf("Select the colour of the tolerence band\n");
                            printf("1.Gold\n");
                            printf("2.Silver\n");
                                 scanf("%d", &tolerence);

                        switch(tolerence)
                    {

                        case 1:
                                fourthband = 0.05;
                            break;
                        case 2:
                                fourthband = 0.10;
                            break;
                        default:
                printf("Your selection is invalid. Try again.\n");
                goto start3;
                break;
                            }

                    }





    {
        //Once all values are collected, we must compute the resistance values in this block

        resistance = (firstband+secondband)*thirdbandunit;
        tolerencevalue = fourthband*((firstband+secondband)*thirdbandunit);
    }

                printf("The resistance value is %.1f +|- %.1f %s\n",resistance,tolerencevalue,returnunit);


                break;


          case 2: printf("This program allows a user to select 4 colours for the resistance bands\n of a resisitor and outputs the resistance value in a clean format\n Note: For the first two inputs the user must enter the bracketed letter for their selection, a number is not a valid choice for the first two inputs!\n");
            goto start;
            break;

          case 3: terminate=1;
            break;

            default:
                printf("Your selection is invalid. Try again.\n");
                break;

            }//Main menu ends
                        {

                        if (terminate==1)
                            exit(0);
                        }


                        return 0; //Exit initializer


}//End of main function code
