// ***************************************************************** // Author: Zhixiang Chen // Class: CSCI/CMPE 1170, Spring 2008 // Lab 7: Switch statement vs. if-else statement // Date: Januray 12, 2009 // Comment: The code here is meant to be revised. // //----------------------------------------------------------------- // // This lab exercise is to play with switch statements and to understand // their equivalence to if-else statements // // Compile and run your program. When everything is fine, // print your .cpp file and turn it to me or the TA. // ***************************************************************** #include < iostream > #include #include < string > #include < cstring > #include < cmath> #include #include using namespace std; int main( ) { //variable declarations char choice; //choice for switch unsigned seed; //random seed int test; //random number test //get the choice cout<<"What is your choice? ('a' to 'd', or 'A' to 'D') =>"; cin>>choice; //a loop to play forever until the user chooses to stop //Warning: The loop has some logical bug. You shall find out and fix it. while (true) { //switch to deal with four cases switch (choice) { case 'a': case 'A': cout<<"I see a rat "< "; cin>>seed; srand(seed); //set the random seed test = rand(); if (test%100 >= 70) cout<<" With 30% of chance, cat hits rat with a bat" < "; cin>>choice; //reuse the choice var if (choice != 'y' && choice != 'Y') { cout<<"Byebye now ........ "<>>>>>>>>>>>>>>> "<