Thread: I need help with my code, i really dont know thats wrong with it

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    2

    Unhappy I need help with my code, i really dont know thats wrong with it

    The code works perfectly if for example if i want to check 3 quadrilaterals, but when i want to check 5 quadrilateralsit almost always prints the first wrong and the rest just fine, and i dont know why that is or how to fix it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      int check;
      int i = 1;
      int a[i];
      int b[i];
      int c[i];
      int d[i];
     
      
      
      
      printf("Please enter the number of quadrilaterals to check: ");
      scanf("%d", &check);
      if(!(check >= 1) || !(check <=127))
      {
        while(!(check >= 1) || !(check <=127))
        {
          printf("Error: Invalid number of quadrilaterals.\n");
          printf("Please enter the number of quadrilaterals to check: ");
          scanf("%d", &check);
        }
      }
     
      
    
    
      while(i <= check){
      printf("Please enter the four sides for quadrilateral %d: ", i);
      scanf("%d" "%d" "%d" "%d", &a[i], &b[i], &c[i], &d[i]); 
      if((!(a[i] >= 1) || !(a[i] <= 225)) || (!(b[i] >= 1) || !(b[i] <= 225)) || (!(c[i] >= 1) || !(c[i] <= 225)) || (!(d[i] >= 1) || !(d[i] <= 225))) 
      {
        while((!(a[i] >= 1) || !(a[i] <= 225)) || (!(b[i] >= 1) || !(b[i] <= 225)) || (!(c[i] >= 1) || !(c[i] <= 225)) || (!(d[i] >= 1) || !(d[i] <= 225)))
        {
          printf("Error: Invalid number of side.\n");
          printf("Please enter the four sides for quadrilateral %d: ", i);
          scanf("%d" "%d" "%d" "%d", &a[i], &b[i], &c[i], &d[i]);
        }
      }    
        
      i++;
      
    }
      for(i = 1; i <= check; i++) {
        if(a[i] == b[i] && a[i] == c[i] && a[i] == d[i]){
          printf("Quadrilateral %d : (a= %d,  b= %d, c= %d, d= %d) is square.\n", i, a[i], b[i], c[i], d[i]);
        }
        else if (a[i] == c[i] && b[i] == d[i]){
          printf("Quadrilateral %d : (a= %d,  b= %d, c= %d, d= %d) is rectangle.\n", i, a[i], b[i], c[i], d[i]);
        }
        else if ((a[i] == c[i] && b[i] != d[i]) || (a[i] != c[i] && b[i] == d[i])){
          printf("Quadrilateral %d : (a= %d,  b= %d, c= %d, d= %d) is isosceles trapezium.\n", i, a[i], b[i], c[i], d[i]);
        }
        else if ((a[i] == d[i] && b[i] == c[i]) || (a[i] == b[i] && c[i] == d[i])){
          printf("Quadrilateral %d : (a= %d,  b= %d, c= %d, d= %d) is kite.\n", i, a[i], b[i], c[i], d[i]);
        }
        else printf("Quadrilateral %d : (a= %d,  b= %d, c= %d, d= %d) is irregular quadrilateral.\n", i, a[i], b[i], c[i], d[i]);
          
          
      
        
        }
    
      
      
      
      
      return 0;
      
    }
    Attached Files Attached Files
    Last edited by obelisk1151; 04-24-2020 at 07:02 AM. Reason: Added code tags -

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int i = 1;
    > int a[i];
    You don't create a[2] just by doing i++ later on in the code.

    What you do end up doing is trashing memory.

    If you want 5, then create 5 up front.
    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
    Registered User
    Join Date
    Apr 2020
    Posts
    2
    but the thing is i dont know how many quadrilaterals the user wants to check, it can go up to 125.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well
    int a[128];
    etc is no big deal on a modern machine.
    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.

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    Code:
    // clockwise 
    
    #include <stdio.h>
    
    void typeOfQuad(int a, int b, int c, int d) {
        if (a == b && b == c && c == d && d == a) {
            printf("square");
        }
        if (a == c && b == d) printf("rectangle");
        //etc...
    }
    
    int main() {
    
        int quads;
        printf("Enter the number of quadrilaterals to check: ");
        scanf("%d",&quads); 
        int a[quads], b[quads], c[quads], d[quads];
    
        for (int i = 1; i < (quads+1); i++) {
            printf("Enter the four sides of quadrilateral %i: ", i);
            scanf("%d %d %d %d", &a[i-1], &b[i-1], &c[i-1], &d[i-1]);
            printf("\n");
        }
    
        for (int i = 0; i < quads; i++) {
            printf("Quadrilateral %d is ", i );
                typeOfQuad( a[i], b[i], c[i], d[i] );
            printf(".\n");
        }
    
        return 0;
    }
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please check my code, dont know whats wrong
    By Limitless in forum C Programming
    Replies: 9
    Last Post: 11-03-2017, 07:04 AM
  2. dont know where im going wrong
    By africanwizz in forum C Programming
    Replies: 5
    Last Post: 10-06-2013, 08:32 AM
  3. I dont know what is wrong???
    By amroto in forum C Programming
    Replies: 3
    Last Post: 05-18-2012, 06:19 AM
  4. dont know what im doing wrong... array
    By jamort in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2009, 12:23 PM
  5. i dont know what is wrong with it (help plz)
    By abuna in forum C++ Programming
    Replies: 16
    Last Post: 09-08-2005, 12:03 PM

Tags for this Thread