Why dosent this work?

This is a discussion on Why dosent this work? within the C++ Programming forums, part of the General Programming Boards category; Can someone tell me why this dosent change the value of sel thats declared in main? void function(float *sel); float ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    411

    Why dosent this work?

    Can someone tell me why this dosent change the value of sel thats declared in main?


    void function(float *sel);

    float number = 3;

    main() {
    float *sel = new float[3];
    function(sel);
    //sel[0] donsent == number ????
    delete[] sel;
    }

    functon(float *sel) {
    sel[0] = number;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Apparently function() is a reserved name, I got an error on that until I changed it to func(). After I did that the program worked just fine.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Since functon(float *sel) is different from function(float *sel). That's why, you made a typing error.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    regardless of the typos I posted here, why dosent this work right.

    My real program is much more complex, this is just the part that I have narrowed down the problem to.

  5. #5
    Snior Member
    Join Date
    Jan 2002
    Posts
    982
    If you correct the typo it does work.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If you correct the typo it does work.

    Actually it doesn't on my compiler. Now that I'm more awake I noticed that in your function definition you forgot to include the return type. This also may be a typo though.
    Code:
    function(float *sel) { // Will flag an error
      sel[0] = number; 
    }
    Code:
    void function(float *sel) { // Works fine
      sel[0] = number; 
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    It is, I will post my real code for clarity.

    heres the function thats starting from

    Code:
    void menus::Mouse(short type, int x, int y) {
    	float *sel = new float[3];
    	glh.Selectf(sel);
    	
    	if(glh.Checkf(sel,skm_btn->col)) battle.Start();
    	if(glh.Checkf(sel,ted_btn->col)) ted.Start();
    
    	delete[] sel;
    }
    
    void glhs::Selectf(float sel[]) {
    	float r,g,b;
    	if(selectmode == false) {
    		display(GLH_SELECT);
    		selectmode = true;
    	}
    	glReadPixels(curx,tempy,1,1,GL_RED,GL_FLOAT,&r);
    	glReadPixels(curx,tempy,1,1,GL_GREEN,GL_FLOAT,&g);
    	glReadPixels(curx,tempy,1,1,GL_BLUE,GL_FLOAT,&b);
    	sel[0] = r;
    	sel[1] = g;
    	sel[2] = b;
    }
    bool glhs::Checkf(float *sel, float *colors) {
    	bool stater = false, stateg = false, stateb = false;
    	if(sel[0] > colors[0]-0.049 && sel[0] < colors[0]+0.049) stater = true;
    	if(sel[1] > colors[1]-0.049 && sel[1] < colors[1]+0.049) stateg = true;
    	if(sel[2] > colors[2]-0.049 && sel[2] < colors[2]+0.049) stateb = true;
    	if(stater == true && stateg == true && stateb == true) return true;
    	else return false;
    }
    I know that the r,g,b variables int selectf have the right values, but those values never make it to the checkf function? I can make it work without arrays, but I really want to use them here.

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    void glhs::Selectf(float sel[]) {
    should be
    void glhs::Selectf(float* sel) {
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    That was just something I was testing, but ive tried it both ways.

    I cant for the life of me figure out why it isnt working.

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    add some trace statements after calling selectf() to print out the values stored in sel. what do you get?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    It gives me 0,0,0 for the array after calling Selectf, but inside the function I know that r,g,b are not equil to 0,0,0.

    I have a sister function that just returns the r,g,b variables, useing that I can say something like this

    sel[0] = glh.Selectf(RED);
    sel[1] = glh.Selectf(GREEN);
    sel[2] = glh.Selectf(BLUE);

    and it works fine? I checked for variables with the same name, the only other sel variable in the program is in a class thats not even active when this is. Do you have any other ideas on what could cause this to mess up indirectly?

  12. #12
    Unregistered
    Guest
    please post glReadPixels()

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    Thats an OpenGL library function, it dose return the values that I want, They just never make it to the sel array?

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    now print out the values returned from glReadPixels. Are they all zero too?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  15. #15
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    what compiler are you using? the code looks ok.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  4. Why dosen't this work
    By Granger9 in forum C Programming
    Replies: 4
    Last Post: 08-16-2002, 08:18 PM
  5. sigh... why dosent this work?
    By jon_nc17 in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2002, 08:30 PM

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