In C if a user enters an input like "andjdnd" in a in a place holder of char then the program crashes.How can we prevent the crash and warn the user about his/her wrong input?
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Show your code. You said place hold of Char. When using C and inputting a string, you have to have an array place hold of the char type. and in the last place of the array you have to have \0, which signals the end.
The issue is that you are using scanf with a %c argument, use a %s instead, this will just capture the first character typed in the input. Without that change what happens is that it takes the first character for the scanf with %c(which means only a single char) and the next set of chars for the next scanf and the return ends up blanking the final input.
So the time = a
the yrs = 0 (because of invalid input)
and sal = 0.0000
One of the problems with validating the input with the code you are using is that there is a basic input validation happening. To insure that you can validate the input you need to capture it, then you can validate and convert as necessary. When you type a char that is not an int to a %d value, what you end up capturing it 0. Then there is no way to to see what was actually typed.
Not the answer you are looking for? Search for more explanations.
In other situations,assume : char name; If a user types more characters(or some invalid character like #,$,%) then what piece of code should I include in my program to check ?There is a inbuilt function to check that which I can't recall.And if you can suggest any other solution?
The following code is an an example of testing if an input string is a number or not:
int isanumber = 1;
while(name[i] != '\0' && i < 10)
if (name[i] >= '0' && name[i] <= '9')
if (isanumber == 1)
printf("Is a number\n");
printf("Not a number\n");