Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

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?

Computer Science
I got my questions answered at in under 10 minutes. Go to now for free help!
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.

Join Brainly to access

this expert answer


To see the expert answer you'll need to create a free account at Brainly

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.
This question came to me when i was answering a question.See the code here: Also how to check that the input is a char or an int?
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.

Ask your own question

Other answers:

In other situations,assume : char name[10]; 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: #include int main() { char name[10]; int i=0; int isanumber = 1; scanf("%s",&name); while(name[i] != '\0' && i < 10) { if (name[i] >= '0' && name[i] <= '9') { } else { isanumber =0; } i++; } if (isanumber == 1) { printf("Is a number\n"); } else { printf("Not a number\n"); } return 0; }

Not the answer you are looking for?

Search for more explanations.

Ask your own question