Thread: crashing program, help

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231

    Question crashing program, help

    So i was making a program and i compiled and ran it, then for some weird reason it
    doesnt work when i compile it now. I put some break points in (i put on into the
    message loop too, it is pretty cool). I put them where I was editing the code most, part
    of the file backup program, if you could help me find the bugs in it that would be cool


    Code:
    #include <windows.h>
    #include <stdio.h>
    #define ID_EDIT 1
    #define ID_BUTTON 2
    #define ID_STATIC 3
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
      FILE *pfile;
      FILE *pfile2;
      static HWND hwndEdit;
      static HWND hwndEdit2;
      static HWND hwndEdit3;
      static HWND hwndEdit4;
      static HWND hwndButton;
      static HWND hwndStatic;
      static HWND hwndStatic2;
      static HWND hwndStatic3;
      static HWND hwndStatic4;
      static HWND hwndStatic5;
      static int len;
      static CHAR text[1000];
      static CHAR text2[1000];
      static CHAR text3[1000];
      static CHAR text4[1000];
      
      switch(msg)
      {
        case WM_CREATE:
    	hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
    				50, 70, 170, 140, hwnd, (HMENU) ID_EDIT,
    				NULL, NULL);
    
    	hwndEdit2 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
    				50, 30, 20, 20, hwnd, (HMENU) ID_EDIT,
    				NULL, NULL);
    				
    	hwndEdit3 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
    				120, 30, 20, 20, hwnd, (HMENU) ID_EDIT,
    				NULL, NULL);
                    
        hwndEdit4 = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
    				190, 30, 40, 20, hwnd, (HMENU) ID_EDIT,
    				NULL, NULL);  
                    
        hwndStatic = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
    				1, 1, 300, 20, hwnd, (HMENU) ID_STATIC,
    				NULL, NULL);  
        
        hwndStatic2 = CreateWindow(TEXT("static"), "/", WS_CHILD | WS_VISIBLE,
    				1, 5, 300, 20, hwnd, (HMENU) ID_STATIC,
    				NULL, NULL); 
                                                      			
        hwndStatic3 = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
    				1, 3, 300, 20, hwnd, (HMENU) ID_STATIC,
    				NULL, NULL); 
    				
    	hwndStatic4 = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
    				1, 4, 300, 20, hwnd, (HMENU) ID_STATIC,
    				NULL, NULL); 			
    				
    				
    	hwndStatic5 = CreateWindow(TEXT("static"), "please insert your date:", WS_CHILD | WS_VISIBLE,
    				1, 5, 300, 20, hwnd, (HMENU) ID_STATIC,
    				NULL, NULL); 			
    				
        hwndButton = CreateWindow(
    		TEXT("button"), TEXT("set title and write to file"),       
    		WS_VISIBLE | WS_CHILD,  
    		30, 240, 200, 25,        
    		hwnd, (HMENU) ID_BUTTON, NULL, NULL);      
    
    	break;
    
    	case WM_COMMAND:	     
               if (HIWORD(wParam) == BN_CLICKED) {
                   pfile = fopen("database1234.xls", "r");
                   printf("break1");
                   if(pfile == NULL){
                   printf("breakloop2");
                   MessageBox(hwnd,"the database for the program was deleted or moved, we will now use the backup,\neverything you enter now will be put into the original file\n and the backup\n the program will try to copy the backup.\n(the program will take care of everything)","database not found",MB_OK);
                   printf("breakloop3");
                   pfile2 = fopen("C:\\reminder\\data\\backup1.xls", "r");
                   printf("breakloop4");
                   if(pfile2 == NULL){
                   printf("breakloop5");
                   system("mkdir C:\\reminder\\data");
                   printf("breakloop6");
                   pfile = fopen("C:reminder\\data\\backup1.xls", "a");}
                            printf("breakloop");
                            }
                   else{
                   printf("breakloop7");
                   fclose(pfile);
                   printf("breakloop8");
                   pfile = fopen("database1234.xls", "a");
                        printf("breakloop9");
                        }
                   fclose(pfile);
                  printf("break10");
                  fclose(pfile2);
                  printf("break11");
                  pfile = fopen("database1234.xls", "a");
                  printf("break12");
                  pfile2 = fopen("C:\\reminder\\data\\backup1.xls", "a"); 
                   printf("break13");
                   len = GetWindowTextLength(hwndEdit) + 1;
                   GetWindowText(hwndEdit, text, len);
                   GetWindowText(hwndEdit2, text2, len);
                   GetWindowText(hwndEdit3, text3, len);
                   GetWindowText(hwndEdit4, text4, len);
                   SetWindowText(hwnd, text);
                   printf("break14");
                   fprintf(pfile,"%s,%s,%s,\"%s\"\n",text2,text3,text4,text); 
                   printf("break15");
                   fprintf(pfile2,"%s,%s,%s,\"%s\"\n",text2,text3,text4,text);
                   printf("break16");
                   //MessageBox(hwnd,text,"text",MB_OK);
                  printf("break17");
                  fclose(pfile);
                  printf("break18");
                  fclose(pfile2);
               printf("break19");
               }
    	break;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    	break;
      }
      return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    			LPSTR lpCmdLine, int nCmdShow )
    {
      MSG  msg ;    
      WNDCLASS wc = {0};
      wc.lpszClassName = TEXT( "Edit Control" );
      wc.hInstance     = hInstance ;
      wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
      wc.lpfnWndProc   = WndProc ;
      wc.hCursor       = LoadCursor(0,IDC_ARROW);
    
      
      RegisterClass(&wc);
      CreateWindow( wc.lpszClassName, TEXT("Edit control"),
                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                    1000, 420, 280, 350, 0, 0, hInstance, 0);  
    
      while( GetMessage(&msg, NULL, 0, 0)) {
        printf("message");
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
      return (int) msg.wParam;
    }
    thanks (and if you could take the cmd window out somehow too that would be cool)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Your code is impossible to read, it has no indentation.

    At least not in the critical bit where the interesting stuff is happening.

    Code:
    if (HIWORD(wParam) == BN_CLICKED) {
      doSomething();
    }
    Create a whole new function for that block of code and indent it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I think this is the part u are talking about
    Code:
    case WM_COMMAND:	     
               if (HIWORD(wParam) == BN_CLICKED) {
                   
                   pfile = fopen("database1234.xls", "r");
                   
                   printf("break1");
                   
                   if(pfile == NULL){
                   
                   printf("breakloop2");
                   
                   MessageBox(hwnd,"the database for the program was deleted or moved, we will now use the backup,\neverything you enter now will be put into the original file\n and the backup\n the program will try to copy the backup.\n(the program will take care of everything)","database not found",MB_OK);
                   
                   printf("breakloop3");
                   
                   pfile2 = fopen("C:\\reminder\\data\\backup1.xls", "r");
                   
                   printf("breakloop4");
                   
                   if(pfile2 == NULL){
                   
                              printf("breakloop5");
                              
                              system("mkdir C:\\reminder\\data");
                   
                              printf("breakloop6");
                              
                              pfile = fopen("C:reminder\\data\\backup1.xls", "a");}
                            
                              printf("breakloop");
                            }
                            
                                 else{
                                       printf("breakloop7");
                              
                                       fclose(pfile);
                   
                                       printf("breakloop8");
                   
                                       pfile = fopen("database1234.xls", "a");
                        
                                       printf("breakloop9");
                                     }
                   fclose(pfile);
                  
                   printf("break10");
                  
                   fclose(pfile2);
                  
                   printf("break11");
                  
                   pfile = fopen("database1234.xls", "a");
                  
                   printf("break12");
                  
                   pfile2 = fopen("C:\\reminder\\data\\backup1.xls", "a"); 
                   
                   printf("break13");
                   
                   len = GetWindowTextLength(hwndEdit) + 1;
                   
                   GetWindowText(hwndEdit, text, len);
                   
                   GetWindowText(hwndEdit2, text2, len);
                   
                   GetWindowText(hwndEdit3, text3, len);
                   
                   GetWindowText(hwndEdit4, text4, len);
                   
                   SetWindowText(hwnd, text);
                   
                   printf("break14");
                   
                   fprintf(pfile,"%s,%s,%s,\"%s\"\n",text2,text3,text4,text); 
                   
                   printf("break15");
                   
                   fprintf(pfile2,"%s,%s,%s,\"%s\"\n",text2,text3,text4,text);
                   
                   printf("break16");
                   
                   //MessageBox(hwnd,text,"text",MB_OK);
                  
                  printf("break17");
                  
                  fclose(pfile);
                  
                  printf("break18");
                  
                  fclose(pfile2);
                  
                  printf("break19");
               }
    	break;
    
    	case WM_DESTROY:
    		
            PostQuitMessage(0);
    	
        break;
             }

  4. #4
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    Ok so it doesnt crash, I added some stuff that makes the app
    user friendly but then I added

    Code:
    case VK_LBUTTON:
                  SetWindowText(hwndStatic5,("")); 
                  break;
    which should set this control to saying nothing but it always says

    Code:
    duplicate case value (to the case WM_DESTROY) 
    previously used here (to the case VK_LBUTTON)
    I hope im not to vague on the concept

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing when replacing newline character
    By mdekom12 in forum C Programming
    Replies: 2
    Last Post: 05-01-2010, 08:49 PM
  2. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM