Good Afternoon,

I'm having trouble with loops that deal with enumeration.
So far I have this code

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

using namespace std; 

int main( )
{
    //enumerate type definition
    enum weekDays {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,Sunday};  

    //variable declarations
    weekDays myDays;        //to deal with weekDays datatype
    string day;             //C++ string object, to get the day
    char anotherDay[50];    //C-string var, to get the day as a C-string

    char choice,            //for choice of C++ strings or C-string; 
        option;             //a flag to stop the loop

    //loop foevever to play the program until the user stops
    while (true)
    {
        //play with C++ strings or C-strings    
        cout<<"Want to play C++ strings or C-strings (y for C++ strings /n for C strings)? => "<<endl; 
        cin>>choice; 

        if (choice == 'y' || choice == 'Y')     //for C++ strings
        {
            //get a day via C++ string object
            cout<<"Enter a week day => "; 
            cin>>day; 

            //covert string week day to enumerated weekDays value
            if (day == "Monday")
            {
                myDays = Monday;                
                cout<< "The day of the week that you entered is: " << day << endl;
            }
            else if (day == "Tuesday")
            {           
                myDays = Tuesday;               
                cout<< "The day of the week that you entered is: " << day << endl;
            }
                //For you: finish the rest of conversion
            /**********  Write Your Code here *************/
            else if (day == "Wednesday")
            {
                myDays = Wednesday;
                cout<< "The day of the week that you entered is: " << day << endl;
            }
            else if (day == "Thursday")
            {           
                myDays = Thursday;
                cout<< "The day of the week that you entered is: " << day << endl;
            }
            else if (day == "Friday")
            {           
                myDays = Friday;
                cout<< "The day of the week that you entered is: " << day << endl;
            }
            else if (day == "Saturday")
            {           
                myDays = Saturday;
                cout<< "The day of the week that you entered is: " << day << endl;
            }
            else if (day == "Sunday")
            {           
                myDays = Sunday;
                cout<< "The day of the week that you entered is: " << day << endl;
            }
            else 
                cout<< "not a valid day. "<<endl; 


        }
        //this part is to play with C-strings   
        else                                
        {
            //get a day via C string object
            cout<<"Enter a week day => "; 
            cin>>anotherDay; 

            //covert string week day to enumerated weekDays value
            if (strcmp(anotherDay, "Monday") == 0)
                myDays = Monday;                
            else if (strcmp(anotherDay, "Tuesday") == 0)
                myDays = Tuesday;               
            //For you: finish the rest of conversion
            /********** Write Your Code here *************/
            else 
                cout<< "not a valid day. "<<endl; 
        }

        //now play with enumerated type value
        switch (myDays)
        {
            case Monday: 
                cout<<"Monday is not a study day. "<<endl;
                cout<<"coz I am still sleepy." <<endl; 
                break; 
            case Tuesday:
                cout<<"Oh, Geez, Tuesday IS a studyday."<<endl; 
                break; 
            //This is for you: Complete the rest of the cases, including the default case.
            /************  Write Your Code here ***********************************/

        }

        //This part is for you: Ask the user whether to continue the play or not
        /************  Write Your Code here *********************************/


    }



    //well done and exit
    return 0; 
}

OK, what problem are you having? The code your instructor provides is pretty straightforward (if a bit BFI, presumably on purpose) and should be easy to extend. Where is the issue, and what is it doing wrong?

Edited 4 Years Ago by Schol-R-LEA

the first loop doesn't stop it keeps running forever, and how to cout the day without the enum value on the second loop

here is the output that I'm getting

Want to play C++ strings or C-strings (y for C++ strings /n for C strings)? =>
y
Enter a week day => Monday
The day of the week that you entered is: 0
Monday is not a study day.
coz I am still sleepy.
Want to play C++ strings or C-strings (y for C++ strings /n for C strings)? =>

Where it says: The day of the week that you entered is 0,
it should be saying Monday

if (day == "Monday")
{
    day = "Monday";
    myDays = Monday;                
    cout<< "The day of the week that you entered is: " << day << endl;
}

Try this and see if it does anything.

I'm trying to go through the loops for a second try, but it just terminates

Here is the revised code

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

using namespace std; 

