How do I print Invalid for letters












0














How do I print invalid when someone enter big or small letters, because supposedly they only enter floats between 0 to 10.



I tried coding like this



It went so wrong.



#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("nnnnnrKindly enter the constentant score by 5 respected
judges:");

do
{
printf("nnScore 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));
}


okay this is my second code



#include<stdio.h>

int main()
{
float judge1;

printf("n%90s","Welcome to the constentant score calculator program :)");

printf("nnnnnrKindly enter the constentant score by 5 respected
judges:");

printf("nnScore 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:");
}
}









share|improve this question









New contributor




Aathf Addli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • what is char1 value ?
    – ntshetty
    Dec 27 at 14:37










  • char1 does not have value....i use it to print invalid when inserting letters @ThiruShetty
    – Aathf Addli
    Dec 27 at 14:47












  • @P__J__: judge1==char1>96 is not always zero, nor is judge1==char1<123 always 1. If judge1 is 1 and char1 is '|', then judge1==char1>96 is 1 and judge1==char1<123 is 0.
    – Eric Postpischil
    Dec 27 at 14:47












  • @P__J__ yes,i want it to work that way,so if they insert letters,it will print "Please input a valid score between 0 and 10:");",but unfortunately it doesnt work that way.
    – Aathf Addli
    Dec 27 at 14:52










  • @ThiruShetty char1 value is depends on the letters,by using ASCII code.
    – Aathf Addli
    Dec 27 at 14:54
















0














How do I print invalid when someone enter big or small letters, because supposedly they only enter floats between 0 to 10.



I tried coding like this



It went so wrong.



#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("nnnnnrKindly enter the constentant score by 5 respected
judges:");

do
{
printf("nnScore 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));
}


okay this is my second code



#include<stdio.h>

int main()
{
float judge1;

printf("n%90s","Welcome to the constentant score calculator program :)");

printf("nnnnnrKindly enter the constentant score by 5 respected
judges:");

printf("nnScore 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:");
}
}









share|improve this question









New contributor




Aathf Addli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • what is char1 value ?
    – ntshetty
    Dec 27 at 14:37










  • char1 does not have value....i use it to print invalid when inserting letters @ThiruShetty
    – Aathf Addli
    Dec 27 at 14:47












  • @P__J__: judge1==char1>96 is not always zero, nor is judge1==char1<123 always 1. If judge1 is 1 and char1 is '|', then judge1==char1>96 is 1 and judge1==char1<123 is 0.
    – Eric Postpischil
    Dec 27 at 14:47












  • @P__J__ yes,i want it to work that way,so if they insert letters,it will print "Please input a valid score between 0 and 10:");",but unfortunately it doesnt work that way.
    – Aathf Addli
    Dec 27 at 14:52










  • @ThiruShetty char1 value is depends on the letters,by using ASCII code.
    – Aathf Addli
    Dec 27 at 14:54














0












0








0


1





How do I print invalid when someone enter big or small letters, because supposedly they only enter floats between 0 to 10.



I tried coding like this



It went so wrong.



#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("nnnnnrKindly enter the constentant score by 5 respected
judges:");

