Enumeration probem

Hello again dear friends! Now i come to you with another problem... I was thinking of building a 6-operations calculator, but first of all i have to solve alowing the user to choose his opration and i made this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{cout<<"Hello friend! This program is to calculate some mathematycal operations for you."<<endl;
 cout<<"Please enter the operation you want to execute as add(for addition), sub(for substitution), mul(for multiplication), div(for division), pow(for power), sqrt(for  square)"<<endl;
 enum answer_type {add, sub, mul, div, pow, sqrt} answer;
 cin>>answer;
 Switch(answer)
 {case add: cout<<"addition";break;
  case sub: cout<<"substitution";break;
  case mul: cout<<"multiplication";break;
  case div: cout<<"division";break;
  case pow: cout<<"power";break;
  case sqrt: cout<<"square";break;
  default: cout<<"Incorrect answer";break;
system("PAUSE");
return 0;}
}

Ignore the fact that it isn't a calculator yet. i will do this after solving this part. Trying to compile it gain me some errors :
8 C:\... no match for 'operator>>' in 'std::cin >> answer'
9 C:\... `Switch' undeclared (first use this function)
10 C:\... expected `;' before '{' token
It would be great if you could help me with these problems.. thank you in advance!
There is no such thing as "Switch" in the C++ language.
Instead there is "switch".

1
2
Switch // no
switch // yes 


Treat enum's like they are int's.

1
2
3
4
5
6
7
8
9
10
int answer;

cin >> answer; // 0 for add, 1 for sub, 2 for mul, and so on...

switch (answer)
{
    case add: // if answer is 0

    // ...
}


Here is a more complicated answer:
http://stackoverflow.com/questions/5633784/input-from-stream-to-enum-type
Thank You for the help
Use an enumeration; it is a good idea.

Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

int main()
{
    enum answer_type { add = '+', sub = '-' , mul = '*' , div = '/',
                        pow = '^', sqrt = 'r', invalid = 0 } ;

    std::cout << "? (+,-,*,/,^,r): " ;
    char input ;
    std::cin >> input ;

    answer_type answer ;

    switch(input)
    {
        case add:
           std::cout << "addition\n" ;
           answer = answer_type(input) ;
           break ;

       // case sub : etc.

       default:
           std::cerr << input << "is an invalid operation\n" ;
           answer = invalid ;
    }

    // ...
}
JLBorges, if you can and want please explain what you've written there because i am very begginer in programing and i now few things not such as std:... thank you in advance
fluture, JLBorges hasn't written anything too complicated for a beginner like you to understand. But if you insist, here's a slightly "easier" version of his code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

int main()
{
    enum answer_type { add = '+', sub = '-' , mul = '*' ,
                       div = '/', pow = '^', sqrt = 'r', invalid = 0 };

    cout << "? (+,-,*,/,^,r): ";
    char input;
    cin >> input;

    answer_type answer;

    switch(input)
    {
        case add:
           cout << "addition\n";
           answer = answer_type(input);
           break;

       // case sub : etc.

       default:
           cerr << input << "is an invalid operation\n";
           answer = invalid;
    }

    // ...

    return 0;
}

Make an effort to understand it.
It's ok now i have understand most of it but i still not understand what does the cerr function means.. i am sorry if being a bother :)
std::cerr is just another output stream, like std::cout. It writes messages to standard-error, rather than standard-out.
Last edited on
Thank you for this information
1. An enumeration is a distinct type. With enum colour_t { /* ... */ }; colour_t is a distinct integral type - the type of colour_t is different from char, int etc.

2. The values that can be used are restricted to one of a set of named constants.
With enum colour_t { RED, GREEN, BLUE, BLACK };, a variable of type colour_t can take only one of the values RED, GREEN, BLUE, BLACK

3. Each of those constant values is an integral value.
By default, the values are RED = 0, GREEN = RED+1, BLUE = GREEN+1, BLACK = GREEN+1

4. We can change the default by explicitly specifying an integral value for an enumeration.
enum colour_t { RED = 1, GREEN = 2, BLUE = 4, BLACK = 0 };

5. Each of the enumerations can be implicitly converted to an integral type.
The value of GREEN in the above example converted to an integer is the integer 2.

6. However, any integral value can't be converted to a colour_t.
If we have an integer 4, it is the value of BLUE and we can convert it to a colour_t via an explicit cast;
1
2
colour_t clr = RED ; 
clr = colour_t(4) ; // clr will get a value of BLUE 


7. Since an enumeration is a distinct type, std::cin does not know how to read it.
It knows all about reading char, int etc., but nothing about reading a colour_t

8. However, we can read an int using std::cin, and if the integer has a value of 1, 2, 4 or 0, we can cast it safely to a colour_t

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

int main()
{
    // answer_type is a distinct type
    // the range of values it can have is add, sub, ..., invalid
    // add can be converted to the integral value int('+') etc.
    enum answer_type { add = '+', sub = '-' , mul = '*' , div = '/',
                        pow = '^', sqrt = 'r', invalid = 0 } ;

    std::cout << "? (+,-,*,/,^,r): " ;
    char input ; // input is a char
    std::cin >> input ; // fine; std::cin knows how to read a char

    // answer is not a char, it is a variable pof type answer_type
    answer_type answer ;

    switch(input)
    {
        case add: // add is implicitly converted to '+'

           // the user has entered '+' as the input
           std::cout << "addition\n" ;
           // therefore, the answer is add or answer_type('+')
           answer = answer_type(input) ;
           break ;

       // case sub : etc.

       default:
           // the chyar that the user has entered is not the value of
           // one of the enumerations add, sub etc.
           std::cerr << input << "is an invalid operation\n" ;
           answer = invalid ;
    }

    // ...
}


You might want to peruse a tutorial or two; for instance this one:
http://www.cprogramming.com/tutorial/enum.html
Last edited on
That helped a lot and i really thank you but i want to know one thing: Why do we use this code:answer = answer_type(input) ;? is it necessary if we already entered the output?
> is it necessary if we already entered the output?

It is not really necessary in the sense that we can write the program without using the enum at all. The idea here is that once we have validated the user input, for the rest of the program we could like to use the enum.

Using an enumerated type has several advantages:

1. The code becomes self-documenting:
1
2
void foo( colour_t clr ) ; // we know that clr is a colour, not just any integer
// it is one of the colours RED, GREEN, ... 


2. We get help from the compiler in catching semantic errors:
1
2
enum shape_t { TRIANGLE, ... } ;
foo( TRIANGLE ) ; // *** error: TRIANGLE is a shape, not a colour 


1
2
3
4
5
6
7
8
9
10
11
12
13
void foo( colour_t clr )
{
     clr *= 23 ; // error: it doesn't make sense to multiply-assign to a colour

     switch(clr)
     {
          case RED: /* ... */ ; break ;
          case BLUE: /* .... */ ; break ;
          case BLACK: /* ..... */  
     } // a helpful compiler can warn us that case GREEN: is missing

     // ... 
}


3. We get help from the debugger; a good debugger will show the value of clr as RED, GREEN etc..

4. We can discriminate between types
1
2
3
4
5
// print out the mnemonic for a colour
std::ostream& operator<< ( std::ostream& stm, colour_t clr ) ;

// print out the name of a shape
std::ostream& operator<< ( std::ostream& stm, shape_t shape ) ;
understood
Topic archived. No new replies allowed.