Solution

/*Programmer: Dr. Bushra Anjum

Date: 2-14-2015

File: hw4.cpp Class: cs1570

Purpose: This file contains the main function for the program

that calculates the wavelength of light to set the dial on the

Crime-o-Light 400 to.

Concepts: Switch-Case, Nested Loops, If-Else

*/

#include <iostream>

using namespace std;

int main()

{

//Constants

const int WAVELENGTH_FOR_HAIR = 400;

const int GG0=200, GG1=43, GG2=17, GG3=23, GG4=77, GG5=55, GG6=35;

//Variables

int input=0;

int n, gooGrade;

float wavelength=0, powerTerm=1;

//The BIG while that ensures that the program continues until the

//user choses to exit/quit

while(input!=5)

{

cout<<"\n\tCrime-o-Light 400\n";

cout<<"\t-----------------\n";

cout<<"1. Hair\n";

cout<<"2. Saliva and hair\n";

cout<<"3. Chicken-noodle soup and saliva and hair\n";

cout<<"4. Green Goo\n";

cout<<"5. exit\n";

cout<<"Please select an option: ";

cin>>input;

//Switch statement dealing with first four cases

switch(input)

{

case 1:

wavelength=WAVELENGTH_FOR_HAIR;

cout<<"The wavelength of light emission is: "<<static_cast<int>(wavelength)<<" nm"<<endl;

break;

case 2:

//while loop to get a valid value for n between 1 and 7

cout<<"Please enter a value for n between [2,7]: ";

cin>>n;

while(n<=1 || n>7)

{

cout<<"Invalid entry ... please enter a value for n between [2,7]: ";

cin>>n;

}

//for loop for coding formula

for(int i =1;i<=n;i++)

{

wavelength+=(1.0/i);

}

wavelength*=100;

cout<<"The wavelength of light emission is: "<<static_cast<int>(wavelength)<<" nm"<<endl;

break;

case 3:

//while loop to get a valid value for n between 1 and 7

cout<<"Please enter a value for n > 0: ";

cin>>n;

while(n<1)

{

cout<<"Invalid entry ... please enter a value for n > 0: ";

cin>>n;

}

//nested for loop for coding formula

wavelength+=powerTerm;

for(int i =1;i<n;i++)

{

for(int j =1;j<=i;j++)

{

powerTerm*=(1.0/2);

}

wavelength+=powerTerm;

powerTerm=1;

}

wavelength*=100;

cout<<"The wavelength of light emission is: "<<static_cast<int>(wavelength)<<" nm"<<endl;

break;

case 4:

//while loop to get a valid value of goo grade between 0 and 6

cout<<"Please enter the goo grade value between [0,6]: ";

cin>>gooGrade;

while(gooGrade<0 || gooGrade>6)

{

cout<<"Invalid entry ... please enter the goo grade value between [0,6]: ";

cin>>gooGrade;

}

//using switch case statement to calculate the wavelength

switch(gooGrade)

{

case 6:

wavelength+=GG6;

case 5:

wavelength+=GG5;

case 4:

wavelength+=GG4;

case 3:

wavelength+=GG3;

case 2:

wavelength+=GG2;

case 1:

wavelength+=GG1;

case 0:

wavelength+=GG0;

}

cout<<"The wavelength of light emission is: "<<static_cast<int>(wavelength)<<" nm"<<endl;

break;

}

wavelength=0;

}

//Goodbye message

cout<<"\nSigning of now ...\n\n";

return 0;

}