Thread: how can i write it shorter?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    1

    how can i write it shorter?

    so i'm beginner and i think this is weird(in if and do function) have another easy way?
    sorry for my bad english
    Code:
    #include"stdio.h"
    int main(){
        int a,b,c,sum=0;
    
    
    do{    printf("put  A : ");
    scanf("%d",&a);
    }
    while(a>30);
    
    
    do{    printf("put  B : ");
    scanf("%d",&b);
    }
    while(b>30);
    
    
    do{    printf("put  C : ");
    scanf("%d",&c);
    }
    while(c>40);
    
    
    sum=a+b+c;
    //printf("sum = %d",sum);
    
    
    if(sum>=80){ printf("A");}
    if(sum>=75&&sum<=79){ printf("B+");}
    if(sum>=70&&sum<=74){ printf("B");}
    if(sum>=65&&sum<=69){ printf("C+");}
    if(sum>=60&&sum<=64){ printf("C");}
    if(sum>=55&&sum<=59){ printf("D+");}
    if(sum>=50&&sum<=54){ printf("D");}
    if(sum<50){ printf("F");}
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,269
    You can simplify your conditions: since you already check for 80+ for A, you only need to check for 75+ for B+. You dont need to check for <= 79 because if the number was > 79, it would have been under the A+ condition.

    This also means that you can create two arrays: an array of ints containing the lower bounds of the grades, and another corresponding array of strings containing the letter grades. You could then loop over the array of ints and check.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Some food for thought: You may want to consider a look up table

    If you had an array of strings with 20 elements, you could do something like this...
    Code:
    result=lookupArray[total/5];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write if statements shorter
    By tinchi in forum C Programming
    Replies: 6
    Last Post: 07-07-2014, 06:11 PM
  2. how can l make this shorter?
    By am687876 in forum C Programming
    Replies: 3
    Last Post: 05-28-2013, 11:12 AM
  3. shorter code
    By czarny020 in forum C Programming
    Replies: 10
    Last Post: 02-14-2011, 12:32 PM
  4. Any way to make shorter???
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-14-2002, 04:02 AM
  5. is there a better/shorter way to do a menu
    By sizzle_chest in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2001, 04:09 PM

Tags for this Thread