int main( )
{
    //enumerate type definition
    enum weekDays {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,Sunday};   //changed by sirisha

    //variable declarations
    weekDays myDays;        //to deal with weekDays datatype
    string day;             //C++ string object, to get the day
    char anotherDay[50];    //C-string var, to get the day as a C-string

    char choice = 'y';          //for choice of C++ strings or C-string; 
    char option = 'y';              //a flag to stop the loop

    //loop foevever to play the program until the user stops
    while (true)
    {
        //play with C++ strings or C-strings    
        cout<<"Want to play C++ strings or C-strings (y for C++ strings /n for C strings)? => "<<endl; 
        cin>>choice; 

        if (choice == 'y' || choice == 'Y')     //for C++ strings
        {
            //get a day via C++ string object
            cout<<"Enter a week day => "; 
            cin>>day; 

            //covert string week day to enumerated weekDays value
            if (day == "Monday")
            {
                myDays = Monday;                //changed by sirisha
                cout<< "The day of the week that you entered is: " << myDays << " " << "which stands for " << day <<endl;
            }
            else if (day == "Tuesday")
            {           
                myDays = Tuesday;               //changed by sirisha
                cout<< "The day of the week that you entered is: " << myDays << " " << "which stands for " << day <<endl;
            }
                //For you: finish the rest of conversion
            /**********  Write Your Code here *************/
            else if (day == "Wednesday")
            {
                myDays = Wednesday;
                cout<< "The day of the week that you entered is: " << myDays << " " << "which stands for " << day <<endl;
            }
            else if (day == "Thursday")
            {           
                myDays = Thursday;
                cout<< "The day of the week that you entered is: " << myDays << " " << "which stands for " << day <<endl;
            }
            else if (day == "Friday")
            {           
                myDays = Friday;
                cout<< "The day of the week that you entered is: " << myDays << " " << "which stands for " << day <<endl;
            }
            else if (day == "Saturday")
            {           
                myDays = Saturday;
                cout<< "The day of the week that you entered is: " << myDays << " " << "which stands for " << day <<endl;
            }
            else if (day == "Sunday")
            {           
                myDays = Sunday;
                cout<< "The day of the week that you entered is: " << myDays << " " << "which stands for " << day <<endl;
            }
            else 
                cout<< "not a valid day. "<<endl; 


        }

        //this part is to play with C-strings   

            //get a day via C string object
            cout<<"Enter a week day => "; 
            cin>>anotherDay; 

            //covert string week day to enumerated weekDays value
            if (strcmp(anotherDay, "Monday") == 0)
            {
                myDays = Monday;                //changed by sirisha 
                cout<<"The week day entered was: " << anotherDay <<" which enumerated value is: " << myDays << endl;
            }
            else if (strcmp(anotherDay, "Tuesday") == 0)
            {
                myDays = Tuesday;               //changed by sirisha
                cout<<"The week day entered was: " << anotherDay <<" which enumerated value is: " << myDays << endl;
            }
            //For you: finish the rest of conversion
            /********** Write Your Code here *************/
            else if (strcmp(anotherDay, "Wednesday") == 0)
            {
                myDays = Wednesday;
                cout<<"The week day entered was: " << anotherDay <<" which enumerated value is: " << myDays << endl;
            }
            else if (strcmp(anotherDay, "Thursday") == 0)
            {
                myDays = Thursday;
                cout<<"The week day entered was: " << anotherDay <<" which enumerated value is: " << myDays << endl;
            }
            else if (strcmp(anotherDay, "Friday") == 0)
            {
                myDays = Friday;
                cout<<"The week day entered was: " << anotherDay <<" which enumerated value is: " << myDays << endl;
            }
            else if (strcmp(anotherDay, "Saturday") == 0)
            {
                myDays = Saturday;
                cout<<"The week day entered was: " << anotherDay <<" which enumerated value is: " << myDays << endl;
            }
            else if (strcmp(anotherDay, "Sunday") == 0)
            {
                myDays = Sunday;
                cout<<"The week day entered was: " << anotherDay <<" which enumerated value is: " << myDays << endl;
            }
            else 
                cout<< "not a valid day. "<<endl; 


        //now play with enumerated type value
        switch (myDays)
        {
            case Monday: 
                cout<<"Monday is not a study day. "<<endl;
                cout<<"coz I am still sleepy." <<endl; 
                break; 
            case Tuesday:
                cout<<"Oh, Geez, Tuesday IS a studyday."<<endl; 
                break; 
            //This is for you: Complete the rest of the cases, including the default case.
            /************  Write Your Code here *********
            **************************/
            case Wednesday:
                cout<<"Wednesday is the day to start studying." <<endl;
                break;
            case Thursday:
                cout<<"Thursday is the day to review your assignments and make notes." <<endl;
                break;
            case Friday:
                cout<<"Friday is a awesome day." <<endl;
                break;
            case Saturday:
                cout<<"Saturday is the day to party." <<endl;
                break;
            case Sunday:
                cout<<"Sunday is the day to get prepare for tests." <<endl;
                break;
            default:
                cout<<"Invalid input" <<endl;
        }

        //This part is for you: Ask the user whether to continue the play or not
        /************  Write Your Code here *********************************/
        cout<<" Do want to continue playing? (y/n) => "; 
        cin>>option;                
        if (option != 'y' || option != 'Y')
        {
            cout<<"Good bye, Have a nice day"<<endl; 

        }
        else
        cout<<"Let's do it, again"<<endl; 


    }



    //well done and exit
    return 0; 
}

here is the output that I'm getting
Want to play C++ strings or C-strings (y for C++ strings /n for C strings)? =>
y
Enter a week day => Monday
The day of the week that you entered is: 0 which stands for Monday
Enter a week day => Thursday
The week day entered was: Thursday which enumerated value is: 3
Thursday is the day to review your assignments and make notes.
Do want to continue playing? (y/n) => y
Good bye, Have a nice day
Want to play C++ strings or C-strings (y for C++ strings /n for C strings)? =>

The problem here is that you don't have any condition to actually break the loop.
Try for example, as an else if command to exit the loop.

else if (anotherDay=="q"){
    break;
}

This code, if you put it just above the

            else 
            cout<< "not a valid day. "<<endl;

command will break the loop when you ask for an input in the anotherDay variable, and your answer is "q".
So whenever you press "q" it will break the loop.
In your case, the break; from the switch { case() } apply only inside the block of switch { case() }.

The wile(true) block it's an infinite loop, so you need an explicit command to break it.

Later edit:
I later noticed your extended version of the program. Use like this.

if (option != 'y' || option != 'Y')
        {
            cout<<"Good bye, Have a nice day"<<endl; 
            break; // exit(0) in other cases - this is where you should kill the loop.
        }

If you have further questions, just post them here.

Edited 4 Years Ago by Lucaci Andrew

This question has already been answered. Start a new discussion instead.