tell me if i'm wrong but something strange happend

This is a discussion on tell me if i'm wrong but something strange happend within the C++ Programming forums, part of the General Programming Boards category; Ok i made a class named alarm the constructer takes threee values as follows: Code: Alarm(int amount, TYPE type, ROOM ...

  1. #1
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148

    tell me if i'm wrong but something strange happend

    Ok i made a class named alarm

    the constructer takes threee values as follows:

    Code:
    Alarm(int amount, TYPE type, ROOM room);
    and before this i have:
    Code:
    enum TYPE {FIRE, BURGLAR, WINDOW};
    enum ROOM {KITCHEN, LIVING, BED1, BED2, BED3, COMPUTER, BATH1, BATH2};
    now as i understand it each one of these enum things has a number starting at 0.

    So i made a function which would tell me where the alarm is and what type it is:

    Code:
    void make(int amount, int type, int room)
    {
     cout<<amount<<" ";
      switch(type)
      {
       case 0:
       cout<<"fire";
       break;
       case 1:
       cout<<"burglar";
       break;
       case 2:
       cout<<"window";
       break;
      }
        
      cout<<" alarms where installed in the ";
      switch(room)
      {
       case 0:
       cout<<"kitchen.";
       break;
       case 1:
       cout<<"living room.";
       break;
       case 2-4:
       cout<<"bed room.";
       break;
       case 5:
       cout<<"computer room.";
       break;
       case 6-7:
       cout<<"bathroom.";
       break;
       
      }
      cout<<"\n";
    }
    now what is going wrong is that no matter what i pass to the constructor it allways gives me burglar alarm but the rest works fine so i thought hey thats weird and before we call the function make() i put

    Code:
    cout<<comp.getType();  
    //yes i have made an instance of alarm named comp
    and its always one even if i make a constructer like so
    Code:
    Alarm kit(2, FIRE, KITCHEN);
    please tell me its a stupid mistake in the program I've gone nuts!!

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    now as i understand it each one of these enum things has a number starting at 0.

    yes, unless you assign another value to the first element:
    Code:
    enum TYPE {FIRE=3, BURGLAR, WINDOW};
    Now WINDOW = 5


    Code:
    case 0:
       cout<<"fire";
       break;
    You should use the ENUM types:
    Code:
    case FIRE:
       cout<<"fire";
       break;
    Code:
    case 6-7:
       cout<<"bathroom.";
    Maybe in another language but in C/C++ this is not possible. Try:
    Code:
    case 6:
    case 7:
       cout<<"bathroom.";
    And again: don't use numbers, use the enum types...

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I have a couple of comments:

    You don't specify numeric values in your enumerators, so you are leaving it to the compiler. I normally set enumerators explicitly, like so:

    enum TYPE {FIRE = 0, BURGLAR = 1, WINDOW = 2};

    However, this is mostly a matter of style (baring a few special cases).

    But because you have not specified values, you can't assume the values in your make function. You should really set your case statements are follows


    Code:
    void make(int amount, TYPE type, ROOM room)
    {
     cout<<amount<<" ";
      switch(type)
      {
       case FIRE:
       cout<<"fire";
       break;
       case BURGLAR:
       cout<<"burglar";
       break;
       case WINDOW:
       cout<<"window";
       break;
      }
    
      cout<<" alarms where installed in the ";
      switch(room)
      {
       case KITCHEN:
       cout<<"kitchen.";
       break;
       case LIVING:
       cout<<"living room.";
       break;
       case BED1:
       case BED2:
       case BED3:
       cout<<"bed room.";
       break;
       case COMPUTER:
       cout<<"computer room.";
       break;
       case BATH1:
       case BATH2:
       cout<<"bathroom.";
       break;
      }
    }
    However, without seeing the code for your constructor, I can't say for sure whether this was causing your problem.

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Try change
    Code:
    void make(int amount, int type, int room)
    to
    Code:
    void make(int amount, TYPE type, int room)
    And also why are you using
    Code:
    ...
    case 0:
       cout<<"fire";
       break;
       case 1:
       cout<<"burglar";
       break;
       case 2:
       cout<<"window";
       break;
    ...
    insteed of
    Code:
    case FIRE:
       cout<<"fire";
       break;
       case BURGLAR:
       cout<<"burglar";
       break;
       case WINDOW:
       cout<<"window";
       break;
    I find the code easier to read, donīt you ???

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Damn. Monster beat to that one. But he has a good point:

    >case 6-7:
    > cout<<"bathroom.";
    >Maybe in another language but in C/C++ this is not possible. Try:

    That could be a problem, because you'll have a case statement checking for -1, not 6 to 7.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  6. #6
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    thanks i'll be using the

    Code:
    case FIRE:
    break;
    now but just to tell you guys

    Code:
    case 6-8:
    break;
    this does work try yourself if you don't believe me. But from now on it think it be better if i did
    Code:
    case 6:
    case 7:
       cout<<"bathroom.";
    Thanks for all the help guys
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  7. #7
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    When i compile it with the switch statments like this

    Code:
    case FIRE:
    break;
    it still doesn't work it compiles but when it shows up it says

    4 burglar alarms installed in the computer room
    this is correct and then it says
    2 burglar alarms installed in the kitchen
    this is wrong its supposed o be fire alarms but why???

    well here is my whole source code:

    main:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include "alarm.h"
    void make(int amount, TYPE type, ROOM room);
    
    int main(int argc, char *argv[])
    {
      Alarm comp(4, BURGLAR, COMPUTER);
      make(comp.getAmount(), comp.getType(), comp.getRoom());
      Alarm kit(2, FIRE, KITCHEN);
      make(kit.getAmount(), kit.getType(), kit.getRoom());
      system("PAUSE");	
      return 0;
    }
    
    void make(int amount, TYPE type, ROOM room)
    {
     cout<<amount<<" ";
      switch(type)
      {
       case FIRE:
       cout<<"fire";
       break;
       case BURGLAR:
       cout<<"burglar";
       break;
       case WINDOW:
       cout<<"window";
       break;
      }
        
      cout<<" alarms where installed in the ";
      switch(room)
      {
       case KITCHEN:
       cout<<"kitchen.";
       break;
       case LIVING:
       cout<<"living room.";
       break;
       case BED1:
       case BED2:
       case BED3:
       cout<<"bed room.";
       break;
       case COMPUTER:
       cout<<"computer room.";
       break;
       case BATH1:
       case BATH2:
       cout<<"bathroom.";
       break;
       
      }
      cout<<"\n";
    }
    alarm.h:
    Code:
    #ifndef ALARM_H
    #define ALARM_H
    #include <iostream.h>
    enum TYPE {FIRE, BURGLAR, WINDOW};
    enum ROOM {KITCHEN, LIVING, BED1, BED2, BED3, COMPUTER, BATH1, BATH2};
    class Alarm
    {
     public:
     
     Alarm();
     Alarm(int amount, TYPE type, ROOM room);
     ~Alarm();
     ROOM getRoom() {return theRoom;}
     int getAmount() {return theAm;}
     TYPE getType() {return itsType;}
     private:
     
     int theAm;
     TYPE itsType;
     ROOM theRoom;
     
    };
    Alarm::Alarm()
    {
    }
    Alarm::Alarm(int amount, TYPE type, ROOM room)
    {
     theAm = amount;
     itsType = BURGLAR;
     theRoom = room;
    }
    
    Alarm::~Alarm()
    {
    }
    #endif
    burglar
    Last edited by devour89; 12-05-2002 at 11:31 PM.
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by devour89
    this does work.
    Nope. It'll compile, but it won't work.

    The reason why is it will evaluate the expression

    so:

    case 6-8:

    is actaully just

    case -2:

    because 6 - 8 = -2

    get it?

  9. #9
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    aha stupid me.... what about the post above yours... can you figure out the problem there?
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Because switch ony works with integers -- you're trying to use a string.

  11. #11
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    yeah i fixed that problem now it compiles fine.... everything runs and then i get:


    4 burglar alarms installed in the computer room
    //this is correct and then it says
    2 burglar alarms installed in the kitchen
    //this is wrong its supposed to be fire alarms but why???

    it changes the rooms but it doesn't change the type of alarm!

    updated source file is above
    Last edited by devour89; 12-05-2002 at 11:43 PM.
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  12. #12
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Originally posted by devour89
    //this is wrong its supposed to be fire alarms but why???

    it changes the rooms but it doesn't change the type of alarm!
    Your constructor for alarm is setting itsType = BURGLAR;
    for any type
    just change the line to
    itsType = type;

  13. #13
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    OMG, i cant believe i didn't see that before

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 11:21 AM
  2. Strange problem with bmp
    By Victor in forum Linux Programming
    Replies: 2
    Last Post: 04-04-2005, 03:48 PM
  3. help with strange errors
    By rockdj in forum C++ Programming
    Replies: 4
    Last Post: 07-27-2004, 12:42 PM
  4. strange declaration error
    By ammar in forum C Programming
    Replies: 7
    Last Post: 12-31-2002, 04:09 AM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21