Thread: How to print invalid

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    5

    How to print invalid

    hello,

    I actually wants to print my "invalid" to the user when they enter letters because supposedly they enter integers between 0 and 10,but i tried and when i ran it by entering a letter,it went wrong,this is my code

    Thanks for your kindness
    Code:
    #include<stdio.h>
    
    
    
    int main()
    
    
    {
    	int trial=0;
    	float judge1=0,judge2,judge3,judge4,judge5;
    	char a;
    	
    	printf("\n%90s","Welcome to the constentant score calculator program :)");
    	
    	printf("\n\n\n\n\n\rKindly enter the constentant score by 5 respected judges:");
    	do
    {
    	printf("\n\nScore by JUDGE 1 (0-10):\t");
    	scanf("%f",&judge1);
    
    
    if ((judge1>-1)&& (judge1<11) )
    	
      printf("The constentant got %.2f from the judge",judge1);
    
    
    else
    	printf("\aPlease input a valid score between 0 and 10:");
    
    
    
    
    } while ((judge1<0) || (judge1>10)||(judge1=a>96) && (judge1=a<123)||(judge1=a<91) && (judge1=a>64));
    
    
    
    
    }
    
    
    #include<stdio.h>
    
    
    
    int main()
    
    
    {
        int trial=0;
        float judge1,judge2,judge3,judge4,judge5;
        char char1;
        
        printf("\n%90s","Welcome to the constentant score calculator program :)");
        
        printf("\n\n\n\n\n\rKindly enter the constentant score by 5 respected judges:");
        do
    {
        printf("\n\nScore by JUDGE 1 (0-10):\t");
        scanf("%f",&judge1);
    
    
    if ((judge1<0)|| (judge1>10) || (judge1==char1>96) || (judge1==char1<123) || (judge1==char1<91) || (judge1==char1>64))
        
    printf("\aPlease input a valid score between 0 and 10:");
    
    
    else
        
    printf("The constentant got %.2f from the judge",judge1);
    
    
    } while ((judge1<0)|| (judge1>10) || (judge1==char1>96) || (judge1==char1<123) || (judge1==char1<91) || (judge1==char1>64));
    
    
    
    
    }
    Last edited by Aathif; 12-27-2018 at 09:00 AM. Reason: new code

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by Aathif
    I actually wants to print my "invalid" to the user when they enter letters because supposedly they enter integers between 0 and 10
    If they are supposed to enter integers, then why are your variables floats?

    I would suggest something like this:
    Code:
    int score;
    if (scanf("%d", &score) == 1 && score >= 0 && score <= 10)
    {
        /* score is valid */
    }
    else
    {
        /* score is invalid */
    }
    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
    Registered User
    Join Date
    Dec 2018
    Posts
    5
    Quote Originally Posted by laserlight View Post
    If they are supposed to enter integers, then why are your variables floats?

    I would suggest something like this:
    Code:
    int score;
    if (scanf("%d", &score) == 1 && score >= 0 && score <= 10)
    {
        /* score is valid */
    }
    else
    {
        /* score is invalid */
    }
    opss sorry,they supposedly enter floats,but actually the problem here is to print the "Please input a valid score between 0 and 10:");" when users enter letters.

    Can you help me ?

  4. #4
    Registered User
    Join Date
    Dec 2018
    Posts
    5
    I edited the code,and the output i got from the code when entering letters is

    "The constentant got 0.00 from the judge"

    although the code is like this(same as above)
    Code:
    #include<stdio.h>
    
    
    
    int main()
    
    
    {
    	int trial=0;
    	float judge1=0,judge2,judge3,judge4,judge5;
    	char a;
    	
    	printf("\n%90s","Welcome to the constentant score calculator program :)");
    	
    	printf("\n\n\n\n\n\rKindly enter the constentant score by 5 respected judges:");
    	do
    {
    	printf("\n\nScore by JUDGE 1 (0-10):\t");
    	scanf("%f",&judge1);
    
    
    if ((judge1>-1)&& (judge1<11) )
    	
      printf("The constentant got %.2f from the judge",judge1);
    
    
    else
    	printf("\aPlease input a valid score between 0 and 10:");
    
    
    
    
    } while ((judge1<0) || (judge1>10)||(judge1=a>96) && (judge1=a<123)||(judge1=a<91) && (judge1=a>64));
    
    
    
    
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    You need to check the return value of scanf.
    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

  6. #6
    Registered User
    Join Date
    Dec 2018
    Posts
    5
    Quote Originally Posted by laserlight View Post
    You need to check the return value of scanf.
    I dont get you

  7. #7
    Registered User
    Join Date
    Dec 2018
    Posts
    5
    What about this code?



    Code:
    #include<stdio.h>
    
    
    
    
    int main()
    
    
    {
    
    
    	float judge1;
    	
    	
    	printf("\n%90s","Welcome to the constentant score calculator program :)");
    	
    	printf("\n\n\n\n\n\rKindly enter the constentant score by 5 respected judges:");
    
    
    	printf("\n\nScore by JUDGE 1 (0-10):\t");
    	scanf("%f",&judge1);
    
    
    if ((judge1>-1) && (judge1<11))
    	
      printf("The constentant got %.2f from the judge",judge1);
    
    
    else
    	printf("\aPlease input a valid score between 0 and 10:");
    }

    so the question for the second code it that,why is it when i enter letter "k" as input,it displays "The constentants got 0.00 score from the judge"?Isnt it supppose to display "Please enter a vlid input between 0 and 10"?

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,736
    Quote Originally Posted by Aathif View Post
    I dont get you
    scanf() as a function returns a value that tells you how many things it managed to read, or EOF in case of an I/O error. In your case, it would return zero if the user tries to give a letter without giving any numbers before that. Just to set something straight, scanf() tries to read as far as is logically possible, meaning that if you give it "5x", it would read the "5" successfully as an entire number.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print statement won't print
    By Sakari in forum C Programming
    Replies: 17
    Last Post: 06-20-2012, 06:41 PM
  2. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  3. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  4. Replies: 0
    Last Post: 03-28-2003, 08:20 AM
  5. print first and print last function
    By RawleyMacias in forum C Programming
    Replies: 6
    Last Post: 02-14-2002, 10:28 PM

Tags for this Thread