Like Tree1Likes
  • 1 Post By Elysia

i use pass by reference but my cout give me weird number

This is a discussion on i use pass by reference but my cout give me weird number within the C++ Programming forums, part of the General Programming Boards category; My school gave me a smart house assignment .When i am pass reference to the function status there it give ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    i use pass by reference but my cout give me weird number

    My school gave me a smart house assignment .When i am pass reference to the function status there it give me weird number


    Code:
    #include <iostream>
    using namespace std;
    
    void enter();
    void panic();
    void menu();
    void status(int[],int[]);
    void panichouse();
    
    
    void main()
    {
    
        char on;
        cout<<"The system is off press s to turn on  :";    
        do
        {    
        cin>>on;
        if(on=='s')
        {
            enter();
        }
        else
        {
            cout<<"Please enter again :";
            
        }
        }while(on!='s');
        
        
    }
    
    void enter()
    {
        ///////Front is 0 ,Back is 1
        char choice,en;
        int correct=0;
        do
        {
        cout<<"Entering(E)or Exiting the house(X) :";
        cin>>choice;
            
        }while(choice!='E'&&choice!='e'&&choice!='X'&&choice!='x');
        switch(choice)
        {
        case 'E':
        case 'e':correct=0;break;
        case 'x':
        case 'X':correct=1;break;
        }
        
        cout<<"Attempt to enter \n";
        cout<<"Enter through Front Door(F) or Back Door(B) :";
        cin>>en;
        ////frontdoor
        if(en=='F'||en=='f'&&correct==0)
        {
            menu();
        }
        else
        {
            if(en=='B'||en=='b'&&correct==0)
            panic();
        }
        ///backdoor
        if(en=='B'||en=='b'&&correct==1)
        {
            cout<<"You are already outside the house\n";
        }
        else
        {
            if(en=='F'||en=='f'&&correct==1)
            panic();
        }
    }
    void menu()
    {
        int choice,light,door;
        cout<<"        ***********WELCOME HOME !***********\n";
        cout<<"    ***********YOU HAVE ENTERED THE HOUSE ***********\n";
        cout<<"        \n 1)CHECK STATUS \n 2)ENTER ROOM \n 3)PANIC BUTTON\n 4)EXITING THE HOUSE  :";
        do
        {
        cin >>choice;
        switch(choice)
        {
        case 1:status(&light,&door);break;
        case 3:panichouse();break;
        case 4:enter();break;
        default:cout<<"Enter again \n";break;
        }
        }while(choice!=1&&choice!=2&&choice!=3&&choice!=4);
    }
    void panic()
    {
        int pass;
        char ina;
        
    
        cout<<"\n\n\n";
        cout<<"            FRONT DOOR LOCKED,BACK DOOR LOCKED    \n";
        cout<<"            HALL/LIVING DOOR LOCKED ,LIGHT ON \n";
        cout<<"            BEDROOM DOOR LOCKED, LIGHT ON \n";
        cout<<"            STUDY ROOM DOOR LOCKED ,LIGHT ON \n";
        cout<<"            KITCHEN DOOR LOCKED ,LIGHT ON    \n";
        cout<<"            BATHROOM DOOR LOCKED, LIGHT ON \n";
        cout<<"            ENTER PASSWORD \n \a";
        do
        {
        cin>>pass;
        if(pass==1234)
        {
            do
            {
            cout<<"        PRESS P TO INACTIVATE PANIC BUTTON\n\a";
            cin>>ina;
            if(ina=='p'||ina=='P')
            {
                main();
            }
            else
            {
                cout<<"Enter again: \n";
            }
            }while(ina!='p'&&ina!='P');
        }
        else
            {
                cout<<"Enter again :\n";
            }
        
        }while(pass!=1234);
    }
    
    void panichouse()
    {
        int light[5]={1,1,1,1,1};
        int door[2]={1,1};
        int pass;
        char ina;
        cout<<"\n\n\n";
        cout<<"            FRONT DOOR LOCKED,BACK DOOR LOCKED    \n";
        cout<<"            HALL/LIVING LIGHT ON \n";
        cout<<"            BEDROOM LIGHT ON \n";
        cout<<"            STUDY ROOM LIGHT ON \n";
        cout<<"            KITCHEN LIGHT ON    \n";
        cout<<"            BATHROOM  LIGHT ON \n";
        cout<<"            ENTER PASSWORD \n \a";
        do
        {
        cin>>pass;
        if(pass==1234)
        {
            do
            {
            cout<<"        PRESS P TO INACTIVATE PANIC BUTTON\n\a";
            cin>>ina;
            if(ina=='p'||ina=='P')
            {
                menu();
            }
            else
            {
                cout<<"Enter again: \n";
            }
            }while(ina!='p'&&ina!='P');
        }
        else
            {
                cout<<"Enter again :\n";
            }
        }while(pass!=1234);
        status(light,door);
        
    }
    
    void status(int light[],int door[])
    {
        
        static int con[5]={1,1,1,1,1};
        int turn,tur,type;
        int exit;
    
        cout<<"ON or OPEN =1      OFF or LOCK =0 \n";
        cout<<"Status  press E to menu \n";
        cout<<"Bedroom\n light:"<<light[0]<<"\t"<<"Aircon:"<<con[0]<<endl;
        cout<<"StudyRoom\n light:"<<light[1]<<"\t"<<"Aircon:"<<con[1]<<endl;
        cout<<"Kitchen\n light:"<<light[2]<<"\t"<<"Aircon:"<<con[2]<<endl;
        cout<<"Bathroom\n light:"<<light[3]<<"\t"<<"Aircon:"<<con[3]<<endl;
        cout<<"Frontdoor "<<"\t"<<"door"<<door[0]<<endl;
        cout<<"Backdoor "<<"/t"<<"door"<<door[1]<<endl;
        cout<<"To exit press 1 to Enter room press 2";
        
        cin>>exit;
        switch(exit)
        {
        case 1:menu();break;
        case 2:goto room;break;
        }
    room:
        do
        {
        cout<<"WHICH ROOM YOU WOULD LIKE TO ENTER  :\n";
        cout<<"a)Bedroom press 1\nb)StudyRoom press 2\nc)Kitchen press 3\nd)Bathroom press 4\ne)Frontdoor press 5\nf)backdoor press 6\n";
        cin>>type;
        switch(type)
        {
            case 1:cout<<"To turn on light press 1 To turn off light press 2\n";
            cin>>turn;
            if(turn==1&&light[0]==0)
            {
                cout<<"Light is on \n";
                light[0]=1;
                
                }    
            else
            {
                if(turn==1&&light[0]==1)
                cout<<"Light is already on \n";
            }
             if(turn==2&&light[0]==1)
            {
                cout<<"Light if off \n";
                light[0]=0;
                
            }
            else
            {
                if(turn==2&&light[0]==0)
                cout<<"Light is already off \n";
                }
             /////air condition
             cout<<"To Turn on aircondition press 1 To Turn off aricondition press 2 \n";
             cin>>tur;
             if(turn==1&&con[0]==0)
            {
                cout<<"Aircondition is on \n";
                con[0]=1;
            }
            else
            {
                if(turn==1&&con[0]==1)
                cout<<"Aircondition is already on \n";
            }
             if(turn==2&&con[0]==1)
            {
                cout<<"Aircondition if off \n";
                con[0]=0;
            }
            else
            {
                if(turn==2&&con[0]==0)
                cout<<"Aircondition is already off \n";
            }
            cout<<"";break;
            case 2:cout<<"To turn on light press 1 To turn off light press 2\n";
        cin>>turn;
        if(turn==1&&light[1]==0)
        {
            cout<<"Light is on \n";
            light[1]=1;
        }
        else
        {
            if(turn==1&&light[1]==1)
            cout<<"Light is already on \n";
        }
         if(turn==2&&light[1]==1)
        {
            cout<<"Light if off \n";
            light[1]=0;
        }
        else
        {
            if(turn==2&&light[1]==0)
            cout<<"Light is already off \n";
            }
         /////air condition
         cout<<"To Turn on aircondition press 1 To Turn off aricondition press 2 \n";
         cin>>tur;
         if(turn==1&&con[1]==0)
        {
            cout<<"Aircondition is on \n";
            con[1]=1;
        }
        else
        {
            if(turn==1&&con[1]==1)
            cout<<"Aircondition is already on \n";
        }
         if(turn==2&&con[1]==1)
        {
            cout<<"Aircondition if off \n";
            con[1]=0;
        }
        else
        {
            if(turn==2&&con[1]==0)
            cout<<"Aircondition is already off \n";
            }
         cout<<"";break;
            case 3:cout<<"To turn on light press 1 To turn off light press 2\n";
        cin>>turn;
        if(turn==1&&light[2]==0)
        {
            cout<<"Light is on \n";
            light[2]=1;
        }
        else
        {
            if(turn==1&&light[2]==1)
            cout<<"Light is already on \n";
        }
         if(turn==2&&light[2]==1)
        {
            cout<<"Light if off \n";
            light[2]=0;
        }
        else
        {
            if(turn==2&&light[2]==0)
            cout<<"Light is already off \n";
            }
         /////air condition
         cout<<"To Turn on aircondition press 1 To Turn off aricondition press 2 \n";
         cin>>tur;
         if(turn==1&&con[2]==0)
        {
            cout<<"Aircondition is on \n";
            con[2]=1;
        }
        else
        {
            if(turn==1&&con[2]==1)
            cout<<"Aircondition is already on \n";
        }
         if(turn==2&&con[2]==1)
        {
            cout<<"Aircondition if off \n";
            con[2]=0;
        }
        else
        {
            if(turn==2&&con[2]==0)
            cout<<"Aircondition is already off \n";
            }
            case 4:cout<<"To turn on light press 1 To turn off light press 2\n";
        cin>>turn;
        if(turn==1&&light[3]==0)
        {
            cout<<"Light is on \n";
            light[3]=1;
        }
        else
        {
            if(turn==1&&light[3]==1)
            cout<<"Light is already on \n";
        }
         if(turn==2&&light[3]==1)
        {
            cout<<"Light if off \n";
            light[3]=0;
        }
        else
        {
            if(turn==2&&light[3]==0)
            cout<<"Light is already off \n";
            }
         /////air condition
         cout<<"To Turn on aircondition press 1 To Turn off aricondition press 2 \n";
         cin>>tur;
         if(turn==1&&con[3]==0)
        {
            cout<<"Aircondition is on \n";
            con[3]=1;
        }
        else
        {
            if(turn==1&&con[3]==1)
            cout<<"Aircondition is already on \n";
        }
         if(turn==2&&con[3]==1)
        {
            cout<<"Aircondition if off \n";
            con[3]=0;
        }
        else
        {
            if(turn==2&&con[3]==0)
            cout<<"Aircondition is already off \n";
            }
         cout<<"";break;
            case 5:cout<<"To open door press 1 To lock door press 2\n";
        cin>>turn;
        if(turn==1&&door[0]==0)
        {
            cout<<"Door open \n";
            door[0]=1;
        }
        else
        {
            if(turn==1&&door[0]==1)
            cout<<"Door is already open \n";
        }
         if(turn==2&&door[0]==1)
        {
            cout<<"Door if locked \n";
            door[0]=0;
        }
        else
        {
            if(turn==2&&door[0]==0)
            cout<<"Door is already Lock \n";
            }
            case 6:cout<<"To open door press 1 To lock door press 2\n";
        cin>>turn;
        if(turn==1&&door[1]==0)
        {
            cout<<"Door open \n";
            door[1]=1;
        }
        else
        {
            if(turn==1&&door[1]==1)
            cout<<"Door is already open \n";
        }
         if(turn==2&&door[1]==1)
        {
            cout<<"Door if locked \n";
            door[1]=0;
        }
        else
        {
            if(turn==2&&door[1]==0)
            cout<<"Door is already Lock \n";
            }
         cout<<"";break;
            case 7:menu();break;
            default:cout<<"PLEASE ENTER AGAIN";break;
            }
            }while(type!=7);
        
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    I do not see you passing anything by reference and there is no such thing as void main. I suggest you read through our C++ Made Easy Tutorials. Once you are done with that come back and ask a specific question.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    I did pass my reference from panichouse to the status... but the cout of the array is weird

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    You did not pass-by-reference. You passed-by-address, which is different.
    ...But you've been told this already, as well as changing the return type of main, and you even have another thread with pretty much the same issue, which you have yet to solve...
    AndrewHunter likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass by reference
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2007, 01:02 PM
  2. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 12:06 PM
  3. pass by reference .... HELP!!
    By NamelessNoob in forum C++ Programming
    Replies: 19
    Last Post: 02-15-2006, 11:50 AM
  4. Pass by reference
    By mcgeady in forum C Programming
    Replies: 11
    Last Post: 02-17-2005, 02:01 AM
  5. pass be reference versus pass by value
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 01:03 PM

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