anonymous
  • anonymous
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
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
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.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
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.
anonymous
  • anonymous
This question came to me when i was answering a question.See the code here: http://openstudy.com/study?F11400362549395HFVVQ=_#/updates/4f0c037ee4b014c09e652046 Also how to check that the input is a char or an int?
anonymous
  • anonymous
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.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
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?
anonymous
  • anonymous
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; }

Looking for something else?

Not the answer you are looking for? Search for more explanations.