Good Afternoon,
I'm having problems with the termination of C++ program that deals with else if statements within a while loop.
Below is the code that I have so far.
while (choice != 'y')
{
//get the choice
cout<<"What is your choice? ('a' to 'd', or 'A' to 'D') =>"<<endl;
cin>>choice1;
//switch to deal with four cases
if ((choice1 == 'a') || (choice1 == 'A')) //changed by sirisha
{
cout<<"I see a rat "<<endl; //no break here
}
else if (choice1 == 'b' || choice == 'B')
{
cout<<"I have a cat "<<endl;
}
else if (choice1 == 'c' || choice == 'C')
{
cout<< "Cat hates rat "<<endl;
cout<<"Rate chases cat "<<endl;
}
else if (choice1 == 'd' || choice == 'D')
//complex statement can be added any where as you wish
{
cout<<"Give me a random seed => ";
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" <<endl;
}
else
cout<<" With 70 % of chance, rat catches cat with a hat" <<endl;
}
else
cout<<"The choice is not valid" <<endl;
}
//repeat or not
cout<<"Want to play again? (y/n) => ";
cin>>choice;
if (choice != 'y' && choice != 'Y')
{
// stop if the choice is not 'y' or not 'Y'
cout<<"Byebye now ........ "<<endl;
}
else
cout<<"Okay, one more time >>>>>>>>>>>>>>>> "<<endl;
//well done and exit
return 0;
}