Results 1 to 7 of 7

Thread: integers

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Angry integers

    Code:
    int result;
    
    int len = GetWindowTextLength(editwage);
    
    if(len>0)
    	TCHAR *buf = new TCHAR[len + 1];		
    	GetWindowText(editwage,buf,len+1);
    	MessageBox(NULL,buf,"hi",MB_OK);
    	int s = atoi(buf);
    
    int len2 = GetWindowTextLength(edithrs);
    
    if(len2>0)
    	TCHAR *buf2 = new TCHAR[len2 + 1];
    	GetWindowText(edithrs,buf,len2+1);
    	int b = atoi(buf2);
    
    	result = s * b;
    
    	char buffer[10];
    	char* sztext = itoa(result,buffer,10);
    
    	MessageBox(NULL,sztext,"testing",MB_OK);
    	
    return 0;
    }
    When I run this code or try to i get the error" buf, and buf2 undeclared identifiers". I declared them though. And then once I try and do the calculation on multiplying s and b I get 0 in the messagebox. I cannot figure out what is going on. Any help is greatly appreciated!!
    Matt

  2. #2
    Declare the variables outside the if blocks. Variable scope is causing the error.

  3. #3
    Megatron
    Guest
    If your if statement requires more than one statement in it, you need to enclose them in braces
    Code:
    if(len>0) 
    {
    	TCHAR *buf = new TCHAR[len + 1];		
    	GetWindowText(editwage,buf,len+1);
    	MessageBox(NULL,buf,"hi",MB_OK);
    	int s = atoi(buf);
    }

  4. #4

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    but will i be able to use the variable s outside of the braces, or in another set?
    Matt

  5. #5
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    I don't see your starting brace but if you declare the variable as global you will be able to use it everywhere. Global declaring is not recommended though!
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  6. #6
    Code:
    // wrong
    if (1)
    {
        int i = 3;
    }
    cout << i; // variable not defined in this scope
    
    // correct
    int i;
    if (1)
    {
        i = 3;
    }
    cout << i; // variable is defined in scope

  7. #7

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    finally got it thanks alot guys!
    Matt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width