3

I wrote a certain method on how to access my fields in a class, but my teacher told me I should use an enum.

How can I re-write this code to use an enum and not use gotos?

void SetType() {
    cout << "Book SetType" << endl;
    Choice: cout << "Please Select from the list: \n "
            << "1- Technical literature \n "
            << "2- Fiction literature \n "
            << "3- Textbook" << endl;
    int i;
    cin >> i;

    switch (i) {
    case 1:
        Type = "Technical literature";
        break;
    case 2:
        Type = "Fiction literature";
        break;
    case 3:
        Type = "Textbook";
        break;
    default:
        cout << "Erorr you entered a wrong choice" << endl;
        goto Choice;
    }
}
4
  • Think about what an enum's real type is, and what it would replace in the code you showed. Commented Nov 20, 2012 at 18:31
  • 1
    You also want to check if you successfully read anything: if (std::cin >> i) { ... } Commented Nov 20, 2012 at 18:32
  • Your teacher meant you should enum values instead of 'magic' numbers. And you can avoid goto with loop, of cause Commented Nov 20, 2012 at 18:32
  • Your teacher addressed two separate points: 1. Use an enum for the switch. 2. Don't use goto, put the whole stuff in an appropriate loop instead. @kenny's answer addresses both. Commented Nov 20, 2012 at 18:55

2 Answers 2

4

just use loops instead of gotos of it is going to be a spaghetti code. Enums are fine to does not care about the numbers for the defines, because they are incremented automatically if you add a new one.

#include <iostream>
#include <string>
void SetType();

using namespace std;
string Type;
int main()
{
    SetType();

    cout << "so you choose " << Type << endl;
    return 0;
}
enum select
{
    Technical_literature = 1,
    Fiction_literature,
    Textbook
};

void SetType() {
    cout<<"Book SetType"<<endl;
    while(1)
    {
        cout<<"Please Select from the list: \n 1- Technical literature \n 2- Fiction literature \n 3- Textbook"<<endl;
        int i;
        cin >> i;

        switch(i) {
        case Technical_literature:
            Type="Technical literature";
            return;
        case Fiction_literature:
            Type="Fiction literature";
            return;
        case Textbook:
            Type="Textbook";
            return;
        default:
            cout << "Erorr you entered a wrong choice" << endl;

        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This explains both of the OP's implicit 2 questions.
1

Your teacher meant that instead of hardcoding constants all over the place you need to declare your i as enum.

enum some_type {
    type_techlit=1, type_fiction, type_textbook
};

some_type i;

And then read up on enums.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.