0

Hello i am a student so i wanted to say sorry in case my writing is tiring, feel free to correct me .

i am having the following problem i am trying to assign an enum int value to another double variable to make one multiplication. so the variable costOfRoom should take the value D or T or S which belong to an enum. (D=200 ,T=150,S=110)

this must be done by the user .

But cant find any way , i tried to make the second variable a string type but its not working again. it will just take the chars normally as a string would do :(

Also tried cin >> type_Ofroom costofroom ; but i think that this is used in Java??

Searched the forum also haven't any similar answer :(

The program runs fine it doesn't have any compiling errors :)

Thanks for your time

/* build a software system which will allow a hotel receptionist,
to enter in bookings for guests who come to the desk.
The system should display the room options as:
Room        Price       Code
---------------------------------------------------------------
Deluxe Room £200         D
Twin Room       £150     T
Single      £110         S

The receptionist should be prompted to enter in the room type and the number of 
nights a guest wishes to stay for and then calculate the amount
they need to pay. 
   */

// solution 
#include <iostream>
using namespace std;
int main() {

    // decleration of variables 
    double number_OfDays = 0, Totalcost = 0, costofroom = 0;
    enum   type_Ofroom { D = 200, T = 150, S = 150 };
    cout << "enter the type of the room " << endl << endl;

    //input of room type
    cin >> costofroom; // **here is the problem**  i am trying to give the 
                       //    values of the enum varaiable 
                        // it should have D or T or S but i cant  make it
    cout << "enter the number of the days " << endl << endl;

    //input of days
    cin >> number_OfDays;

    // calculation 
    Totalcost = costofroom * number_OfDays;

    // result 
    cout << "the costumer has to pay " << Totalcost << " pounds" << endl << endl;
    return 0;
}
  • You can write your own cin operator, taken from here: http://stackoverflow.com/a/10371706/5313831 – buart Oct 07 '15 at 23:41

2 Answers2

1

You can read into a double, and then check against your enum values:

//input of room type
while (1)
{
    cin >> costofroom;
    if (costofroom == 0.0)
        costofroom = D;
    else if (costofroom == 1.0)
        costofroom = T;
    else if (costofroom == 2.0)
        costofroom = S;
    else
    {
        cout << "You didn't enter a valid option" << endl;
        continue;
    }
    break;
}

However, it would be better to read into an int, and then set your double afterward.

double costofroom;
int option;

...

//input of room type
while (1)
{
    cin >> option;
    if (option == 0)
        costofroom = D;
    else if (option == 1)
        costofroom = T;
    else if (option == 2)
        costofroom = S;
    else
    {
        cout << "You didn't enter a valid option" << endl;
        continue;
    }
    break;
}
owacoder
  • 4,815
  • 20
  • 47
0

The user can only input characters. cin will convert groupings of numbers to an int (e.g. 123) or or double (e.g. 123.5). It will also handle non-numeric groupings to std::strings (e.g. hello) or individual characters (e.g. c).

Once you have the user's input, you can convert them to your enum. You can use if statements, case statements or some type of table look up to do this.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21