do
{
printf("nnScore 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));
}


okay this is my second code



#include<stdio.h>

int main()
{
float judge1;

printf("n%90s","Welcome to the constentant score calculator program :)");

printf("nnnnnrKindly enter the constentant score by 5 respected
judges:");

printf("nnScore 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:");
}
}









share|improve this question









New contributor




Aathf Addli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











How do I print invalid when someone enter big or small letters, because supposedly they only enter floats between 0 to 10.



I tried coding like this



It went so wrong.



#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("nnnnnrKindly enter the constentant score by 5 respected
judges:");

do
{
printf("nnScore 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));
}


okay this is my second code



#include<stdio.h>

int main()
{
float judge1;

printf("n%90s","Welcome to the constentant score calculator program :)");

printf("nnnnnrKindly enter the constentant score by 5 respected
judges:");

printf("nnScore 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:");
}
}






c printf






share|improve this question









New contributor




Aathf Addli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Aathf Addli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Dec 27 at 15:37









dbush

92.7k12101132




92.7k12101132






New contributor




Aathf Addli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Dec 27 at 14:32









Aathf Addli

13




13




New contributor




Aathf Addli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Aathf Addli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Aathf Addli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • what is char1 value ?
    – ntshetty
    Dec 27 at 14:37










  • char1 does not have value....i use it to print invalid when inserting letters @ThiruShetty
    – Aathf Addli
    Dec 27 at 14:47












  • @P__J__: judge1==char1>96 is not always zero, nor is judge1==char1<123 always 1. If judge1 is 1 and char1 is '|', then judge1==char1>96 is 1 and judge1==char1<123 is 0.
    – Eric Postpischil
    Dec 27 at 14:47












  • @P__J__ yes,i want it to work that way,so if they insert letters,it will print "Please input a valid score between 0 and 10:");",but unfortunately it doesnt work that way.
    – Aathf Addli
    Dec 27 at 14:52










  • @ThiruShetty char1 value is depends on the letters,by using ASCII code.
    – Aathf Addli
    Dec 27 at 14:54


















  • what is char1 value ?
    – ntshetty
    Dec 27 at 14:37










  • char1 does not have value....i use it to print invalid when inserting letters @ThiruShetty
    – Aathf Addli
    Dec 27 at 14:47












  • @P__J__: judge1==char1>96 is not always zero, nor is judge1==char1<123 always 1. If judge1 is 1 and char1 is '|', then judge1==char1>96 is 1 and judge1==char1<123 is 0.
    – Eric Postpischil
    Dec 27 at 14:47












  • @P__J__ yes,i want it to work that way,so if they insert letters,it will print "Please input a valid score between 0 and 10:");",but unfortunately it doesnt work that way.
    – Aathf Addli
    Dec 27 at 14:52










  • @ThiruShetty char1 value is depends on the letters,by using ASCII code.
    – Aathf Addli
    Dec 27 at 14:54
















what is char1 value ?
– ntshetty
Dec 27 at 14:37




what is char1 value ?
– ntshetty
Dec 27 at 14:37












char1 does not have value....i use it to print invalid when inserting letters @ThiruShetty
– Aathf Addli
Dec 27 at 14:47






char1 does not have value....i use it to print invalid when inserting letters @ThiruShetty
– Aathf Addli
Dec 27 at 14:47














@P__J__: judge1==char1>96 is not always zero, nor is judge1==char1<123 always 1. If judge1 is 1 and char1 is '|', then judge1==char1>96 is 1 and judge1==char1<123 is 0.
– Eric Postpischil
Dec 27 at 14:47






@P__J__: judge1==char1>96 is not always zero, nor is judge1==char1<123 always 1. If judge1 is 1 and char1 is '|', then judge1==char1>96 is 1 and judge1==char1<123 is 0.
– Eric Postpischil
Dec 27 at 14:47














@P__J__ yes,i want it to work that way,so if they insert letters,it will print "Please input a valid score between 0 and 10:");",but unfortunately it doesnt work that way.
– Aathf Addli
Dec 27 at 14:52




@P__J__ yes,i want it to work that way,so if they insert letters,it will print "Please input a valid score between 0 and 10:");",but unfortunately it doesnt work that way.
– Aathf Addli
Dec 27 at 14:52












@ThiruShetty char1 value is depends on the letters,by using ASCII code.
– Aathf Addli
Dec 27 at 14:54




@ThiruShetty char1 value is depends on the letters,by using ASCII code.
– Aathf Addli
Dec 27 at 14:54












3 Answers
3






active

oldest

votes


















2














When you use the "%f" as the format string for scanf, it will read only characters that are valid for a floating point type and will stop reading if it detects any other characters. So if someone types "abc", nothing is written to judge1 and those characters are left in the input buffer to be read again. You'll then get stuck in an infinite loop reading those same characters.



Also, this expression doesn't make sense:



judge1=a>96


> has higher precedence than ==, so it is equivalent to:



judge1=(a>96)


Assuming a is assigned a value, a>96 compares that value with 96 and evaluates to either 0 or 1. Then you assign this value to judge1, overwriting what was read from the user. Assuming you meant to use == this doesn't make sense either. That being the case, either judge1==0 or judge1==1 is evaluated depending on the result of a>96. So the above expression will only be true if judge1 is 1 and a is greater than 96 or judge1 is 0 and a is less than or equal to 96.



