Thread: enum program not working

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question enum program not working

    greetings;

    i get this error when i compile, how do i correct it? C:\Program Files\Microsoft Visual Studio\MyProjects\enum\enum.cpp(11) : error C2676: binary '++' : 'enum game_result' does not define this operator or a conversion to a type acceptable to the predefined operator


    here is my code;
    Code:
    #include <iostream>
    using namespace std;
    
    enum game_result {win, lose, tie, cancel};
    
    main()
    {
    	game_result result;
    	enum game_result omit = cancel;
    	
    	for (result = win; result <= cancel; result++) {
    		if (result == omit) 
    			cout << "The game was cancelled\n";
    		else {
    			cout << "The game was played ";
    			if (result == win)
    				cout << "and we won!";
    			if (result == lose)
    				cout << "and we lost.";
    			cout << "\n";
    		}
    	}
    	return 0;
    }
    Last edited by correlcj; 11-03-2002 at 03:47 PM.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    That is probably because (As seb an I were discussion before...) an enum is NOT an int, so you can't treat it like one.

    You could overload an operator for it, or use an int for your iteration and then cast it to your enum, or just come up with some better construct for what you are trying to do.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Enums don't work like that and your code makes no sense.
    Joe

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Hey Joe!

    Enums don't work like that and your code makes no sense.
    What do you mean by it not making sense? How do I fix it then?
    i've never gotten this error before and have little info on how to correct it. plz help?
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    >What do you mean by it not making sense?

    Sorry, on a re-read I think I've realised what you're trying to do. Try either nested if/elses or a switch/case. You can compare enum values but cannot add or subtract an intergral value from them, as no enum value may exist for the increment (and they don't even have to be in a sequential order).
    Joe

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Well from your algo it looks like you want it to just output:

    The game was played and we won!
    The game was played and we lost
    The game was played
    The game was cancelled

    It seems strange that you define an enum like that and then don't actually use it in the intended manner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM