Thread: Passing counters in functions?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    11

    Passing counters in functions?

    Hi,
    I'll post my code here if someone wants to help. It's a bit simplified compared to the real code. I've borrowed some stuff from others on this forum. Thanks!

    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    
    int MouseScroll(int roof) {
    	
    	HANDLE hStdin,hStdout;
    	hStdin=GetStdHandle(STD_INPUT_HANDLE);
    	hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
    	DWORD num;
    	INPUT_RECORD inputrecord;
    	COORD location;
    	
    	location.X=0;
    	location.Y=0;
    	
    	int x_gammal = 0;
    	int i = 0; //What to do with counter int?
    	
    	while (true) {
    		
    		ReadConsoleInput(hStdin,&inputrecord,1,&num); 
    		
    		if (inputrecord.EventType == MOUSE_EVENT) {
    			
    			SetConsoleCursorPosition(hStdout,location);
    			int x_ny = inputrecord.Event.MouseEvent.dwMousePosition.X;
    			
    			if (x_gammal != x_ny) {
    				
    				if (x_gammal < x_ny) {
    					
    					if (i < roof) {
    						i++; 
    						cout << i << flush; }
    					
    					else {
    						i = roof+1;
    						cout << i << flush; }
    				}
    				
    				else {
    					
    					if (i > 0) {
    						i--; 
    						cout << i << flush; }
    					
    					else {
    						i = 0;
    						cout << i << flush; }
    				}
    				
    				x_gammal = x_ny;
    			}
    		}
    	}
    }
    
    
    
    void MenuHandler(int choice) {
    	
    	int x_gammal = 0;
    	
    	int i = 0; 
    	int menu_roof = 0;
    	long menu[] = {0,0,0};
    	
    	//Menu choice 
    		
    	switch (choice) { //More cases will be added
    		
    	case 1: //Focus mode
    		menu[0] = 2; 
    		menu[1] = 6;
    		menu[2] = 3; 
    		
    		menu_roof = 2;
    		
    		MouseScroll(menu_roof);
    		
    		//SetPropFocusMode(0, menu[i]);  //Used with other SDK
    		break;	
    	}
    }
    	
    
    
    	int main() {
    		
    		int menu_value = 0;
    		bool Continue = 1;
    		
    		
    		//camera control
    		//InitControl();
    		//GetPropCameraCount();
    		//Connect(0);
    		
    		while(Continue) {
    			
    			if (GetAsyncKeyState(VK_F1) < 0) {
    				menu_value = 1;
    				MenuHandler(menu_value);
    			}
    			
    		}
    		
    		//DisConnect(0);
    		return 0;
    	}
    Every time the function MouseScroll is called i is set to zero. This stops it from changing its value. I would like MouseScroll to remember what value i had previously.

    If everything then works as I expect the following will happen:
    when F1 is held down I would be able to move the mouse in the x-direction and increase or decrease i.

    I made it work when I wrote the code in one piece but not when splitting it into different functions.

    Thanks very much for any help!

    /Kristian

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    int MouseScroll(int roof) {
    	/* .... */
    	static int i = 0;
            /* .... */
    }
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    11
    Thanks! Now it's working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing by reference to functions
    By avi2886 in forum C Programming
    Replies: 12
    Last Post: 03-03-2009, 01:56 PM
  2. Trouble passing args to functions in other files
    By Midnight Coder in forum C Programming
    Replies: 6
    Last Post: 01-03-2009, 05:13 PM
  3. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM
  4. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM