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; 
}

I think the only problem is a misplaced closing bracket. If you move the closing bracket at line 42 to line 56, then all should work. Right now, line 42 is where the while-loop ends, but it should end at line 56.

Hint: Using proper indentation will help you identify such little mistakes easily.

First thing is umm always try to name your variables properly. As it is, by reading your code, I see that your confused yourself between choice and choice1. You might want to have fairly different names for those variables and have a relook at your code. Hint: Have a good look at your if and else conditions. On other hand, right before the end of the loop your program prematurely terminates. This means that your program ends and condition is not tested for again. Hint: Have a look at your while condition again and deicede where to place your return statement. Also you might want to use a do while loop instead of just while. This is because from what i see, you would want your code to run atleast once before you test the condition.

Here is the whole code

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>

using namespace std; 

int main( )
{
    //variable declarations
    char choice1, choice='y';                   //choice1 for switch and choice for replay//changed by sirisha
    unsigned seed;                          //random seed
    int test, selection;                                //random number test




    //a loop to play forever until the user chooses to stop

    while  (true)
    {
    //get the choice
    cout<<"What is your choice? ('a' to 'd', or 'A' to 'D') =>"<<endl; 
    cin>>choice1;
        //switch to deal with four cases
        switch (choice1)                    //changed by sirisha
        {
            case 'a':   
            case 'A':
                cout<<"I see a rat "<<endl;
                break;
            case 'b':
            case 'B':   
                cout<<"I have a cat "<<endl; 
                break; 
            case 'c':
            case 'C':  
                cout<< "Cat hates rat "<<endl; 
                cout<<"Rate chases cat "<<endl; 
                break; 
            case 'd':
            case '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; 

                break; 
            default: 
                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; 
            break;                  
        }

        cout<<"Okay, one more time >>>>>>>>>>>>>>>> "<<endl; 
    }
    /*************************************************************************************
    The following two parts are for you to complete.
    *************************************************************************************/

    /**************************************************************************************
    Part A. Rewrite the above given switch statememt with an int type selction variable.
    **************************************************************************************/
    //Write your code here:

        while  (true)
    {
    //get the choice
    cout<<"What is your choice? ('1' to '4') =>"<<endl; 
    cin>>selection;
        //switch to deal with four cases
        switch (selection)                  //changed by sirisha
        {

            case 1:
                cout<<"I see a rat "<<endl;
                break;

            case 2: 
                cout<<"I have a cat "<<endl; 
                break; 

            case 3:  
                cout<< "Cat hates rat "<<endl; 
                cout<<"Rate chases cat "<<endl; 
                break; 

            case 4: 
                //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; 

                break; 
            default: 
                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; 
            break;                  
        }

        cout<<"Okay, one more time >>>>>>>>>>>>>>>> "<<endl; 
    }

    /**************************************************************************************
    Part B. Rewrite the above given switch statement using if-else statements
    **************************************************************************************/
    //Write your code here:

    while (choice != 'y')
    {
    //get the choice
    cout<<"What is your choice? ('a' to 'd', or 'A' to 'D') =>"<<endl; 
    cin>>choice1;

        if ((choice1 == 'a') || (choice1 == 'A'))           
        {
            cout<<"I see a rat "<<endl; 

        }
        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; 
                break;
        }       
        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; 
}

If you have trouble with your while loop, try with the for one: Here's a quick example.

#include <iostream>
#include <string>
using namespace std;

int main(){
    for (string a;a!="y";){ //the for exits only when "y" is typed.
        cout<<"> ";
        cin>>a;
        if (a=="1" or a=="1."){ // if you type "1" or "1." it will print "1".
            cout<<"1\n";
        }
        else if (a=="2" or a=="2."){
            cout<<"2\n";
        }
        else if (a=="3" or a=="3."){
            cout<<"3\n";
        }
        //else if (whatever you want to type in);
        else{
            if (a!="y") cout<<"invalid input\n";
            else cout<<"exiting\n";
        }
    }
    return (0);
}

Later edit:
I look over your code and there were some minor issues:

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main( )
{
    //variable declarations
    char choice1, choice='y';                   //choice1 for switch and choice for replay//changed by sirisha
    unsigned seed;                          //random seed
    int test, selection;                                //random number test




    //a loop to play forever until the user chooses to stop

    while  (true)
    {
    //get the choice
    cout<<"What is your choice? ('a' to 'd', or 'A' to 'D') =>"<<endl;
    cin>>choice1;
        //switch to deal with four cases
        switch (choice1)                    //changed by sirisha
        {
            case 'a':
            case 'A':
                cout<<"I see a rat "<<endl;
                break;
            case 'b':
            case 'B':
                cout<<"I have a cat "<<endl;
                break;
            case 'c':
            case 'C':
                cout<< "Cat hates rat "<<endl;
                cout<<"Rate chases cat "<<endl;
                break;
            case 'd':
            case '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;

                break;
            default:
                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;
            break;
        }

        cout<<"Okay, one more time >>>>>>>>>>>>>>>> "<<endl;
    }
    /*************************************************************************************
    The following two parts are for you to complete.
    *************************************************************************************/

    /**************************************************************************************
    Part A. Rewrite the above given switch statememt with an int type selction variable.
    **************************************************************************************/
    //Write your code here:

        while  (true)
    {
    //get the choice
    cout<<"What is your choice? ('1' to '4') =>"<<endl;
    cin>>selection;
        //switch to deal with four cases
        switch (selection)                  //changed by sirisha
        {

            case 1:
                cout<<"I see a rat "<<endl;
                break;

            case 2:
                cout<<"I have a cat "<<endl;
                break;

            case 3:
                cout<< "Cat hates rat "<<endl;
                cout<<"Rate chases cat "<<endl;
                break;

            case 4:
                //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;

                break;
            default:
                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;
            break;
        }

        cout<<"Okay, one more time >>>>>>>>>>>>>>>> "<<endl;
    }

    /**************************************************************************************
    Part B. Rewrite the above given switch statement using if-else statements
    **************************************************************************************/
    //Write your code here:

    while (choice != 'y')
    {
    //get the choice
    cout<<"What is your choice? ('a' to 'd', or 'A' to 'D') =>"<<endl;
    cin>>choice1;

        if ((choice1 == 'a') || (choice1 == 'A'))
        {
            cout<<"I see a rat "<<endl;

        }
        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;
                break;
        }
        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;
            break; //YOU forgot this break; in order to exit your loop.

        }

        else
        cout<<"Okay, one more time >>>>>>>>>>>>>>>> "<<endl;
    } //here you should close the while.
    //well done and exit
    return 0;
}

Here's the moddified code: you missplaced your last while loop bracet, and you didn't put a break when the 'y' was typed in.
By this program, you will be needed to parsh through the first 2 loops to get to the third;)
If you have further questions, just post them here.

Edited 4 Years Ago by Lucaci Andrew

This article has been dead for over six months. Start a new discussion instead.