Another problem is that a is never assigned a value. You seem to be under the impression that when you call scanf("%f",&judge1); that the first character read is written to a. There is no link that causes that to happen, so a is left uninitialized.



What you want to do instead is read in a line of text using fgets, then using strtof to read a float. The strtof function accepts the address of a pointer as the second parameter to let you know where in the string the parsing stopped. So if this pointer does not point to the null terminator at the end of the string (or to a newline character, since fgets reads and stores the newline), then you know you read a non-float character.



float judge1;
char line[100];
char *p;
int invalid_input;

do {
invalid_input = 0;
fgets(line, sizeof(line), stdin);
errno = 0;
judge1 = strtof(line, &p);
if (errno || ((*p != 0) && (*p != 'n')) || (judge1 < 0) || (judge1 > 10)) {
printf("Please input a valid score between 0 and 10:");
invalid_input = 1;
} else {
printf("The constentant got %.2f from the judgen ",judge1);
}
} while (invalid_input);





share|improve this answer























  • being a beginner in C,so im sorry,that i cant really understand every meaning of yours :(,So,can you help me to edit my code,so that when user enter a letter it will print "Please enter a valid input" instead of "The constentant score is 0.00" or to infinite loop ....i really dont get it sorry.
    – Aathf Addli
    Dec 27 at 15:27










  • @AathfAddli The code in my answer does what you need it to do. Read everything again carefully.
    – dbush
    Dec 27 at 15:30










  • would you mind to look on my second code?i edited my post,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"?
    – Aathf Addli
    Dec 27 at 15:38










  • @AathfAddli It has the same problem as the first piece of code, as described in the first paragraph of this answer, and is addressed in the code in the answer.
    – dbush
    Dec 27 at 15:40










  • so lets say,referring to your first paragraph,if i use %f,and the input entered is abc,i will always go to the infinite loop because it wont read it right?
    – Aathf Addli
    Dec 27 at 16:24



















1














First, check the return value of scanf. If it fails to match the item it will return 0. Then you can check whether the number entered is within bounds:



int r, judge1;
...
r = scanf("%d", &judge1);
if(r != 1)
{
printf("invalid inputn");
while((r = fgetc(stdin)) != EOF && r != 'n');
}
else if((judge1 < 0) || (judge1 > 10))
printf("input out of rangen");
else
printf("valid inputn");





share|improve this answer























  • he need to check even ASCII characters
    – ntshetty
    Dec 27 at 14:56










  • yes this code will work fine,but this is not the answer that im looking for,my problem is when entering the letter,for example k,it will print "The constentant score is 0.00",by looking through my codes,it should display"Please enter a valid input between 0 and 10",there you go,i edited my code....please help
    – Aathf Addli
    Dec 27 at 15:08










  • @AathfAddli you are wrong, if you follow what mnistic did it will do what you expect.
    – bruno
    Dec 27 at 15:42












  • @mnistic you must compare the result of scanf to 1 to manage all the error cases
    – bruno
    Dec 27 at 15:45










  • @bruno i see,but what does this mean if(!r)
    – Aathf Addli
    Dec 27 at 15:46



















0














The problem is you have the bad charachter stuck in the stream. The solution is here:
scanf fails why?.
The suggested thing to do from John Bode is to use a getchar to get it out. Also apply the check mnistic suggested and it should work.



it doesn't offer anything and it only messes with the greater picture.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Aathf Addli is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53946669%2fhow-do-i-print-invalid-for-letters%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    When you use the "%f" as the format string for scanf, it will read only characters that are valid for a floating point type and will stop reading if it detects any other characters. So if someone types "abc", nothing is written to judge1 and those characters are left in the input buffer to be read again. You'll then get stuck in an infinite loop reading those same characters.



    Also, this expression doesn't make sense:



    judge1=a>96


    > has higher precedence than ==, so it is equivalent to:



    judge1=(a>96)


    Assuming a is assigned a value, a>96 compares that value with 96 and evaluates to either 0 or 1. Then you assign this value to judge1, overwriting what was read from the user. Assuming you meant to use == this doesn't make sense either. That being the case, either judge1==0 or judge1==1 is evaluated depending on the result of a>96. So the above expression will only be true if judge1 is 1 and a is greater than 96 or judge1 is 0 and a is less than or equal to 96.



    Another problem is that a is never assigned a value. You seem to be under the impression that when you call scanf("%f",&judge1); that the first character read is written to a. There is no link that causes that to happen, so a is left uninitialized.



    What you want to do instead is read in a line of text using fgets, then using strtof to read a float. The strtof function accepts the address of a pointer as the second parameter to let you know where in the string the parsing stopped. So if this pointer does not point to the null terminator at the end of the string (or to a newline character, since fgets reads and stores the newline), then you know you read a non-float character.



    float judge1;
    char line[100];
    char *p;
    int invalid_input;

    do {
    invalid_input = 0;
    fgets(line, sizeof(line), stdin);
    errno = 0;
    judge1 = strtof(line, &p);
    if (errno || ((*p != 0) && (*p != 'n')) || (judge1 < 0) || (judge1 > 10)) {
    printf("Please input a valid score between 0 and 10:");
    invalid_input = 1;
    } else {
    printf("The constentant got %.2f from the judgen ",judge1);
    }
    } while (invalid_input);





    share|improve this answer























    • being a beginner in C,so im sorry,that i cant really understand every meaning of yours :(,So,can you help me to edit my code,so that when user enter a letter it will print "Please enter a valid input" instead of "The constentant score is 0.00" or to infinite loop ....i really dont get it sorry.
      – Aathf Addli
      Dec 27 at 15:27










    • @AathfAddli The code in my answer does what you need it to do. Read everything again carefully.
      – dbush
      Dec 27 at 15:30










    • would you mind to look on my second code?i edited my post,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"?
      – Aathf Addli
      Dec 27 at 15:38










    • @AathfAddli It has the same problem as the first piece of code, as described in the first paragraph of this answer, and is addressed in the code in the answer.
      – dbush
      Dec 27 at 15:40










    • so lets say,referring to your first paragraph,if i use %f,and the input entered is abc,i will always go to the infinite loop because it wont read it right?
      – Aathf Addli
      Dec 27 at 16:24
















    2














    When you use the "%f" as the format string for scanf, it will read only characters that are valid for a floating point type and will stop reading if it detects any other characters. So if someone types "abc", nothing is written to judge1 and those characters are left in the input buffer to be read again. You'll then get stuck in an infinite loop reading those same characters.



    Also, this expression doesn't make sense:



    judge1=a>96


    > has higher precedence than ==, so it is equivalent to:



    judge1=(a>96)


    Assuming a is assigned a value, a>96 compares that value with 96 and evaluates to either 0 or 1. Then you assign this value to judge1, overwriting what was read from the user. Assuming you meant to use == this doesn't make sense either. That being the case, either judge1==0 or judge1==1 is evaluated depending on the result of a>96. So the above expression will only be true if judge1 is 1 and a is greater than 96 or judge1 is 0 and a is less than or equal to 96.



    Another problem is that a is never assigned a value. You seem to be under the impression that when you call scanf("%f",&judge1); that the first character read is written to a. There is no link that causes that to happen, so a is left uninitialized.



    What you want to do instead is read in a line of text using fgets, then using strtof to read a float. The strtof function accepts the address of a pointer as the second parameter to let you know where in the string the parsing stopped. So if this pointer does not point to the null terminator at the end of the string (or to a newline character, since fgets reads and stores the newline), then you know you read a non-float character.



    float judge1;
    char line[100];
    char *p;
    int invalid_input;

    do {
    invalid_input = 0;
    fgets(line, sizeof(line), stdin);
    errno = 0;
    judge1 = strtof(line, &p);
    if (errno || ((*p != 0) && (*p != 'n')) || (judge1 < 0) || (judge1 > 10)) {
    printf("Please input a valid score between 0 and 10:");
    invalid_input = 1;
    } else {
    printf("The constentant got %.2f from the judgen ",judge1);
    }
    } while (invalid_input);





    share|improve this answer























    • being a beginner in C,so im sorry,that i cant really understand every meaning of yours :(,So,can you help me to edit my code,so that when user enter a letter it will print "Please enter a valid input" instead of "The constentant score is 0.00" or to infinite loop ....i really dont get it sorry.
      – Aathf Addli
      Dec 27 at 15:27










    • @AathfAddli The code in my answer does what you need it to do. Read everything again carefully.
      – dbush
      Dec 27 at 15:30










    • would you mind to look on my second code?i edited my post,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"?
      – Aathf Addli
      Dec 27 at 15:38










    • @AathfAddli It has the same problem as the first piece of code, as described in the first paragraph of this answer, and is addressed in the code in the answer.
      – dbush
      Dec 27 at 15:40










    • so lets say,referring to your first paragraph,if i use %f,and the input entered is abc,i will always go to the infinite loop because it wont read it right?
      – Aathf Addli
      Dec 27 at 16:24














    2












    2








    2






    When you use the "%f" as the format string for scanf, it will read only characters that are valid for a floating point type and will stop reading if it detects any other characters. So if someone types "abc", nothing is written to judge1 and those characters are left in the input buffer to be read again. You'll then get stuck in an infinite loop reading those same characters.



    Also, this expression doesn't make sense:



    judge1=a>96


    > has higher precedence than ==, so it is equivalent to:



    judge1=(a>96)


    Assuming a is assigned a value, a>96 compares that value with 96 and evaluates to either 0 or 1. Then you assign this value to judge1, overwriting what was read from the user. Assuming you meant to use == this doesn't make sense either. That being the case, either judge1==0 or judge1==1 is evaluated depending on the result of a>96. So the above expression will only be true if judge1 is 1 and a is greater than 96 or judge1 is 0 and a is less than or equal to 96.



    Another problem is that a is never assigned a value. You seem to be under the impression that when you call scanf("%f",&judge1); that the first character read is written to a. There is no link that causes that to happen, so a is left uninitialized.



    What you want to do instead is read in a line of text using fgets, then using strtof to read a float. The strtof function accepts the address of a pointer as the second parameter to let you know where in the string the parsing stopped. So if this pointer does not point to the null terminator at the end of the string (or to a newline character, since fgets reads and stores the newline), then you know you read a non-float character.



    float judge1;
    char line[100];
    char *p;
    int invalid_input;

    do {
    invalid_input = 0;
    fgets(line, sizeof(line), stdin);
    errno = 0;
    judge1 = strtof(line, &p);
    if (errno || ((*p != 0) && (*p != 'n')) || (judge1 < 0) || (judge1 > 10)) {
    printf("Please input a valid score between 0 and 10:");
    invalid_input = 1;
    } else {
    printf("The constentant got %.2f from the judgen ",judge1);
    }
    } while (invalid_input);





    share|improve this answer














    When you use the "%f" as the format string for scanf, it will read only characters that are valid for a floating point type and will stop reading if it detects any other characters. So if someone types "abc", nothing is written to judge1 and those characters are left in the input buffer to be read again. You'll then get stuck in an infinite loop reading those same characters.



    Also, this expression doesn't make sense:



    judge1=a>96


    > has higher precedence than ==, so it is equivalent to:



    judge1=(a>96)


    Assuming a is assigned a value, a>96 compares that value with 96 and evaluates to either 0 or 1. Then you assign this value to judge1, overwriting what was read from the user. Assuming you meant to use == this doesn't make sense either. That being the case, either judge1==0 or judge1==1 is evaluated depending on the result of a>96. So the above expression will only be true if judge1 is 1 and a is greater than 96 or judge1 is 0 and a is less than or equal to 96.



    Another problem is that a is never assigned a value. You seem to be under the impression that when you call scanf("%f",&judge1); that the first character read is written to a. There is no link that causes that to happen, so a is left uninitialized.



    What you want to do instead is read in a line of text using fgets, then using strtof to read a float. The strtof function accepts the address of a pointer as the second parameter to let you know where in the string the parsing stopped. So if this pointer does not point to the null terminator at the end of the string (or to a newline character, since fgets reads and stores the newline), then you know you read a non-float character.



    float judge1;
    char line[100];
    char *p;
    int invalid_input;

    do {
    invalid_input = 0;
    fgets(line, sizeof(line), stdin);
    errno = 0;
    judge1 = strtof(line, &p);
    if (errno || ((*p != 0) && (*p != 'n')) || (judge1 < 0) || (judge1 > 10)) {
    printf("Please input a valid score between 0 and 10:");
    invalid_input = 1;
    } else {
    printf("The constentant got %.2f from the judgen ",judge1);
    }
    } while (invalid_input);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 27 at 15:39

























    answered Dec 27 at 15:17









    dbush

    92.7k12101132




    92.7k12101132












    • being a beginner in C,so im sorry,that i cant really understand every meaning of yours :(,So,can you help me to edit my code,so that when user enter a letter it will print "Please enter a valid input" instead of "The constentant score is 0.00" or to infinite loop ....i really dont get it sorry.
      – Aathf Addli
      Dec 27 at 15:27










    • @AathfAddli The code in my answer does what you need it to do. Read everything again carefully.
      – dbush
      Dec 27 at 15:30










    • would you mind to look on my second code?i edited my post,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"?
      – Aathf Addli
      Dec 27 at 15:38










    • @AathfAddli It has the same problem as the first piece of code, as described in the first paragraph of this answer, and is addressed in the code in the answer.
      – dbush
      Dec 27 at 15:40










    • so lets say,referring to your first paragraph,if i use %f,and the input entered is abc,i will always go to the infinite loop because it wont read it right?
      – Aathf Addli
      Dec 27 at 16:24


















    • being a beginner in C,so im sorry,that i cant really understand every meaning of yours :(,So,can you help me to edit my code,so that when user enter a letter it will print "Please enter a valid input" instead of "The constentant score is 0.00" or to infinite loop ....i really dont get it sorry.
      – Aathf Addli
      Dec 27 at 15:27










    • @AathfAddli The code in my answer does what you need it to do. Read everything again carefully.
      – dbush
      Dec 27 at 15:30










    • would you mind to look on my second code?i edited my post,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"?
      – Aathf Addli
      Dec 27 at 15:38










    • @AathfAddli It has the same problem as the first piece of code, as described in the first paragraph of this answer, and is addressed in the code in the answer.
      – dbush
      Dec 27 at 15:40










    • so lets say,referring to your first paragraph,if i use %f,and the input entered is abc,i will always go to the infinite loop because it wont read it right?
      – Aathf Addli
      Dec 27 at 16:24
















    being a beginner in C,so im sorry,that i cant really understand every meaning of yours :(,So,can you help me to edit my code,so that when user enter a letter it will print "Please enter a valid input" instead of "The constentant score is 0.00" or to infinite loop ....i really dont get it sorry.
    – Aathf Addli
    Dec 27 at 15:27




    being a beginner in C,so im sorry,that i cant really understand every meaning of yours :(,So,can you help me to edit my code,so that when user enter a letter it will print "Please enter a valid input" instead of "The constentant score is 0.00" or to infinite loop ....i really dont get it sorry.
    – Aathf Addli
    Dec 27 at 15:27












    @AathfAddli The code in my answer does what you need it to do. Read everything again carefully.
    – dbush
    Dec 27 at 15:30




    @AathfAddli The code in my answer does what you need it to do. Read everything again carefully.
    – dbush
    Dec 27 at 15:30












    would you mind to look on my second code?i edited my post,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"?
    – Aathf Addli
    Dec 27 at 15:38




    would you mind to look on my second code?i edited my post,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"?
    – Aathf Addli
    Dec 27 at 15:38












    @AathfAddli It has the same problem as the first piece of code, as described in the first paragraph of this answer, and is addressed in the code in the answer.
    – dbush
    Dec 27 at 15:40




    @AathfAddli It has the same problem as the first piece of code, as described in the first paragraph of this answer, and is addressed in the code in the answer.
    – dbush
    Dec 27 at 15:40












    so lets say,referring to your first paragraph,if i use %f,and the input entered is abc,i will always go to the infinite loop because it wont read it right?
    – Aathf Addli
    Dec 27 at 16:24




    so lets say,referring to your first paragraph,if i use %f,and the input entered is abc,i will always go to the infinite loop because it wont read it right?
    – Aathf Addli
    Dec 27 at 16:24













    1














    First, check the return value of scanf. If it fails to match the item it will return 0. Then you can check whether the number entered is within bounds:



    int r, judge1;
    ...
    r = scanf("%d", &judge1);
    if(r != 1)
    {
    printf("invalid inputn");
    while((r = fgetc(stdin)) != EOF && r != 'n');
    }
    else if((judge1 < 0) || (judge1 > 10))
    printf("input out of rangen");
    else
    printf("valid inputn");





    share|improve this answer























    • he need to check even ASCII characters
      – ntshetty
      Dec 27 at 14:56










    • yes this code will work fine,but this is not the answer that im looking for,my problem is when entering the letter,for example k,it will print "The constentant score is 0.00",by looking through my codes,it should display"Please enter a valid input between 0 and 10",there you go,i edited my code....please help
      – Aathf Addli
      Dec 27 at 15:08










    • @AathfAddli you are wrong, if you follow what mnistic did it will do what you expect.
      – bruno
      Dec 27 at 15:42












    • @mnistic you must compare the result of scanf to 1 to manage all the error cases
      – bruno
      Dec 27 at 15:45










    • @bruno i see,but what does this mean if(!r)
      – Aathf Addli
      Dec 27 at 15:46
















    1














    First, check the return value of scanf. If it fails to match the item it will return 0. Then you can check whether the number entered is within bounds:



    int r, judge1;
    ...
    r = scanf("%d", &judge1);
    if(r != 1)
    {
    printf("invalid inputn");
    while((r = fgetc(stdin)) != EOF && r != 'n');
    }
    else if((judge1 < 0) || (judge1 > 10))
    printf("input out of rangen");
    else
    printf("valid inputn");





    share|improve this answer























    • he need to check even ASCII characters
      – ntshetty
      Dec 27 at 14:56










    • yes this code will work fine,but this is not the answer that im looking for,my problem is when entering the letter,for example k,it will print "The constentant score is 0.00",by looking through my codes,it should display"Please enter a valid input between 0 and 10",there you go,i edited my code....please help
      – Aathf Addli
      Dec 27 at 15:08










    • @AathfAddli you are wrong, if you follow what mnistic did it will do what you expect.
      – bruno
      Dec 27 at 15:42












    • @mnistic you must compare the result of scanf to 1 to manage all the error cases
      – bruno
      Dec 27 at 15:45










    • @bruno i see,but what does this mean if(!r)
      – Aathf Addli
      Dec 27 at 15:46














    1












    1








    1






    First, check the return value of scanf. If it fails to match the item it will return 0. Then you can check whether the number entered is within bounds:



    int r, judge1;
    ...
    r = scanf("%d", &judge1);
    if(r != 1)
    {
    printf("invalid inputn");
    while((r = fgetc(stdin)) != EOF && r != 'n');
    }
    else if((judge1 < 0) || (judge1 > 10))
    printf("input out of rangen");
    else
    printf("valid inputn");





    share|improve this answer














    First, check the return value of scanf. If it fails to match the item it will return 0. Then you can check whether the number entered is within bounds:



    int r, judge1;
    ...
    r = scanf("%d", &judge1);
    if(r != 1)
    {
    printf("invalid inputn");
    while((r = fgetc(stdin)) != EOF && r != 'n');
    }
    else if((judge1 < 0) || (judge1 > 10))
    printf("input out of rangen");
    else
    printf("valid inputn");






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 28 at 0:44

























    answered Dec 27 at 14:54









    mnistic

    7,0131820




    7,0131820












    • he need to check even ASCII characters
      – ntshetty
      Dec 27 at 14:56










    • yes this code will work fine,but this is not the answer that im looking for,my problem is when entering the letter,for example k,it will print "The constentant score is 0.00",by looking through my codes,it should display"Please enter a valid input between 0 and 10",there you go,i edited my code....please help
      – Aathf Addli
      Dec 27 at 15:08










    • @AathfAddli you are wrong, if you follow what mnistic did it will do what you expect.
      – bruno
      Dec 27 at 15:42












    • @mnistic you must compare the result of scanf to 1 to manage all the error cases
      – bruno
      Dec 27 at 15:45










    • @bruno i see,but what does this mean if(!r)
      – Aathf Addli
      Dec 27 at 15:46


















    • he need to check even ASCII characters
      – ntshetty
      Dec 27 at 14:56










    • yes this code will work fine,but this is not the answer that im looking for,my problem is when entering the letter,for example k,it will print "The constentant score is 0.00",by looking through my codes,it should display"Please enter a valid input between 0 and 10",there you go,i edited my code....please help
      – Aathf Addli
      Dec 27 at 15:08










    • @AathfAddli you are wrong, if you follow what mnistic did it will do what you expect.
      – bruno
      Dec 27 at 15:42












    • @mnistic you must compare the result of scanf to 1 to manage all the error cases
      – bruno
      Dec 27 at 15:45










    • @bruno i see,but what does this mean if(!r)
      – Aathf Addli
      Dec 27 at 15:46
















    he need to check even ASCII characters
    – ntshetty
    Dec 27 at 14:56




    he need to check even ASCII characters
    – ntshetty
    Dec 27 at 14:56












    yes this code will work fine,but this is not the answer that im looking for,my problem is when entering the letter,for example k,it will print "The constentant score is 0.00",by looking through my codes,it should display"Please enter a valid input between 0 and 10",there you go,i edited my code....please help
    – Aathf Addli
    Dec 27 at 15:08




    yes this code will work fine,but this is not the answer that im looking for,my problem is when entering the letter,for example k,it will print "The constentant score is 0.00",by looking through my codes,it should display"Please enter a valid input between 0 and 10",there you go,i edited my code....please help
    – Aathf Addli
    Dec 27 at 15:08












    @AathfAddli you are wrong, if you follow what mnistic did it will do what you expect.
    – bruno
    Dec 27 at 15:42






    @AathfAddli you are wrong, if you follow what mnistic did it will do what you expect.
    – bruno
    Dec 27 at 15:42














    @mnistic you must compare the result of scanf to 1 to manage all the error cases
    – bruno
    Dec 27 at 15:45




    @mnistic you must compare the result of scanf to 1 to manage all the error cases
    – bruno
    Dec 27 at 15:45












    @bruno i see,but what does this mean if(!r)
    – Aathf Addli
    Dec 27 at 15:46




    @bruno i see,but what does this mean if(!r)
    – Aathf Addli
    Dec 27 at 15:46











    0














    The problem is you have the bad charachter stuck in the stream. The solution is here:
    scanf fails why?.
    The suggested thing to do from John Bode is to use a getchar to get it out. Also apply the check mnistic suggested and it should work.



    it doesn't offer anything and it only messes with the greater picture.






    share|improve this answer


























      0














      The problem is you have the bad charachter stuck in the stream. The solution is here:
      scanf fails why?.
      The suggested thing to do from John Bode is to use a getchar to get it out. Also apply the check mnistic suggested and it should work.



      it doesn't offer anything and it only messes with the greater picture.






      share|improve this answer
























        0












        0








        0






        The problem is you have the bad charachter stuck in the stream. The solution is here:
        scanf fails why?.
        The suggested thing to do from John Bode is to use a getchar to get it out. Also apply the check mnistic suggested and it should work.



        it doesn't offer anything and it only messes with the greater picture.






        share|improve this answer












        The problem is you have the bad charachter stuck in the stream. The solution is here:
        scanf fails why?.
        The suggested thing to do from John Bode is to use a getchar to get it out. Also apply the check mnistic suggested and it should work.



        it doesn't offer anything and it only messes with the greater picture.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 27 at 15:16









        Jordan Sapida

        226




        226






















            Aathf Addli is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Aathf Addli is a new contributor. Be nice, and check out our Code of Conduct.













            Aathf Addli is a new contributor. Be nice, and check out our Code of Conduct.












            Aathf Addli is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53946669%2fhow-do-i-print-invalid-for-letters%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas