Thread: Multiple if statements

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    1

    Multiple if statements

    Hello I just started programming in C at school. I have, as homework, to create a program i would use. I wanted to create one that would let me know where would i placed in LoL according to my placement wins, but i dont know where im having problems. Can you let me know what should i do?

    Code:
    #include <stdio.h>#include <stdlib.h>
    int main(){
        int PMMR;
        int plw;
        int plwMMR;
        int nplwMMR;
        int AMMR;
        int WMMR=1300;
        printf("Introduce tu pre-reset MMR: ");
        scanf("%i",&PMMR);
        printf("Introduzca placement wins: ");
        scanf("%i",&plw);
        nplwMMR=PMMR+WMMR/2;
        plwMMR=((2*plw-10)*(50*(PMMR+1200))/2);
        AMMR=nplwMMR+plwMMR;
        if (AMMR>=0&&AMMR<=299);printf("Tu liga, aproximadamente, es: Bronze V");
        if (AMMR>=230&&AMMR<=459);printf("Tu liga, aproximadamente, es: Bronze IV");
        if (AMMR>=460&&AMMR<=689);printf("Tu liga, aproximadamente, es: Bronze III");
        if (AMMR>=690&&AMMR<=919);printf("Tu liga, aproximadamente, es: Bronze II");
        if (AMMR>=920&&AMMR<=1149);printf("Tu liga, aproximadamente, es: Bronze I");
        if (AMMR>=1150&&AMMR<=1219);printf("Tu liga, aproximadamente, es: Silver V");
        if (AMMR>=1220&&AMMR<=1289);printf("Tu liga, aproximadamente, es: Silver IV");
        if (AMMR>=1290&&AMMR<=1359);printf("Tu liga, aproximadamente, es: Silver III");
        if (AMMR>=1360&&AMMR<=1429);printf("Tu liga, aproximadamente, es: Silver II");
        if (AMMR>=1430&&AMMR<=1499);printf("Tu liga, aproximadamente, es: Silver I");
        if (AMMR>=1500&&AMMR<=1569);printf("Tu liga, aproximadamente, es: Gold V");
        if (AMMR>=157&&AMMR<=1639);printf("Tu liga, aproximadamente, es: Gold IV");
        if (AMMR>=1640&&AMMR<=1709);printf("Tu liga, aproximadamente, es: Gold III");
        if (AMMR>=1710&&AMMR<=1779);printf("Tu liga, aproximadamente, es: Gold II");
        if (AMMR>=1780&&AMMR<=1849);printf("Tu liga, aproximadamente, es: Gold I");
        if (AMMR>=1850&&AMMR<=1919);printf("Tu liga, aproximadamente, es: Platinum V");
        if (AMMR>=1920&&AMMR<=1989);printf("Tu liga, aproximadamente, es: Platinum IV");
        if (AMMR>=1990&&AMMR<=2059);printf("Tu liga, aproximadamente, es: Platinum III");
        if (AMMR>=2060&&AMMR<=2129);printf("Tu liga, aproximadamente, es: Platinum II");
        if (AMMR>=2130&&AMMR<=2199);printf("Tu liga, aproximadamente, es: Platinum I");
        if (AMMR>=2200&&AMMR<=2269);printf("Tu liga, aproximadamente, es: Diamond V");
        if (AMMR>=2270&&AMMR<=2339);printf("Tu liga, aproximadamente, es: Diamond IV");
        if (AMMR>=2340&&AMMR<=2409);printf("Tu liga, aproximadamente, es: Diamond III");
        if (AMMR>=2410&&AMMR<=2479);printf("Tu liga, aproximadamente, es: Diamond II");
        if (AMMR>=2480&&AMMR<=2550);printf("Tu liga, aproximadamente, es: Diamond I");
    }

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    "Three or more, use a for".

    Condense those values into a table (array of structs).

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    There is method to the madness. For Bronze they go up by 230, and after that they go up by 70 (making allowances for a couple of typos).
    Code:
    #include <stdio.h>
    
    #define METALS_SIZE    5
    #define NUMBERS_SIZE   5
    #define INCREMENT1   230  // Used only for basest metal (bronze in element 0).
    #define INCREMENT2    70  // Used for all other metals.
    #define MAX_LIMIT    ((INCREMENT1*NUMBERS_SIZE)+((METALS_SIZE-1)*NUMBERS_SIZE*INCREMENT2))
    
    int main(int argc, char *argv[]) {
      char *metal[METALS_SIZE] = {"Bronze", "Sliver", "Gold", "Platinum", "Diamond"};
      char *number[NUMBERS_SIZE] = {"V", "IV", "III", "II", "I"};
      int AMMR = 0, i, limit;
    
      if (argc > 1) AMMR = atoi(argv[1]);
    
      if (AMMR < 0) return 1;     // handle errors
      if (AMMR > MAX_LIMIT) return 1;
    
      limit = INCREMENT1;
      for (i = 0; AMMR > limit; i++)
        limit += (i < NUMBERS_SIZE-1) ? INCREMENT1 : INCREMENT2;
    
      printf("%s %s\n", metal[i / NUMBERS_SIZE], number[i % NUMBERS_SIZE]);
    
      return 0;
    }
    EDIT: A generalization of the above.
    Code:
    #include <stdio.h>
    
    #define METALS_SIZE    5
    #define NUMBERS_SIZE   5
    
    int main(int argc, char *argv[]) {
      const char *metals[METALS_SIZE] = {"Bronze", "Sliver", "Gold", "Platinum", "Diamond"};
      const char *numbers[NUMBERS_SIZE] = {"V", "IV", "III", "II", "I"};
      int incs[METALS_SIZE] = {230, 70, 70, 70, 70};
      int ammr = 0, i, limit, max_limit = 0;
    
      for (i = 0; i < METALS_SIZE; i++)
        max_limit += NUMBERS_SIZE * incs[i];
    
      if (argc > 1) ammr = atoi(argv[1]);
    
      if (ammr < 0) return 1;     // handle errors
      if (ammr > max_limit) return 1;
    
      limit = incs[0];
      for (i = 0; ammr > limit; )
        limit += incs[++i / NUMBERS_SIZE];
    
      printf("%s %s\n", metals[i / NUMBERS_SIZE], numbers[i % NUMBERS_SIZE]);
    
      return 0;
    }
    Last edited by algorism; 01-21-2016 at 07:39 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by Valter Núñez View Post
    but i dont know where im having problems. Can you let me know what should i do?
    When asking for help with a problem, you should explain the problem that you are having. The more specific you are, the better chance you have of receiving meaningful assistance.

    You should also be compiling with maximum warnings.

    Code:
    /*
    main.c||In function 'main':|
    main.c|17|warning: suggest braces around empty body in an 'if' statement|
    main.c|18|warning: suggest braces around empty body in an 'if' statement|
    main.c|19|warning: suggest braces around empty body in an 'if' statement|
    main.c|20|warning: suggest braces around empty body in an 'if' statement|
    main.c|21|warning: suggest braces around empty body in an 'if' statement|
    main.c|22|warning: suggest braces around empty body in an 'if' statement|
    main.c|23|warning: suggest braces around empty body in an 'if' statement|
    main.c|24|warning: suggest braces around empty body in an 'if' statement|
    main.c|25|warning: suggest braces around empty body in an 'if' statement|
    main.c|26|warning: suggest braces around empty body in an 'if' statement|
    main.c|27|warning: suggest braces around empty body in an 'if' statement|
    main.c|28|warning: suggest braces around empty body in an 'if' statement|
    main.c|29|warning: suggest braces around empty body in an 'if' statement|
    main.c|30|warning: suggest braces around empty body in an 'if' statement|
    main.c|31|warning: suggest braces around empty body in an 'if' statement|
    main.c|32|warning: suggest braces around empty body in an 'if' statement|
    main.c|33|warning: suggest braces around empty body in an 'if' statement|
    main.c|34|warning: suggest braces around empty body in an 'if' statement|
    main.c|35|warning: suggest braces around empty body in an 'if' statement|
    main.c|36|warning: suggest braces around empty body in an 'if' statement|
    main.c|37|warning: suggest braces around empty body in an 'if' statement|
    main.c|38|warning: suggest braces around empty body in an 'if' statement|
    main.c|39|warning: suggest braces around empty body in an 'if' statement|
    main.c|40|warning: suggest braces around empty body in an 'if' statement|
    main.c|41|warning: suggest braces around empty body in an 'if' statement|
    */
    empty body in an 'if' statement - that means your compiler isn't seeing anything happening under any of your "if"s.

    Code:
    if (AMMR>=0&&AMMR<=299);printf("Tu liga, aproximadamente, es: Bronze V");
    There is a semi-colon after all of your "if" statements.

    Code:
    if (AMMR>=0&&AMMR<=299);
    It doesn't belong there. It should look more like this:

    Code:
    if(AMMR>=0&&AMMR<=299)  // <--- no semi-colon after the "if"!
        printf("Tu liga, aproximadamente, es: Bronze V");
    In C, a semi-colon by itself is a valid statement, which says "do nothing". It is called a null statement.

    What your current code is actually doing:

    Code:
    if(AMMR>=0&&AMMR<=299)   // comparison
        ;                    // if true, do nothing
    printf("Tu liga, aproximadamente, es: Bronze V");  // this will always be printed since it's not part of the if
    Also, you should use more newlines and indention to make your code easier to read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple if statements
    By Curtis Coffman in forum C Programming
    Replies: 5
    Last Post: 09-05-2012, 07:13 PM
  2. if statement with multiple switch statements.
    By blu in forum C Programming
    Replies: 27
    Last Post: 02-16-2012, 11:05 PM
  3. help with multiple switch statements and arrays
    By dangalong801 in forum C Programming
    Replies: 7
    Last Post: 05-23-2011, 01:58 AM
  4. Multiple if statements and conditions
    By tomeatworld in forum C Programming
    Replies: 19
    Last Post: 11-07-2010, 10:43 AM
  5. Multiple If Statements Question.
    By thekautz in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2008, 03:06 PM

Tags for this Thread