Thread: Debugging program

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    3

    Debugging program

    I have written the following code myself:

    Code:
    #include <stdio.h>
    
    int main()
    
    
    {
    int width;
    int height;
    
    
    printf("Please enter the width and height for your rectangle (separated by a comma, e.g. 3,4): \n"); 
    scanf("%d,%d", &width, &height);
    
    
         {
         if (width > 80)
             {
             printf("Invalid value - %d is too wide.\n", width);
             }
         else if (width < 0)
             {
             printf("Invalid value: %d is a negative integer.\n", width);
             }
         else 
             return 0; 
         }
    
    
         {
         if (height > 80)
             {
             printf("Invalid value - %d is too high.\n", height);
             }
         else if (height < 0)
             {
             printf("Invalid value: %d is a negative integer\n", height);
             }
         else 
             return 0;
         }
         
         while (width == 0 || height == 0 )
             break;
         
    {
         for (height > 0 && height <= 80; height == 0; --height)
         { 
             for(width > 0 && width <= 80; width == 0; --width)
                 {
                     printf("*");
                 }
             printf("\n");
         }
    
    
         while ((height * width) > 0 && (height * width) <= 6400)
         {
         int main();
         }
    }
    }
    However after I compile this code, the code/program asks for the input and does not return the output. In this case, the output is a rectangle (maximum dimensions 80x80 characters) composed of asterisks.

    Any assistance/suggestions on debugging this program would be appreciated.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I would suggest that you look into increasing your compiler warning level. There are several problems with your code that your compiler should be able to warn you about.

    main.c||In function ‘main’:|
    main.c|46|warning: statement with no effect [-Wunused-value]|
    main.c|48|warning: statement with no effect [-Wunused-value]|
    main.c|58|warning: redundant redeclaration of ‘main’ [-Wredundant-decls]|
    main.c|3|note: previous definition of ‘main’ was here|
    ||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
    Note: It looks like you're trying to call main() instead of redeclaring it. However main() should really never be called by any function including it'self.


    Jim

  3. #3
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi ..
    looking at the code, those curly braces, it seems you wanted to have a few functions in the program, they are just not coded properly. As it stands now it never gets to see the printf("*") statement as a valid width value will hit the return 0 and thus end the program.
    After cleaning up the compile warnings as suggested.... have a look at placing those blocks of code that finish with a return into a function of its own..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    1. Indentation
    2. Indentation (seriously)
    Code:
    #include <stdio.h>
    
    int main()
    {
      int width;
      int height;
    
      printf("Please enter the width and height for your rectangle "
             "(separated by a comma, e.g. 3,4): \n");
      scanf("%d,%d", &width, &height);
    
      {
        if (width > 80) {
          printf("Invalid value - %d is too wide.\n", width);
        } else if (width < 0) {
          printf("Invalid value: %d is a negative integer.\n", width);
        } else
          return 0;
      }
    
    
      {
        if (height > 80) {
          printf("Invalid value - %d is too high.\n", height);
        } else if (height < 0) {
          printf("Invalid value: %d is a negative integer\n", height);
        } else
          return 0;
      }
    
      while (width == 0 || height == 0)
        break;
    
      {
        for (height > 0 && height <= 80; height == 0; --height) {
          for (width > 0 && width <= 80; width == 0; --width) {
            printf("*");
          }
          printf("\n");
        }
    
        while ((height * width) > 0 && (height * width) <= 6400) {
          int main();
        }
      }
    }
    https://en.wikipedia.org/wiki/Indent_style

    Look at lines 18 and 28
    return from what?
    Since this is in main(), it's just the end of the program when the user types an in-range number.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debugging program?
    By brack in forum C Programming
    Replies: 4
    Last Post: 11-21-2010, 03:20 PM
  2. help debugging program.
    By martin249 in forum C Programming
    Replies: 5
    Last Post: 09-18-2010, 09:35 AM
  3. Help for debugging a program
    By Saeid87 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 06:44 AM
  4. help debugging my program
    By kenyi in forum C Programming
    Replies: 2
    Last Post: 08-04-2007, 11:26 PM
  5. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM